Using macros on multiple documents - problem

G

Ganeth

I'm working on a documentation project involving 300+ word documents. Changes made in one doc need to be applied to all these docs. Anyways, to save time I wrote a macro in vb which, in short
Searches for all files in a given director
Then loops for each document
Opens i
Applies some basic changes such as replacing words or changing the margin
Saves i
Closes i

Nothing major, and everything is just peachy for 90% of these docs. However, some of them have been created using a mail merge from an Excel spreadsheet. As soon as my macro runs up against these documents, Word crashes. Reinstalling has not solved the problem. It's obviously got something to do with the fact that these documents have been produced via a mail merge, but I'm stumped. Any ideas anyone

Cheers

Ganeth
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Hi Ganeth,

A document produced by mailmerging is really no different from any other
document as the data inserted by the mergefields becomes ordinary text in
the document. Of course, that's not to say that there is not something
about that text that is causing the macro to fail. To try and get some
help, you would have to post back to the newsgroup, as continuation of this
thread, the code from the macro indicating what line of code causes the
macro to fail.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP
 
J

Jezebel

A document produced by mailmerging is really no different from any other
document as the data inserted by the mergefields becomes ordinary text in
the document.

I'm not sure that this is true, Doug. I've got a VB DLL that I use as a Word
add-in, and it too fails on mail-merged documents. I've never investigated
why because it's not relevant for my purposes, but there's definitely a
problem.
 
G

Ganeth

There's no error message generated by the crash - just the normal 'Send error report?' dialogue box (I never have, btw). However, I do have the accompanying xml file generated by the error sending program, which I can post if required, although it's 200 odd kbs. The macro code is very short (and also very quick and dirty) and I've posted it at the end of this message. I think maybe the answer here lies in stripping away whatever remnants there are from the mailmerge before processing - the macro does open the first file and perform the requested operations: it's only when it tries to save and close the document that the crash occurs. So, if there's any suggestions about how to accomplish that I think that might do the trick

Many thanks for your interest



Macro Code

Sub M_process_many_files(

' InsertFileNames Macr
' Macro created 08/07/97 by Jim Chevallie
'Edited 6.2.04 by gareth.dar

'Assign the values of Word's File Find (Search) dialogue box to 'mydialog
Set mydialog = Dialogs(wdDialogFileFind
'Assign certain info from the dialogue box to variable
'(This will pick up whatever you set when you went into File Open
's$ = mydialog.SearchPat
N$ = mydialog.Nam
'Assign the more generic Application.FileSearch to the 'Fs' variabl
Set Fs = Application.FileSearc
'Use the With instruction to work with Fs as the implied high-level qualifie
' (i.e., Fs.Lookin, Fs.MatchTestExactly, etc.
With F
'Assign the values captured from WdFileFind to the Application.FileSearch Variable
'MsgBox mydialog.SearchPat
where = InputBox("Enter the full path for the directory","Where?"
Application.FileSearch.LookIn = wher
Application.FileSearch.FileName = N
'Set other parameters for Application.FileSearc
Application.FileSearch.MatchTextExactly = Tru
Application.FileSearch.FileType = msoFileTypeAllFile
Application.FileSearch.SearchSubFolders =
'Test if the execution was successfu
If .Execute() > 0 The
'Using a 'For' loop, go through the found file
For i = 1 To .FoundFiles.Coun

Documents.Open FileName:=.FoundFiles(i), ConfirmConversions:=False,
ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="",
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="",
WritePasswordTemplate:="", Format:=wdOpenFormatAut

'Do somethin
'MsgBox "Opened" + .FoundFiles(I
do_it = "y
'do_it = InputBox("Process this document? Default = 'y', no = 'n'", "Process this

document", "y"
If do_it <> "" The
If do_it = "y" The

Application.Run MacroName:="F_page_margins

'Save and clos
ActiveDocument.Sav
ActiveDocument.Clos
End I
End I
Next
'If execution was not successful, display a messag
Els
MsgBox "There were no files found.
End I
End Wit

End Sub
 
G

Ganeth

As an addendum, I've managed to come up with the below, which basically copies the contents of the opened document into a new blank word doc, performs the operations on this new document, and saves it with a new filename in the same folder. The original document is not closed, as the problem occurs when the macro tries to save and close the original. It's not ideal, but it does show that the macro has no problems saving and closing documents which were not created via a mail marge

Ganet

Macro Code

Sub M_process_many_files(

' InsertFileNames Macr
' Macro created 08/07/97 by Jim Chevallie
'Edited 6.2.04 by gareth.dar

'Assign the values of Word's File Find (Search) dialogue box to 'mydialog
Set mydialog = Dialogs(wdDialogFileFind
'Assign certain info from the dialogue box to variable
'(This will pick up whatever you set when you went into File Open
's$ = mydialog.SearchPat
N$ = mydialog.Nam
'Assign the more generic Application.FileSearch to the 'Fs' variabl
Set Fs = Application.FileSearc
'Use the With instruction to work with Fs as the implied high-level qualifie
' (i.e., Fs.Lookin, Fs.MatchTestExactly, etc.
With F
'Assign the values captured from WdFileFind to the Application.FileSearch Variable
'MsgBox mydialog.SearchPat
where = InputBox("where"
Application.FileSearch.LookIn = wher
Application.FileSearch.FileName = N
'Set other parameters for Application.FileSearc
Application.FileSearch.MatchTextExactly = Tru
'Application.FileSearch.FileType = msoFileTypeAllFile
Application.FileSearch.FileType = msoFileTypeWordDocument
Application.FileSearch.SearchSubFolders =
'Test if the execution was successfu
If .Execute() > 0 The
'Using a 'For' loop, go through the found file
For i = 1 To .FoundFiles.Coun

Documents.Open FileName:=.FoundFiles(i), ConfirmConversions:=False,
ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="",
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="",
WritePasswordTemplate:="", Format:=wdOpenFormatAut

'Do somethin
'MsgBox "Opened" + .FoundFiles(I
do_it = "y
'do_it = InputBox("Process this document? Default = 'y', no = 'n'", "Process this document", "y"
If do_it <> "" The
If do_it = "y" The

Application.Run MacroName:="zz_open_copy_save_and_close

'Save and clos
'ActiveDocument.Sav
'ActiveDocument.Clos
End I
End I
Next
'If execution was not successful, display a messag
Els
MsgBox "There were no files found.
End I
End Wit

End Su

Sub zz_open_copy_save_and_close(

' zz_open_copy_save_and_close Macr
' Macro recorded 09.02.2004 by gareth.dar

Selection.WholeStor
Selection.Cop
newname = Replace(ActiveDocument.FullName, ".doc", "new.doc"
Documents.Add DocumentType:=wdNewBlankDocumen
Selection.Past
Application.Run MacroName:="F_page_margins
ActiveDocument.SaveAs FileName:=newname, FileFormat:=
wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:=
True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=
False, SaveNativePictureFormat:=False, SaveFormsData:=False,
SaveAsAOCELetter:=Fals
ActiveWindow.Clos
End Sub
 
T

Tsu Dho Nimh

Nothing major, and everything is just peachy for 90% of these docs. However, some of them have been created using a mail merge from an Excel spreadsheet. As soon as my macro runs up against these documents, Word crashes. Reinstalling has not solved the problem. It's obviously got something to do with the fact that these documents have been produced via a mail merge, but I'm stumped. Any ideas anyone?

Open one of these documents, and "Step Through" the macro while
you see what happens, what works, and what doesn't work.

Often it's a tiny detail like an extra blank line, a style that
isn't there, or lack of an error handler.


Tsu Dho Nimh
 
G

Ganeth

Thanks Tsu Dho Nimh (took me a moment to get that)

I'll try this like you said on the basis that no turn should be left unstoned, but I think I've pretty well narrowed the problem down to the fact that Word doesn't like closing documents that are linked to a mail merge data source. In fact, although this only struck me as relevant just now, Word will, in fact, sometimes crash when I close these mail-merge-created documents manually with a mouse click (especially if there are no other documents open).

Ganeth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top