Help with Form Fields from access to Word

lovett10

Registered User.
Local time
Today, 07:27
Joined
Dec 1, 2011
Messages
150
Below is my code, which works perfectly...
Code:
Private Sub WorksReport_Click()
    Dim LWordDoc As String
     Dim oApp As Object
    Dim MWordDoc As String
 
    MWordDoc = "Z:\WorksReport\V" & Me.VisitNumber & ".docx"
    'Path to the word document
     LWordDoc = "Z:\WorksReport\V" & Me.VisitNumber & ".docx"
Debug.Print LWordDoc
    If Dir(LWordDoc) = "" Then
        LWordDoc = "Z:\WorksReport\VXXXXX.docx"
 
         'Create an instance of MS Word
         Set oApp = CreateObject(Class:="Word.Application")
         oApp.Visible = True
        'Open the Document
         oApp.Documents.Open FileName:=LWordDoc
    ActiveDocument.SaveAs FileName:=MWordDoc
        
 
    Else
             'Create an instance of MS Word
         Set oApp = CreateObject(Class:="Word.Application")
         oApp.Visible = True
        'Open the Document
         oApp.Documents.Open FileName:=LWordDoc
 
    End If
End Sub

The only other thing i would like to add is
Code:
.FormFields("VisitNumber").Result = Me!VisitNumber

To the active document. Please can u help me with how to implement this into my code.
Thanks
 
Last edited:
Here's how I would do it.

Code:
Dim appWord As Word.Application, currentDocs As Word.Documents, doc As Word.Document
 
On Error GoTo ErrorHandler

Set appWord = GetObject(Class:="Word.Application")
Set currentDocs = appWord.Documents
Set doc = currentDocs.Open(FileLocation)
With doc
   .Bookmarks("VisitNumber").Range.Text = Me!VisitNumber
End With
 
ErrorHandler:
   If Err = 429 Then
      'Word is not running; open Word with CreateObject
      Set appWord = CreateObject("Word.Application")
      Resume Next
   Else
      MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
   End If
   
Set appWord = Nothing
Set doc = Nothing
Set currentDocs = Nothing

Hope this helps a little
 

Users who are viewing this thread

Back
Top Bottom