PDA

View Full Version : Solved: Opening word from access



icthus123
06-08-2007, 04:25 AM
I'm trying to open a word document from access and enter some data from my access form into the document.

I've used this code:


Dim objWord As Object
Dim objDocument As Word.Document

Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDocument = objWord.Documents.Add

This opens a new word document. However, I can't work out how to go from here to entering text automatically into the document. Can anyone help?

Ebrow
06-08-2007, 05:30 AM
Basically you want to use the "objWord." in front of any VBA code that you would normally use if you created a macro in word.

I would add the reference MS Word to your excel workbook, this will allow the autocorrections of your syntax when typing out your code.

example:


Selection.TypeText Text:="insert text here" 'this is for Word Only

objWord.Selection.TypeText Text:="insert text here" 'this is used in your sub.



You want to use the following code to insert text


objWord.Selection.TypeText Text:="insert text here"


or by variable


objWord.Selection.TypeText Text:=myVariable


you can also enter paragraphs by doing:


objWord.Selection.TypeParagraph

icthus123
06-08-2007, 06:18 AM
Thankyou very much Ebrow. That was almost what I was doing. Except I thought I'd have to use objDocument like this:


objDocument.Selection.TypeText Text:="insert text here"


I think that must have been what was causing me problems.