C
CPutnam
Hi, all. I have two merges (Word 97) that both use Access queries as their
datasources (Access97). The datasource queries have different fields in
them (because one of the queries combines multiple pieces of data from the
same table into one record).
Both merges work fine and I've even set up a macro to automatically do the
merge (because this will all be distributed to a number of different users
who will need to do this multiple times). Here's the problem:
The macro does the first merge and saves the result into a new document (the
user is prompted for the document name; I'll call it DocA). Then it
activates the second document, does the merge and saves that merge result to
a new document (call this one DocB). Then it copies all the data from DocB
into DocA. Then it closes DocB and activates DocA so that the user can do
any editing he/she needs to do.
BUT when DocB is closed, I get the "Invalid Merge Field" message for every
field in DocB. If I click Cancel, I get the error message. If I click OK,
then I get the field that was suggested. Neither of these results is good.
I thought that clearing the data source from DocB would take care of the
problem, i.e. because the text that was being copied wouldn't look at all
like a merged document. But then I found out that the error message
appeared when I CLOSED DocB. So I thought that if I closed without saving,
that might take care of the problem. But it didn't.
Here's the macro that I'm using. Any ideas will be greatly appreciated!!
Thanks. Carol.
P.S. There will also be users using this macro in Word 2002 so if anyone
can see any problems that I'll be creating for myself, please let me know.
Thanks again.
----------------------------------------------------------------------------
-
Sub ProjectAbstractMerge()
'
' ProjectAbstractMerge Macro
' Macro recorded 09/09/04 by cpapishputnam
'
On Error GoTo Err_NothingtoMerge
ChangeFileOpenDirectory "C:\Download\ProjAbstractMergeFiles\"
Rem Documents.Open FileName:="ProjectAbstractTEMPLATE97.doc", _
rem ConfirmConversions:=True, ReadOnly:=False,
AddToRecentFiles:=False, _
rem PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
rem WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
rem wdOpenFormatAuto
ActiveDocument.MailMerge.ViewMailMergeFieldCodes = wdToggle
'If the document has not yet been saved
'Ask the user to provide a filename
strDocName = InputBox("Please enter a new name " & _
"for your merged document.")
ActiveDocument.SaveAs FileName:="c:\download\ProjAbstractMergeFiles\" &
strDocName
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Ecoregion:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
End With
Selection.Find.Execute
Selection.MoveUp Unit:=wdLine, Count:=2
Documents.Open FileName:="ProjectAbstract_keyContacts.doc", _
ConfirmConversions:=True, ReadOnly:=False, AddToRecentFiles:=False,
_
PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
wdOpenFormatAuto
ActiveDocument.MailMerge.ViewMailMergeFieldCodes = wdToggle
ActiveDocument.SaveAs
FileName:="c:\download\ProjAbstractMergeFiles\ProjectAbstract_KeyContactsMer
ged.doc"
Documents("C:\download\ProjAbstractMergeFiles\ProjectAbstract_KeyContactsMer
ged.doc").Activate
Set doc = ActiveDocument
doc.MailMerge.MainDocumentType = wdNotAMergeDocument
Selection.WholeStory
Selection.Copy
Documents("c:\download\ProjAbstractMergeFiles\" & strDocName &
".doc").Activate
Selection.Paste
ActiveDocument.Save
Documents("ProjectAbstract_keyContactsmerged.doc").Close
SaveChanges:=wdDoNotSaveChanges
Documents("c:\download\ProjAbstractMergeFiles\" & strDocName &
".doc").Activate
ProcessDone:
End
Err_NothingtoMerge:
Select Case Err.Number
Case 5631
Resume Next
Case Else
MsgBox "Error # " & Err.Number & ": " & Err.Description, vbOKOnly +
vbCritical, strProcessName
End Select
GoTo ProcessDone
End Sub
datasources (Access97). The datasource queries have different fields in
them (because one of the queries combines multiple pieces of data from the
same table into one record).
Both merges work fine and I've even set up a macro to automatically do the
merge (because this will all be distributed to a number of different users
who will need to do this multiple times). Here's the problem:
The macro does the first merge and saves the result into a new document (the
user is prompted for the document name; I'll call it DocA). Then it
activates the second document, does the merge and saves that merge result to
a new document (call this one DocB). Then it copies all the data from DocB
into DocA. Then it closes DocB and activates DocA so that the user can do
any editing he/she needs to do.
BUT when DocB is closed, I get the "Invalid Merge Field" message for every
field in DocB. If I click Cancel, I get the error message. If I click OK,
then I get the field that was suggested. Neither of these results is good.
I thought that clearing the data source from DocB would take care of the
problem, i.e. because the text that was being copied wouldn't look at all
like a merged document. But then I found out that the error message
appeared when I CLOSED DocB. So I thought that if I closed without saving,
that might take care of the problem. But it didn't.
Here's the macro that I'm using. Any ideas will be greatly appreciated!!
Thanks. Carol.
P.S. There will also be users using this macro in Word 2002 so if anyone
can see any problems that I'll be creating for myself, please let me know.
Thanks again.
----------------------------------------------------------------------------
-
Sub ProjectAbstractMerge()
'
' ProjectAbstractMerge Macro
' Macro recorded 09/09/04 by cpapishputnam
'
On Error GoTo Err_NothingtoMerge
ChangeFileOpenDirectory "C:\Download\ProjAbstractMergeFiles\"
Rem Documents.Open FileName:="ProjectAbstractTEMPLATE97.doc", _
rem ConfirmConversions:=True, ReadOnly:=False,
AddToRecentFiles:=False, _
rem PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
rem WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
rem wdOpenFormatAuto
ActiveDocument.MailMerge.ViewMailMergeFieldCodes = wdToggle
'If the document has not yet been saved
'Ask the user to provide a filename
strDocName = InputBox("Please enter a new name " & _
"for your merged document.")
ActiveDocument.SaveAs FileName:="c:\download\ProjAbstractMergeFiles\" &
strDocName
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Ecoregion:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
End With
Selection.Find.Execute
Selection.MoveUp Unit:=wdLine, Count:=2
Documents.Open FileName:="ProjectAbstract_keyContacts.doc", _
ConfirmConversions:=True, ReadOnly:=False, AddToRecentFiles:=False,
_
PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
wdOpenFormatAuto
ActiveDocument.MailMerge.ViewMailMergeFieldCodes = wdToggle
ActiveDocument.SaveAs
FileName:="c:\download\ProjAbstractMergeFiles\ProjectAbstract_KeyContactsMer
ged.doc"
Documents("C:\download\ProjAbstractMergeFiles\ProjectAbstract_KeyContactsMer
ged.doc").Activate
Set doc = ActiveDocument
doc.MailMerge.MainDocumentType = wdNotAMergeDocument
Selection.WholeStory
Selection.Copy
Documents("c:\download\ProjAbstractMergeFiles\" & strDocName &
".doc").Activate
Selection.Paste
ActiveDocument.Save
Documents("ProjectAbstract_keyContactsmerged.doc").Close
SaveChanges:=wdDoNotSaveChanges
Documents("c:\download\ProjAbstractMergeFiles\" & strDocName &
".doc").Activate
ProcessDone:
End
Err_NothingtoMerge:
Select Case Err.Number
Case 5631
Resume Next
Case Else
MsgBox "Error # " & Err.Number & ": " & Err.Description, vbOKOnly +
vbCritical, strProcessName
End Select
GoTo ProcessDone
End Sub