473,406 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Albert Kallal's Access/Word Mail Merge in 2007

Hello!

First off, many many thanks to Albert who wrote the Mail Merge code for MS Access I am using. It has been working beautifully for a few years. However, my client just (without notice!) upgraded from Access 2000 to Access 2007. Now that component is failing.

The merge is building the data source file fine (text file named merge.888).
I am providing an absolute path reference to the word file being used for the mail merge. Nothing has changed and this worked perfectly in the 2000 version.

Has anyone else run into this or have any ideas? The actual code being used is below:


----
Function RidesMergeWord(strDocName As String, _
strDataDir As String, _
Optional strOutDocName As String)

' This code takes a word document that has been setup as a MERGE document.
' This merge document is opened, then mailmerge is executed. The original
' document is then closed. The result is a raw word document with no connectons
' to the merge.txt (a csv source data file).

'Parms:
' strDocName - full path name of word doc (.doc)
' strDataDir - dir (full path) where docuemnts and the merge.888 file is placed
' strOutDocName - full path name of merged document (saved).
'
' The above parms are suppled by other routines. You likey should not need to call this
' routine directly. See the sub called MergeNoPrompts.

' Albert D. Kallal (c) 2001
' kalla@msn.com
'

Dim wordApp As Object ' running instance of word
Dim WordDoc As Object ' one instance of a word doc
Dim strActiveDoc As String ' doc name (no path)
Dim lngWordDest As Long ' const for dest, 0 = new doc, 1 = printer
Dim MyPbar As New clsRidesPBar ' create a instance of our Progress bar.


MyPbar.ShowProgress
MyPbar.TextMsg = "Launching Word...please wait..."
MyPbar.Pmax = 4 ' 4 steps to inc
MyPbar.IncOne ' step 1....start!

On Error GoTo CreateWordApp
Set wordApp = GetObject(, "Word.Application")
On Error Resume Next

MyPbar.IncOne ' step 2, word is loaded.

Set WordDoc = wordApp.Documents.Open(strDocName)

MyPbar.IncOne ' step 3, doc is loaded

strActiveDoc = wordApp.ActiveDocument.Name

WordDoc.MailMerge.OpenDataSource _
Name:=strDataDir & TextMerge, _
ConfirmConversions:=False, _
ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _
WritePasswordTemplate:="", Revert:=False, Format:=0
' , Connection:="", SQLStatement:="", SQLStatement1:=""


With WordDoc.MailMerge
.Destination = 0 ' 0 = new doc
.MailAsAttachment = False
.MailAddressFieldName = ""
.MailSubject = ""
.SuppressBlankLines = True
With .datasource
.FirstRecord = 1
' .LastRecord = 1
End With
.Execute Pause:=True
End With
MyPbar.IncOne ' step 4, doc is merged
WordDoc.Close (False)

wordApp.Visible = True
wordApp.Windows(wordApp.Windows.Count).Activate
If strOutDocName <> "" Then
wordApp.ActiveDocument.SaveAs strOutDocName
End If

MyPbar.HideProgress

' AppActivate "Microsoft Word"
wordApp.Activate
wordApp.WindowState = 0 'wdWindowStateRestore

Set wordApp = Nothing
Set WordDoc = Nothing
Set MyPbar = Nothing

DoEvents

' If bolShowMerge = True Then
' WordApp.Dialogs(676).Show 'wdDialogMailMerge
' End If

Exit Function

CreateWordApp:
' this code is here to use the EXISTING copy of
' ms-access running. If getobject fails, then
' ms-word was NOT running. The below will then
' launch word
Set wordApp = CreateObject("Word.Application")
Resume Next

End Function
---


Thank You!
-Esther Lane
Jul 5 '07 #1
1 6515
FishVal
2,653 Expert 2GB
Hello!

First off, many many thanks to Albert who wrote the Mail Merge code for MS Access I am using. It has been working beautifully for a few years. However, my client just (without notice!) upgraded from Access 2000 to Access 2007. Now that component is failing.

The merge is building the data source file fine (text file named merge.888).
I am providing an absolute path reference to the word file being used for the mail merge. Nothing has changed and this worked perfectly in the 2000 version.

Has anyone else run into this or have any ideas? The actual code being used is below:


----
Function RidesMergeWord(strDocName As String, _
strDataDir As String, _
Optional strOutDocName As String)

' This code takes a word document that has been setup as a MERGE document.
' This merge document is opened, then mailmerge is executed. The original
' document is then closed. The result is a raw word document with no connectons
' to the merge.txt (a csv source data file).

'Parms:
' strDocName - full path name of word doc (.doc)
' strDataDir - dir (full path) where docuemnts and the merge.888 file is placed
' strOutDocName - full path name of merged document (saved).
'
' The above parms are suppled by other routines. You likey should not need to call this
' routine directly. See the sub called MergeNoPrompts.

' Albert D. Kallal (c) 2001
' kalla@msn.com
'

Dim wordApp As Object ' running instance of word
Dim WordDoc As Object ' one instance of a word doc
Dim strActiveDoc As String ' doc name (no path)
Dim lngWordDest As Long ' const for dest, 0 = new doc, 1 = printer
Dim MyPbar As New clsRidesPBar ' create a instance of our Progress bar.


MyPbar.ShowProgress
MyPbar.TextMsg = "Launching Word...please wait..."
MyPbar.Pmax = 4 ' 4 steps to inc
MyPbar.IncOne ' step 1....start!

On Error GoTo CreateWordApp
Set wordApp = GetObject(, "Word.Application")
On Error Resume Next

MyPbar.IncOne ' step 2, word is loaded.

Set WordDoc = wordApp.Documents.Open(strDocName)

MyPbar.IncOne ' step 3, doc is loaded

strActiveDoc = wordApp.ActiveDocument.Name

WordDoc.MailMerge.OpenDataSource _
Name:=strDataDir & TextMerge, _
ConfirmConversions:=False, _
ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _
WritePasswordTemplate:="", Revert:=False, Format:=0
' , Connection:="", SQLStatement:="", SQLStatement1:=""


With WordDoc.MailMerge
.Destination = 0 ' 0 = new doc
.MailAsAttachment = False
.MailAddressFieldName = ""
.MailSubject = ""
.SuppressBlankLines = True
With .datasource
.FirstRecord = 1
' .LastRecord = 1
End With
.Execute Pause:=True
End With
MyPbar.IncOne ' step 4, doc is merged
WordDoc.Close (False)

wordApp.Visible = True
wordApp.Windows(wordApp.Windows.Count).Activate
If strOutDocName <> "" Then
wordApp.ActiveDocument.SaveAs strOutDocName
End If

MyPbar.HideProgress

' AppActivate "Microsoft Word"
wordApp.Activate
wordApp.WindowState = 0 'wdWindowStateRestore

Set wordApp = Nothing
Set WordDoc = Nothing
Set MyPbar = Nothing

DoEvents

' If bolShowMerge = True Then
' WordApp.Dialogs(676).Show 'wdDialogMailMerge
' End If

Exit Function

CreateWordApp:
' this code is here to use the EXISTING copy of
' ms-access running. If getobject fails, then
' ms-word was NOT running. The below will then
' launch word
Set wordApp = CreateObject("Word.Application")
Resume Next

End Function
---


Thank You!
-Esther Lane
Hi!
There are too many external reasons for code to fail. The best way to solve the problem is to debug the code. I'm not sure whether you are experienced in code debugging so the following instructions are for complete newbie.
  • if code fails silently w/o error message popup then comment the line "On Error Resume Next" (put ' mark to the start of the line)
  • make the code to run as it supposed to be run
  • when a dialog with error message popups press <Debug> button, this will open VBA window where faulty code row will be highlighted
  • the highlighted code row may be not erroneous itself but calls procedure where program flow fails, so press <F8> until you get to the code row which raise the error
Having done this post where does the code actualy fail (the code row highlighted when you press <Debug> and the code row which you can't pass by pressing <F8> if not the same) and what is the error message.

Good luck.

P.S. I'm pretty sure the code fails after Word.Application is created and before it becomes visible, so you'll need to manually close Word process from Win TaskManager.

P.P.S. If you are not experienced in VBA make backup copy first.
Jul 8 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Neil Ginsberg | last post by:
I have a strange situation using Access to automate a Word mail merge. Using Access 2000 and Word 2000, the code opens Word, opens the document in Word, sets a table in the calling Access...
3
by: Strasser | last post by:
In Access2000 mass emailing worked perfectly (very powerful tool!). Doesn't work when using XP version of both Access and Outlook, even though I checked the box to ensure that I was sending the...
3
by: Andy Davis | last post by:
I have set up a mail merge document in Word 2003 which gets its data from my Access 2000 database. I want to set up a button on a form that: 1. runs the query to provide the dat for the merge...
0
by: Linda Cacina | last post by:
Hello all you fine folks, Here is some code I am using to merge data from a single record Access 2K3 table into a NEW word document based on a pre-defined Word merge template doc. All I want to...
4
by: pmhaupt2 | last post by:
I developed an Access 2003 program that will allow the user to produce a group of Word letters that merge with data records from an Access database. I created a mail merge Word document and...
8
by: Ron B | last post by:
Help!!! What am I doing wrong? I am working with Office 2003 and am trying to create a command button on an Access form that will create a mail merge in Word from an Access table. I want to...
6
by: crealesmith | last post by:
Firstly, I have no problem with mail merging to Word, VB code for that works perfectly. On one mail merge I need to merge 15 fields of data that are from 3 seperate records. The 3 records are all...
2
by: veaux | last post by:
I have single integers representing a month (i.e. 2) and need to convert this to (March 1, 2007). So "2" represents really the end of the previous month. I'm using MonthName and adding 1 in my...
1
by: kayberrie | last post by:
I want to write a VBA mail merge code. I want to link the code/macro/dohicky to a nifty little button so it makes life easy. I think I can handle the button part, the code part - not so much. I know...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.