Hey Guys,
I found this code somewhere on the internet, maybe even from this forum but I can't remember, basically, the merge works great but the issue im having is, when the merge is open and, lets say the user closes the word document, but then needs it open again, so they re-click the button on the form that activates the merge again, it gives an error, below is the code:
The error I am getting is:
"Run Time Error '91':
Object Variable or With Block Variable Not Set"
And debug is highlighting this code:
Set oMainDoc = oApp.Documents.Open("U:\test")
I am using access 2007, any help would be appreciated as I am a novice with vba.
Thank you!
I found this code somewhere on the internet, maybe even from this forum but I can't remember, basically, the merge works great but the issue im having is, when the merge is open and, lets say the user closes the word document, but then needs it open again, so they re-click the button on the form that activates the merge again, it gives an error, below is the code:
Code:
Option Compare Database
Dim WithEvents oApp As Word.Application
Private Sub Command0_Click()
Dim oMainDoc As Word.Document
Dim oSel As Word.Selection
Dim sDBPath As String
Set oMainDoc = oApp.Documents.Open("U:\test")
oApp.Visible = True
With oMainDoc.MailMerge
.MainDocumentType = wdFormLetters
sDBPath = "U:\Letter Database.mdb"
.OpenDataSource Name:=sDBPath, _
SQLStatement:="SELECT * FROM [tblcompanies]"
End With
With oMainDoc
.MailMerge.Destination = wdSendToNewDocument
.MailMerge.Execute
End With
oApp.Activate
oApp.Documents.Parent.Visible = True
oApp.Application.WindowState = 1
oApp.ActiveWindow.WindowState = 1
End Sub
Private Sub Form_Load()
Set oApp = CreateObject("Word.Application")
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set oApp = Nothing
End Sub
The error I am getting is:
"Run Time Error '91':
Object Variable or With Block Variable Not Set"
And debug is highlighting this code:
Set oMainDoc = oApp.Documents.Open("U:\test")
I am using access 2007, any help would be appreciated as I am a novice with vba.
Thank you!