Control Word from Access (1 Viewer)

JohnPapa

Registered User.
Local time
Today, 03:55
Joined
Aug 15, 2010
Messages
954
At present I use A03 and Word03 and I would like to be able to fill in predetermined fields in Word from my A03 db. I do not want to use mail merge.

I want to use Late Binding for compatibility across versions of Word. I tried inserting bookmarks in the Word document, but I do not seem to be able to reference the bookmark from A03 though VBA. The code below opens an existing Word file "abc.doc" and writes text.

BTW is bookmarks the way to go? If yes, how do I refernce a bookmark say "txtBookmark" and assign to it the value say "George".

Thanks

Code:
Dim objWord As Object
Dim doc As Object
Dim bolOpenedWord As Boolean
On Error Resume Next
Dim blnDum As Boolean
blnDum = fIsAppRunning("word", True)
If blnDum = True Then
  Set objWord = GetObject(, "Word.Application")
Else
  Set objWord = CreateObject("Word.Application")
End If
    objWord.Documents.Open "C:\junk\abc.doc"
    bolOpenedWord = True
objWord.Visible = True
On Error GoTo 0
 objWord.Activate
Set doc = objWord.Documents.Add
'Insert Text
objWord.Selection.TypeText "This is a test" & vbCrLf
'Insert Paragraph
objWord.Selection.TypeParagraph
'Insert More text and Paragraphs
objWord.Selection.TypeText "More text inserted"
objWord.Activate
On Error Resume Next
doc.Save
On Error GoTo 0
doc.Close False
Set doc = Nothing
If bolOpenedWord = True Then
    objWord.Quit
End If
Set objWord = Nothing
 

DavidAtWork

Registered User.
Local time
Today, 01:55
Joined
Oct 25, 2011
Messages
699
Yes using bookmarks is possible. Your line of code which reads:
Set doc = objWord.Documents.Add
should be:
Set doc = objWord.Documents.Open("C:\junk\abc.doc")
then use this method to reference the bookmarks
doc.FormFields("txtBookmark").Result = "This is a test"

David
 

JohnPapa

Registered User.
Local time
Today, 03:55
Joined
Aug 15, 2010
Messages
954
Thanks David,
It worked fine. Can you think of a better way to fill in values in Word from Access than using bookmarks?
I will also look into the Object model of Word.
John
 

DavidAtWork

Registered User.
Local time
Today, 01:55
Joined
Oct 25, 2011
Messages
699
Apart from inserting blocks of hard coded text as you've already managed above, I don't see any other way.
The FormFields method works well as if trying to emulate a mail merge approach

David
 

JohnPapa

Registered User.
Local time
Today, 03:55
Joined
Aug 15, 2010
Messages
954
Many thanks again. I just wanted to confirm what you said in view of my embarking on a substantial project involving such an action.
John
 

JohnPapa

Registered User.
Local time
Today, 03:55
Joined
Aug 15, 2010
Messages
954
Thanks Michael,

A couple of issues that may be of interest:
1) It may be a good idea to check whether Word is running (another Word document) and if it is, then to alert the user and maybe do not take any further action unless other Word file is closed.
2) Your idea of using classes (which I have never used) may be interesting in the following manner. If all bookmarks have the same name (excluding the prefix) as the table field name then you could pass as parameters only the Word doc name and the Access table which contains the field values. As an example, assume you have 3 bookmarks, call then booOne, booTwo and booThree in a Word document call Sample.doc. If these values are stored in table tblExample in fields txtOne, txtTwo and txtThree then we can pass as parameters "Sample.doc" and "tblExample" to a sub which will loop through all bookmarks (I am assuming this is possible) and for each bookmark will pick the appropriate value from the db table. We'll see how this works out in practice.

John
 

JohnPapa

Registered User.
Local time
Today, 03:55
Joined
Aug 15, 2010
Messages
954
I found this in msdn regarding looping through the bookmarks of a specific Word document. When I try to compile it give me a Compile error "User-defined type not defined" for "Dim bkMark As Bookmark". Do I need to include a Word library in my Access application and how is this done? In my code above "MsgBox doc.Bookmarks.Count" works ok and gives the number of bookmarks -1.

Code:
Sub LoopThroughBookmarks()    
Dim bkMark As Bookmark    
Dim strMarks() As String    
Dim intCount As Integer    
If ActiveDocument.Bookmarks.Count > 0 Then        
  ReDim strMarks(ActiveDocument.Bookmarks.Count - 1)        
  intCount = 0        
  For Each bkMark In ActiveDocument.Bookmarks            
    strMarks(intCount) = bkMark.Name            
    intCount = intCount + 1        
  Next bkMark    
End If
End Sub
 

mdlueck

Sr. Application Developer
Local time
Yesterday, 20:55
Joined
Jun 23, 2011
Messages
2,631
1) It may be a good idea to check whether Word is running (another Word document) and if it is, then to alert the user and maybe do not take any further action unless other Word file is closed.

I have had other instances of Word open working on other documents, no collisions occurr. My Access code is building a brand new document, so I am guaranteed the file will not already be open in another instance of Word.

2) Your idea of using classes (which I have never used) may be interesting in the following manner.

Jump in, the water's great! :cool:

When I try to compile it give me a Compile error "User-defined type not defined" for "Dim bkMark As Bookmark". Do I need to include a Word library in my Access application and how is this done?

That will be caused by using Late Binding coding style. You can not refer to any Types or Constants which binding the Word binding to your project would provide.

Example code from driving Excel with Late Bindings:

Code:
  'Make Excel Instance Minimized
  objExcelApp.WindowState = 2 '= wdWindowStateMinimize
I found out that the value of the constant wdWindowStateMinimize was 2, so substituted / commented. My guess is you have run into the same sort of thing. Objects must be Dim'ed as Object (generic) and no use of Constants, then the code should work.
 

JohnPapa

Registered User.
Local time
Today, 03:55
Joined
Aug 15, 2010
Messages
954
Thanks Michael,
Is there a way to loop through all Bookmarks in a Word doc when using Late binding?
John
 

JohnPapa

Registered User.
Local time
Today, 03:55
Joined
Aug 15, 2010
Messages
954
Micahel, with your help I figured it out. THe following will loop through all Bookmarks.

Code:
Dim bkMark As Object

For Each bkMark In doc.bookmarks

  MsgBox bkMark.Name
Next bkMark

 

danit58

Registered User.
Local time
Today, 01:55
Joined
Aug 16, 2005
Messages
24
Hi all,

it is possible to adapt the code to drive Word model (dot) with more than one record, creating a single document consisting of one page per record?

best regards, Dani
 

Users who are viewing this thread

Top Bottom