Error when creating a masterdocument with VBA

P

periodic

I have read that master documents are not good. But for now I want to give it
try but still be on the safe side. By writing some VBA code to take a number
of files make a subfolder, copy all relevant files there and then create a
master document there. So it does not matter if the master document corrupts
some files since they are just copies of the original anyway. Plus I can make
different editions of the book I am working on putting together. The editions
must have some different formating and so on. Anyway I have gotten folder and
file handling to work fine for me. Although when I try to run the following
code.

Sub makeEdition(currPath As String, dirName As String, ByRef filesToCopy As
Collection)
makeDirAndFillIn currPath:=currPath, dirName:=dirName,
filesToCopy:=filesToCopy
ChDir path:=currPath
Documents.Add Template:=currPath & "\Chapter-template.dot"
Dim masterName As String
masterName = currPath & "\" & dirName & "\master.doc"
ActiveDocument.SaveAs fileName:=masterName,
FileFormat:=wdFormatDocument, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False,
SaveFormsData:=False, _
SaveAsAOCELetter:=False

Dim masterDoc As Document
Dim masterDocRange As Range

Set masterDoc = Documents("master.doc")

Set masterDocRange = masterDoc.Range
masterDocRange.Collapse direction:=wdCollapseStart


ActiveWindow.ActivePane.View.Type = wdMasterView

Dim subDocName As String
Dim subDocPath As String

subDocPath = currPath & "\" & dirName & "\"

ChangeFileOpenDirectory path:=subDocPath
For i = 1 To filesToCopy.count
subDocName = filesToCopy(i)
masterDocRange.Subdocuments.AddFromFile name:=subDocName,
ConfirmConversions:=False, ReadOnly:=False, Revert:=True
Next i

masterDoc.Close

End Sub

It it the last for loop that is the problem. As soon as I try to add a sub
document. Word hangs and stops responding and I have to kill it from the task
manager.

Does anyone have any ideas why this does not work or this a bug in word?

I think I am going to use the INCLUDETEXT field instead. But I can't help
wondering what is wrong with my code.
 
Z

zkid

In the problem for loop, I think you need to insert msgbox filesToCopy(i) to
determine the collection's contents. Insert it just after

For i = 1 To filesToCopy.count
subDocName = filesToCopy(i)

I generally use arrays, not collections. I think you need to see exactly
what is contained in the filesToCopy collection. Maybe it's not a valid path?
 
P

periodic

I have verified that the paths are OK. The strange thing is I tested to
create a macro inserting four files into a master document. It worked fine
when I did the recording but when I tried to run the recorded macro the same
problem arose. Word just stopped responding and I had to kill it.

I just thought collections was a good thing since I don't know how large it
will be and I can index my entries with keys. Which is good (or at least I
hope MS has some good implementation of associative collections).
 
Z

zkid

I'll try to take a look at it later tonight when I have more time. In the
meantime, here's a stupid question: Are all of the documents in the copy
directory indeed Word docs? Is there maybe an Excel spreadsheet or anything
else that would not retrieve properly into Word?
 
P

periodic

Yes they are ;) And its not a big deal so you do not have to look into it if
you have better things to do. I have already solved it using INCLUDETEXT
fields instead and considering giving that up also and just keeping the docs
separate using some macros to manage the collection.

The strangest part of it is that even a recorded macro makes my word freeze.
Just wonder if this is a bug in Word2003
 
Z

zkid

I just re-read you original question, and you stated the documents have
different formatting. I use INCLUDETEXT quite a bit, and I believe it is
definitely the best way to go here to keep you out of trouble. Subdocuments
REALLY don't like different formatting.

If you would like to continue along the master document route, it would be
extremely helpful if you supplied all of the code so I don't have to spend
too much time figuring out, and replicating, the process.

Thanks.
 
P

periodic

What I meant is that the different editions of the book will have different
formating. But all subdocuments strictly adheres to one template so thats not
a big trouble.

Hmm I just tried this on another computer and then it hangs even if you try
to record a macro inserting one file. It said scanning for viruses. I suspect
that the virus scanner is interfering in some way. I have to check this more
after weekend.

I do not think any of my code will help you since it might be computer
related problems
 

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