Using Word Objects from MS-Access 2007 VBA (1 Viewer)

ChislePip

Registered User.
Local time
Tomorrow, 05:26
Joined
Mar 14, 2014
Messages
14
First ever thread.... I'm using MS-Access 2007 for a client and need to manipulate Word documents. I'm finding it difficult to find the definitive list of objects, methods and properties to use in this version of VBA. The specific need is akin to a 'merge'. I need to (i) open a blank Word document (which contains the required styles), (ii) copy the contents of selected Word files into the blank document, (iii) SAVE AS another file name.
I've experimented with code equivalents of "open document; select all; copy; select base document; paste" etc, but am getting syntax errors. Most help and samples are from later versions of Access.
 

JHB

Have been here a while
Local time
Today, 19:26
Joined
Jun 17, 2012
Messages
7,732
Show the code you have and also the error number and description you get + where in the code you get it.
 

ChislePip

Registered User.
Local time
Tomorrow, 05:26
Joined
Mar 14, 2014
Messages
14
Apologies for the delay. Thanks for responding. In the following code (which I've largely plagiarised from other peoples samples), I get a run-time error 424 (Object required) on the statement "Selection.wholestory". I'm guessing "wholestory" is a later syntax, which is not known to Access 2007. Just to re-iterate, I'm new to the manipulation of Word docs, but I think the code shows what I'm trying to do. ie Open a base doc, copy doc 1 into it, copy doc 2 into it, then save the resulting content. Hope you can help.:(
 

ChislePip

Registered User.
Local time
Tomorrow, 05:26
Joined
Mar 14, 2014
Messages
14
Errr... stupid here forgot to paste the code!!

Sub MergeDocuments()
Dim originalDoc As Document, tempDoc As Document, newDoc As Document
Dim myPath As String, myPath1 As String, myPath2 As String, myPath3 As String
myPath = "C:\Users\xxxxxxxxx\" 'ThisDocument.Path
strDocBase = "Skeleton.docx"
strDoc1 = "Test Doc A.docx"
strDoc2 = "Test Doc B.docx"
myPathBase = myPath & strDocBase
myPath1 = myPath & strDoc1
myPath2 = myPath & strDoc2
Set objApp = CreateObject("Word.Application")

'Open base document & 'activate' it
Set docInput = objApp.Documents.Open(myPathBase)
objApp.Documents(myPathBase).Activate

'Open first document to be copied and pasted, & 'activate' it
Set docTemp = objApp.Documents.Open(myPath1)
objApp.Documents(myPath1).Activate
Selection.wholestory
Selection.Copy

'Switch back to Base document and paste content of doc1
objApp.Documents(myPathBase).Activate
Selection.PasteAndFormat (wdPasteDefault)

'Open second document to be copied and pasted, & 'activate' it
objApp.Documents(myPath2).Activate
Selection.wholestory
Selection.Copy

'Switch back to Base document and paste content of doc2
objApp.Documents(myPathBase).Activate
Selection.PasteAndFormat (wdPasteDefault)

'Save resultant merged content as a new doc
ActiveDocument.SaveAs FileName:="Test2.docx", FileFormat:= _
wdFormatXMLDocument, LockComments:=False, Password:="", AddToRecentFiles _
:=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
:=True, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
End Sub
 

JHB

Have been here a while
Local time
Today, 19:26
Joined
Jun 17, 2012
Messages
7,732
I think you need the Word object you created in front of it.
Code:
objApp.Selection.WholeStory
 

ChislePip

Registered User.
Local time
Tomorrow, 05:26
Joined
Mar 14, 2014
Messages
14
Thanks again. Adding that code leads to error 438 ("Object doesn't support this property or method") in reference to the syntax "wholestory". If that's not valid syntax for 2007, what is the recommended way of selecting all text? Do you know any URL pointing me to the 2007 reference guide (ie all objects, properties and methods).
 

ChislePip

Registered User.
Local time
Tomorrow, 05:26
Joined
Mar 14, 2014
Messages
14
Apologies again for the delay. I did a "Quick Reply", but that didn't seem to be associated with the thread! Won't do that again.

Anyway, I tried your suggestion and prefixed the code with the "objApp". It initially fell over saying that "wholestory" property was not known, but now it's working OK!. Not sure what happened there. But my code is now working.:)

Thanks for your assistance. I'll keep looking for the definitive reference manual for 2007 though.
 

JHB

Have been here a while
Local time
Today, 19:26
Joined
Jun 17, 2012
Messages
7,732
You're welcome, good it works now!
Did you even read my signature? :)
 

Users who are viewing this thread

Top Bottom