Forum Discussion

maneeshmassey's avatar
maneeshmassey
Copper Contributor
May 17, 2022

Open Microsoft Word Event Certificate from within Access

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?

 

 

 

 

Resources