Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / July 2007

Tip: Looking for answers? Try searching our database.

Error when creating a masterdocument with VBA

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
periodic - 25 Jul 2007 20:10 GMT
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.
zkid - 26 Jul 2007 16:42 GMT
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?

> 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
[quoted text clipped - 55 lines]
> I think I am going to use the INCLUDETEXT field instead. But I can't help
> wondering what is wrong with my code.
periodic - 26 Jul 2007 16:56 GMT
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).

> In the problem for loop, I think you need to insert msgbox filesToCopy(i) to
> determine the collection's contents.  Insert it just after
[quoted text clipped - 4 lines]
> 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?
zkid - 26 Jul 2007 18:12 GMT
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?

> 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
[quoted text clipped - 13 lines]
> > 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?
periodic - 26 Jul 2007 21:08 GMT
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

> 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
[quoted text clipped - 18 lines]
> > > 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?
zkid - 27 Jul 2007 00:54 GMT
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.

> 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
[quoted text clipped - 26 lines]
> > > > 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?
periodic - 27 Jul 2007 02:06 GMT
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

> 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
[quoted text clipped - 37 lines]
> > > > > 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?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.