Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add text into Word Documents at a specific position?

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

like image 540
mahboub_mo Avatar asked Aug 22 '12 10:08

mahboub_mo


People also ask

How do I insert text wherever I want in 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.

How can you move a text from one position to another in a Word document?

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”.

Which Word feature allows you to quickly add text to a specific position on the page?

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.


2 Answers

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#
like image 68
Fionnuala Avatar answered Oct 17 '22 02:10

Fionnuala


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"
like image 45
Wayne Avatar answered Oct 17 '22 03:10

Wayne