From: Robert Bonds on
Need a bit of help, please. Am trying to use VBA (in Microsoft Access) to run
the simplest kind of Word mail merge, but for the life of me I can't get the
syntax right. The merge template in Word is already set up with a fixed
tab-delimited text file as its source, so all that's needed is to run the
merge... but it won't go. Here's the procedure, along with the routine for
starting Word in case the problem lies there:

--------

Sub GenerateLetters()

Call GetWord
DoCmd.SetWarnings False
wd.Documents.Open "D:\myTemplate.doc"
DoCmd.SetWarnings True

With wd.ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With

ActiveDocument.SaveAs "D:\myNewDocument.doc"
wd.Quit

--------

Sub GetWord()
On Error GoTo Error_GetWord

Set wd = GetObject(, "Word.Application")
Exit Sub

Error_GetWord:

If Err.Number = 429 Then
Set wd = CreateObject("Word.Application")
Resume Next
Else
MsgBox "Error " & Err.Number & " - " & Err.Description
End If

End Sub

--------

The procedure hangs at the line ".Destination = wdSendToNewDocument", which
is no doubt why the code following that point seems to merely save the
template to the new location rather than actually merging to a new document
at the new location. But I can't figure out how/where to have the code
correctly specify the name and location of the new document to be created by
the merge.

Any help will be greatly appreciated. Thanks.

Robert Bonds


From: Peter Jamieson on
Regardless of the error message/code you are seeing, in this case you
probably need to make the registry fix described in

http://support.microsoft.com/kb/825765

Peter Jamieson

http://tips.pjmsn.me.uk

On 25/03/2010 23:30, Robert Bonds wrote:
> Need a bit of help, please. Am trying to use VBA (in Microsoft Access) to run
> the simplest kind of Word mail merge, but for the life of me I can't get the
> syntax right. The merge template in Word is already set up with a fixed
> tab-delimited text file as its source, so all that's needed is to run the
> merge... but it won't go. Here's the procedure, along with the routine for
> starting Word in case the problem lies there:
>
> --------
>
> Sub GenerateLetters()
>
> Call GetWord
> DoCmd.SetWarnings False
> wd.Documents.Open "D:\myTemplate.doc"
> DoCmd.SetWarnings True
>
> With wd.ActiveDocument.MailMerge
> .Destination = wdSendToNewDocument
> .SuppressBlankLines = True
> With .DataSource
> .FirstRecord = wdDefaultFirstRecord
> .LastRecord = wdDefaultLastRecord
> End With
> .Execute Pause:=False
> End With
>
> ActiveDocument.SaveAs "D:\myNewDocument.doc"
> wd.Quit
>
> --------
>
> Sub GetWord()
> On Error GoTo Error_GetWord
>
> Set wd = GetObject(, "Word.Application")
> Exit Sub
>
> Error_GetWord:
>
> If Err.Number = 429 Then
> Set wd = CreateObject("Word.Application")
> Resume Next
> Else
> MsgBox "Error "& Err.Number& " - "& Err.Description
> End If
>
> End Sub
>
> --------
>
> The procedure hangs at the line ".Destination = wdSendToNewDocument", which
> is no doubt why the code following that point seems to merely save the
> template to the new location rather than actually merging to a new document
> at the new location. But I can't figure out how/where to have the code
> correctly specify the name and location of the new document to be created by
> the merge.
>
> Any help will be greatly appreciated. Thanks.
>
> Robert Bonds
>
>
From: Doug Robbins - Word MVP on
How is wd declared?

I would also declare wdDoc and use

Set wdDoc = wd.Documents.Open("D:\myTemplate.doc")

then use wdDoc in place of wd.ActiveDocument

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

"Robert Bonds" <RobertBonds(a)discussions.microsoft.com> wrote in message
news:643D2396-BFC2-4C61-B421-F99A434BBA99(a)microsoft.com...
> Need a bit of help, please. Am trying to use VBA (in Microsoft Access) to
> run
> the simplest kind of Word mail merge, but for the life of me I can't get
> the
> syntax right. The merge template in Word is already set up with a fixed
> tab-delimited text file as its source, so all that's needed is to run the
> merge... but it won't go. Here's the procedure, along with the routine for
> starting Word in case the problem lies there:
>
> --------
>
> Sub GenerateLetters()
>
> Call GetWord
> DoCmd.SetWarnings False
> wd.Documents.Open "D:\myTemplate.doc"
> DoCmd.SetWarnings True
>
> With wd.ActiveDocument.MailMerge
> .Destination = wdSendToNewDocument
> .SuppressBlankLines = True
> With .DataSource
> .FirstRecord = wdDefaultFirstRecord
> .LastRecord = wdDefaultLastRecord
> End With
> .Execute Pause:=False
> End With
>
> ActiveDocument.SaveAs "D:\myNewDocument.doc"
> wd.Quit
>
> --------
>
> Sub GetWord()
> On Error GoTo Error_GetWord
>
> Set wd = GetObject(, "Word.Application")
> Exit Sub
>
> Error_GetWord:
>
> If Err.Number = 429 Then
> Set wd = CreateObject("Word.Application")
> Resume Next
> Else
> MsgBox "Error " & Err.Number & " - " & Err.Description
> End If
>
> End Sub
>
> --------
>
> The procedure hangs at the line ".Destination = wdSendToNewDocument",
> which
> is no doubt why the code following that point seems to merely save the
> template to the new location rather than actually merging to a new
> document
> at the new location. But I can't figure out how/where to have the code
> correctly specify the name and location of the new document to be created
> by
> the merge.
>
> Any help will be greatly appreciated. Thanks.
>
> Robert Bonds
>
>
From: Robert Bonds on
Doug, Peter,

Thanks VERY much for your help. Looks like I needed to implement the
suggestions from both of you to get this to work. And now it does. Thanks
again.

Best,
Robert Bonds

-----------------------

"Doug Robbins - Word MVP" wrote:

> How is wd declared?
>
> I would also declare wdDoc and use
>
> Set wdDoc = wd.Documents.Open("D:\myTemplate.doc")
>
> then use wdDoc in place of wd.ActiveDocument
>
> --
> Hope this helps.
>
> Please reply to the newsgroup unless you wish to avail yourself of my
> services on a paid consulting basis.
>
> Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
>
> "Robert Bonds" <RobertBonds(a)discussions.microsoft.com> wrote in message
> news:643D2396-BFC2-4C61-B421-F99A434BBA99(a)microsoft.com...
> > Need a bit of help, please. Am trying to use VBA (in Microsoft Access) to
> > run
> > the simplest kind of Word mail merge, but for the life of me I can't get
> > the
> > syntax right. The merge template in Word is already set up with a fixed
> > tab-delimited text file as its source, so all that's needed is to run the
> > merge... but it won't go. Here's the procedure, along with the routine for
> > starting Word in case the problem lies there:
> >
> > --------
> >
> > Sub GenerateLetters()
> >
> > Call GetWord
> > DoCmd.SetWarnings False
> > wd.Documents.Open "D:\myTemplate.doc"
> > DoCmd.SetWarnings True
> >
> > With wd.ActiveDocument.MailMerge
> > .Destination = wdSendToNewDocument
> > .SuppressBlankLines = True
> > With .DataSource
> > .FirstRecord = wdDefaultFirstRecord
> > .LastRecord = wdDefaultLastRecord
> > End With
> > .Execute Pause:=False
> > End With
> >
> > ActiveDocument.SaveAs "D:\myNewDocument.doc"
> > wd.Quit
> >
> > --------
> >
> > Sub GetWord()
> > On Error GoTo Error_GetWord
> >
> > Set wd = GetObject(, "Word.Application")
> > Exit Sub
> >
> > Error_GetWord:
> >
> > If Err.Number = 429 Then
> > Set wd = CreateObject("Word.Application")
> > Resume Next
> > Else
> > MsgBox "Error " & Err.Number & " - " & Err.Description
> > End If
> >
> > End Sub
> >
> > --------
> >
> > The procedure hangs at the line ".Destination = wdSendToNewDocument",
> > which
> > is no doubt why the code following that point seems to merely save the
> > template to the new location rather than actually merging to a new
> > document
> > at the new location. But I can't figure out how/where to have the code
> > correctly specify the name and location of the new document to be created
> > by
> > the merge.
> >
> > Any help will be greatly appreciated. Thanks.
> >
> > Robert Bonds
> >
> >