How do I write to a specific position in a Word document, for example, line 5, character 50? I have searched for a couple of hours, but couldn't find a solution.
I am using Microsoft.Office.Interop.Word
Go to Insert > Text Box, and then select Draw Text Box. Click or tap in the document, and drag to draw the text box the size that you want. To add text to a text box, select inside the text box, and then type or paste text.
Select the texts you want to move to another place, and then press shortcut key “Ctrl + X” to do a cut. Move your cursor where you want the texts to go, and then paste it in by pressing shortcut key “Ctrl + V”.
Using the insertion point to add text You can use the insertion point in a variety of ways. Blank document: When a new blank document opens, the insertion point will appear in the top-left corner of the page. If you want, you can begin typing from this location.
If you are content with the more simple sentence, rather than lines:
ActiveDocument.Sentences(1).Characters(5).Select
Selection.Collapse
Selection.InsertBefore "added "
Five paragraphs and 50 spaces in VBA
Selection.Text = String(5, vbCrLf)
Selection.Collapse wdCollapseEnd
Selection.Text = String(50, " ")
However, for a particular position, I would prefer a textbox:
Set sh = doc.Shapes.AddTextbox(1, 10, 344, 575, 80)
sh.Name = "Course1"
With some properties:
sh.Fill.Visible = False
sh.Line.Visible = False
sh.TextFrame.MarginLeft = 0#
sh.TextFrame.MarginRight = 0#
sh.TextFrame.MarginTop = 0#
sh.TextFrame.MarginBottom = 0#
If you will be inserting text at the same location every time a simple way to do this is to create a .dotx template file with a bookmark at the location. Make sure the template is included in the build
Doc = Word.Documents.Add("Directory\Filename")
Doc.Bookmarks.Item("BookmarkName").Range.Text = "Text to be inserted"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With