Reply
 
Thread Tools Display Modes
  #1   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

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.
  #2   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Peter Jamieson Peter Jamieson is offline
external usenet poster
 
Posts: 4,582
Default Mail Merge Automation Code problem

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.

  #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.

Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Failing Mail Merge Automation Frank[_4_] Mailmerge 1 June 18th 08 08:59 PM
Mail Merge Automation: Call rejeted by Callee KCT Microsoft Word Help 0 December 19th 07 10:39 AM
VBA code - automation of creating labels [email protected] Microsoft Word Help 2 November 20th 07 03:46 PM
when I use mail merge the merge drops the '0" of zip code Michbostn Mailmerge 1 June 16th 07 11:10 PM
Header / Footer mail merge issue with custom built word automation solution. eJimmi Mailmerge 4 July 18th 06 02:48 PM


All times are GMT +1. The time now is 11:56 PM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"