View Single Post
  #3   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Peter Hibbs[_2_] Peter Hibbs[_2_] is offline
external usenet poster
 
Posts: 19
Default Mail Merge Automation Code problem

WOW, that works perfectly. Thanks very much Peter. I will study it now
to see exactly how it works.

Thanks again.

Peter Hibbs.

On Tue, 25 Aug 2009 19:24:05 +0100, Peter Jamieson
wrote:

You will need to check this for yourself, but try something more like...

Dim ObjMMMD As Word.Document
Set ObjWord = New Word.Application
ObjWord.Visible = True
Set ObjMMMD = ObjWord.Documents.Open(Filename:=txtFilename)
With ObjWord.ActiveDocument.MailMerge

'Check if doc is valid mail merge doc
If .State = wdMainAndDataSource _
Or .State = wdMainAndSourceAndHeader Then
vProgress = .DataSource.TableName

'Check if doc is linked to correct table
If InStr(1, vProgress, "tblMailMerge") = 0 Then
' maybe close your doc before quitting?
ObjMMMD.Close SaveChanges:=False
Set ObjMMMD = Nothing
ObjWord.Quit
Set ObjWord = Nothing
Beep
MsgBox "ERROR. The Mail Merge doc is invalid",
vbCritical + vbOKOnly, "Invalid Source Data"
Exit Sub
End If
.SuppressBlankLines = True
.Destination = wdSendToNewDocument
.Execute
' The ActiveDocument is now the newly created document
ObjWord.ActiveDocument.Saved = True
' but we can reference the MMMD
ObjMMMD.Close SaveChanges:=False
Set ObjMMMD = Nothing
Else
MsgBox "ERROR. The doc is not a MM Master ", vbCritical +
vbOKOnly, "Invalid File"
' maybe close your doc before quitting?
ObjMMMD.Close SaveChanges:=False
Set ObjMMMD = Nothing
ObjWord.Quit
End If
End With
Set ObjWord = Nothing


then consider modifying where you choose to make ObjWord visible and/or
where you .Activate ObjWord and/or ActivateDocument

Peter Jamieson

http://tips.pjmsn.me.uk

Peter Hibbs wrote:
Using Access 2003 and Word 2003.

I am creating a mail merge document from within an Access database
using the code below.

Set ObjWord = New Word.Application
ObjWord.Visible = True
ObjWord.Documents.Open Filename:=txtFilename
With ObjWord.ActiveDocument.MailMerge

'Check if doc is valid mail merge doc
If .State = wdMainAndDataSource _
Or .State = wdMainAndSourceAndHeader Then
vProgress = .DataSource.TableName

'Check if doc is linked to correct table
If InStr(1, vProgress, "tblMailMerge") = 0 Then
ObjWord.Quit
Set ObjWord = Nothing
Beep
MsgBox "ERROR. The Mail Merge doc is invalid",
vbCritical + vbOKOnly, "Invalid Source Data"
Exit Sub
End If
.SuppressBlankLines = True
.Destination = wdSendToNewDocument
.Execute
ObjWord.ActiveDocument.Saved = True
Else
MsgBox "ERROR. The doc is not a MM Master ", vbCritical +
vbOKOnly, "Invalid File"
ObjWord.Quit
End If
End With
Set ObjWord = Nothing

This all works fine. What actualy happens is this :-
When the code runs the mail merge master document is briefly displayed
on screen and then the mail merge document itself is displayed. The
user prints the document as normal and then closes the document.
Because I have set the Saved flag in code, the document closes as
normal (unless the user actually makes a change to the document before
printing it, in which case he needs to save it again before exiting,
mail merge files are always saved to disk for reference purposes).

When the mail merge document is closed, the mail merge master document
is then displayed again, the user has to close this one as well and
when he does, the 'Do you want to save the changes to .... etc' dialog
is displayed, he clicks No and then Word closes completely.

What I would ideally like is for the mail merge master document NOT to
be displayed at all (the user does not really need to see it), is this
possible? Failing that, I would like the Saved flag to be set on the
master document so that the user is not asked if he wants to save the
file each time he closes it (he hasn't changed it anyway so I don't
know why this happens).

Is there any way to achieve either of those things in code. Note that
I do not want to add any code to the document files, the users will
not be able to add VBA code to Word documents.

Peter Hibbs.