PDA

View Full Version : Code to insert text



mari_hitz
07-22-2012, 04:32 PM
Hi Everybody,

I am new here and in the world of VBA code for word.
I am trying to create a document for people to send me templates of e-mails and where the variable's strings are supposed to be written I would like to insert a code in a button for the text to be inserted where the cursor is.
The code that I have used I founded in the net, it works fine, however when I save and close the document, the text inserts at the end of the document instead of where the cursor is, do you have any idea why?

This is the code:

Sub InsertAfterMethod() Dim MyText As String Dim MyRange As Object Set MyRange = ActiveDocument.Range MyText = "<Replace this with your text>" MyRange.InsertAfter (MyText) End Sub

fumei
07-22-2012, 05:35 PM
Code toisert text at the cursor would be something like:Selection.Collapse ' in case you have something selected
Selection.InsertAfter MyText
When you set a range for the whole documentSet MyRange = ActiveDocument.Range Then inserting text after will do just that...at the end of the range/document.