Open Microsoft Word Event Certificate from within Access

Copper Contributor

Hi!,

I am trying to open a marriage certificate that I designed in MS-Word from within Access. I wish to use a table either containing names of husbands and wives, or allow the user to either type the name of husbands and their spouse's into a Textbox control and a date field to pick the date of their wedding took place. After having done this, the user pushes a button on the form and the marriage certificate opens up in word with both of their names written beautifully with italic font that I already created. 

Is this scenario possible in Access? Is this doable ?? 

 

I even tried this myself by placing bookmarks after the names placeholders for both the husband and wife, but this did not work because when I ran the sub routine, it created certificates for all the record sets that I have in my table. This is not what I want. I want the certificate to be generated for only the persons whose names are typed into the Text boxes. 

 

I am trying to design a marriage certificate issuing software. 

Can someone please help??

 

Here is the code I am using:

 

'------------------Code Start-----------------------
Sub OpenWordDoc(strDocName As String)
    Dim objApp As Object

    'Opens the document

    Set objApp = CreateObject("Word.Application")
    objApp.Visible = True
    objApp.Documents.Open strDocName
End Sub
'------------------Code End-------------------------

Then from the On Click event of the command button on the form (named cmdOpenWordDoc in this example) I pasted the following (where Me.FilePath is the name of the control on the form storing the path to the associated file):

'------------------Code Start-----------------------
Private Sub cmdOpenWordDoc_Click()
'Check to see that there is a document file path associated with the record
    If IsNull(Me.FilePath) Or Me.FilePath = "" Then
        MsgBox "Please Ensure There Is A File Path Associated " & _
        "For This Document", _
               vbInformation, "Action Cancelled"
        Exit Sub
    Else
        'Check that the document specified in the file path is actually there
        If (Dir(Me.FilePath) = "") Then
            MsgBox "Document Does Not Exist In The Specified Location", _
                   vbExclamation, "Action Cancelled"
            Exit Sub
        Else
            'Opens document in Word
            Call OpenWordDoc(Me.FilePath)
        End If
    End If
End Sub
'------------------Code End-----------------------

 

 

Here's a separate code I used for the bookmarks example.

 

Public Sub ExportNamesToWord()

Dim wApp As Word.Application

Dim wDoc As Word.Document

Dim rs As DAO.RecordSet

 

Set wApp = New Word.Application

Set wDoc = wApp.Documents.Open ("C:\Users\Documents\MC.docx")     

 

'MC (Marriage Certificate is the name of the word document)

 

Set rs = CurrentDb.OpenRecordSet ("tblNames")

 

If Not rs.EOF Then rs.MoveFirst

 

Do Until rs.EOF

      wDoc.Bookmarks("FirstName").Range.Text = Nz(rs!FirstName, "")

      wDoc.Bookmarks("LastName").Range.Text = Nz(rs!LastName, "")

      wDoc.SaveAs "C:\Users\Documents\" & rs!ID & "_MC.docx"

rs.MoveNext

Loop

 

End Sub

 

I used the above codes separately and still I was not even close to achieving the desired result.

What am I doing wrong here?

Can someone please guide?

 

 

 

 

2 Replies

@maneeshmassey 

 

You need to explore the use of Mail Merge in Word to perform this task. One of the most frequently recommended approaches is one created by Albert Kallal. It's a link partway down the page. Although it was written some time ago, it was updated not too long ago, it is still one of the more frequently used methods to merge records from Access into Word documents. 

 

 

@George Hepworth 

Thank you for the quick reply. I did not expect this to work in MS-Access, but I will surely have a look at the resource you pointed to and let you know.

Thank you once again.