Using VB to insert text into Word

Using VB to insert text into Word

Post by Rishi Roon » Sat, 02 Aug 2003 04:21:43


I am trying to write a Visual Basic program that will
allow me to open an existing word document and then place
text at a certain line number and column number. How do I
go about doing this???

thanks very much
 
 
 

Using VB to insert text into Word

Post by Lars Lykke » Sat, 02 Aug 2003 05:56:43

Hi Rishi

I tried moving the cursor to a desired line/column while in macro recording
mode, which gave me this macro:

Sub Macro1()
Selection.MoveDown Unit:=wdLine, Count:=10
Selection.MoveRight Unit:=wdCharacter, Count:=5
End Sub

Or maybe better by using Edit-goto-line which gave this:

Sub Macro2()
Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst, Count:=10
Selection.MoveRight Unit:=wdCharacter, Count:=5
End Sub

Naturally you must make sure that you start from the top so the first
statement may be:

Selection.GoTo What:=wdGoToPage, Which:=wdGoToFirst

regards

Lars

 
 
 

Using VB to insert text into Word

Post by Doug Robbi » Sun, 03 Aug 2003 10:21:28

Hi Rishi,

Best thing to do is either insert a bookmark at the location and use

ActiveDocument.Bookmarks("BookMarkName").Range.InsertBefore "Your Text"

or use

ActiveDocument.Variables("varname").Value = "Your Text"

and at the location in the document, have a [ DOCVARIABLE "varname " }
field.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
 
 

Using VB to insert text into Word

Post by Rishi Roon » Wed, 06 Aug 2003 04:10:14

Thanks for the past replies. They were helpful but I am
still havin a little trouble. If I am just using Visual
Basic 6.0, not the editor in WORD, how do I go about
opening a word file. Then inserting any text at any
location, ie column 45, line 23.

I can run the editor and open a file and make changes..
but I want to be able to use VB.. not just an editor.
Thanks very much..

- Rishi