Problem with VBA and Access 2010

mgjdun

Registered User.
Local time
Today, 12:19
Joined
Nov 28, 2011
Messages
10
Hi,

After being forced to use Office 2010 (I was using Office 2003) I'm encountering problems with my database. The function below was working fine in Access 2003. However, now I'm forced to switch to Office 2010, and the function isn't working anymore.

The function should mailmerge data from a query (strQueryName) to a word document (strFileName) and print the document.

Can anybody help me?

Best regards,
Mathijs


Code:
Public Function MergeQuest(strFileName As String, strQueryName As String, strDBpath As String)

On Error GoTo ErrHandling

Dim objDoc As Word.Document
Dim objWord As Word.Application
Dim blnCreated As Boolean

On Error Resume Next
Set objWord = GetObject("Word.Application")
If Err Then
    Set objWord = CreateObject("Word.Application")
    blnCreated = True
End If

On Error GoTo ErrHandling

Set objDoc = objWord.Documents.Open("" & strFileName & "")

'Make Word Visible
objWord.Visible = True

'Execute the MailMerge
With objDoc.mailmerge
    'Set Merge Data Source
    objDoc.mailmerge.OpenDataSource Name:=strDBpath, _
    LinktoSource:=True, _
    Connection:="QUERY " & strQueryName, _
    SQLStatement:="SELECT * FROM " & strQueryName
    .Destination = wdSendToNewDocument
    .Execute
    objWord.ActiveDocument.PrintOut False
    objWord.ActiveDocument.Close wdDoNotSaveChanges
End With

'probleem oplossen: automate multiple instances of Microsoft Word simultaneously
objWord.NormalTemplate.Saved = True
'Close The form files and the merged document
objDoc.Close wdDoNotSaveChanges

If blnCreated Then
objWord.Quit
End If

Set objDoc = Nothing
Set objWord = Nothing

Exit Function

ErrHandling:
MsgBox "Whoops" 'Better error handling of course
End Function
 

Users who are viewing this thread

Back
Top Bottom