WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

OLE with Word

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Insert Text and CRLFs into a Word Doc


Question:

I am writing some code automate a MS Word by putting text into a document. The fields that are in this file are the fields that typically are part of a letter heading:
Name
Address
City,State Zip 

Dear Contact_First_Names:
I need to put a CRLF after each field except for the salutation which needs 2 lines.

Answer:

To insert a CRLF use the TypeParagraph Method.

To insert text use the TypeText Method.

Sample Code:

filetocreate = "C:\temp\WordTemp.doc"

oAPP = ObjectCreate("Word.Application")
oConstants = ObjectConstantsGet(oAPP)
oAPP.Application.visible = @true
oAPP.Application.ChangeFileOpenDirectory(FilePath(filetocreate))
oAPP.Application.Documents.Add
oActive = oAPP.Application.ActiveDocument

oAPP.Application.Selection.Range.EndOf(:: Unit=oConstants.wdStory, Extend=oConstants.wdMove)
oAPP.Application.Selection.TypeText("First and Last Name")
oAPP.Application.Selection.TypeParagraph ; a @CRLF
oAPP.Application.Selection.TypeText("Address")
oAPP.Application.Selection.TypeParagraph; a @CRLF
oAPP.Application.Selection.TypeText("City, State Zip")
oAPP.Application.Selection.TypeParagraph
oAPP.Application.Selection.TypeParagraph
oAPP.Application.Selection.TypeText("Dear Friend:")

Pause("Debugging",StrCat("DONE - Check the contents of the file ",filetocreate))

oAPP.Application.ActiveDocument.SaveAs(StrCat(FileRoot(filetocreate),".",FileExtension(filetocreate)))
oAPP.Application.ActiveDocument.Close
oAPP.Application.Quit
oActive = 0
oApp = 0


Article ID:   W16645
File Created: 2005:12:12:16:11:20
Last Updated: 2005:12:12:16:11:20