PDA

View Full Version : Solved: Create a blank scratch document from someone else's .doc file?



DaVinney
04-13-2011, 07:58 AM
Using a Word 2007 macro, I'm creating a new document by inserting a list of documents by others. It works fine, except the working version starts with a blank new doc that can alter the appearance of things like fonts, spacing, etc. that the user no doubt wishes to maintain in the new compiled document.

#1 - One solution that works okay, but klunky:
1. Make a copy of one of the user's docs (saves to a temporary file name, leaving the original untouched).
2. Delete all content:

Dim oStory As Range
For Each oStory In ActiveDocument.StoryRanges
oStory.Delete
Next

3. Compile new document.
4. Have user SaveAs a new file name using: Dialogs(wdDialogFileSaveAs).Show
5. Kill the temporary copied file created in Step 1 (still trying to get the kill to work, by the way).

#2 - Another method I would prefer if someone could advise me how to do it:
1. Create a new blank doc using one of the user's .doc or .docx files as a "template".
2. Compile new document.
3. Save new doc, done.

#3 - Some other way?

Is method #2 easily done considering the user's file is not a .dot file?

Please advise.

Frosty
04-13-2011, 09:16 AM
#2 -- yes, you can use existing documents (.doc and .docx) as the "template" in the Documents.Add method, however... just like a template, any content in the document will show up in the new document.

So your steps to create a temp doc in #1 are not necessary, although it is good to learn the filesystem object in order to delete files. However, you may still need to delete the document content.

However, I would ask what about these documents do you want to retain, if not the content? Styles? Page Setup?

It's tough to give you a better way to do something without knowing why you're doing it all. It seems strange to me that you want to create a new document based on a user's document, and then delete all the content.

Perhaps it would be useful to open the user's document read-only, check the .AttachedTemplate property, and then create a new document based on that template?

DaVinney
04-13-2011, 10:21 AM
The deleting all content part was just to create a blank scratch document...though it's no longer necessary with the Documents.add method (which I came across previously, but thought it had to be based upon a template file).

Seems Documents.add will work and simplify things a lot. Thanks!

How do I give the focus to the new document created by Documents.add so I can continue the loop that inserts the user's list of files?

Paul_Hossler
04-13-2011, 10:23 AM
If you're trying to have a 'Master' document with the other documents included, you might use the INCLUDETEXT field with section breaks between each. Leaving off the usual \* MERGEFORMAT preserved the original formatting

You can also lock the fields and that would make the embedded text part of the master

Attachment has with 4 subdocuments included in a master

Paul

Frosty
04-13-2011, 10:25 AM
I've never really worked with Master documents... but if that works, fantastic.

In answer to your Documents.Add question... it's a function which returns the document opened as a document object. So...

dim oMyNewDoc as Document

Set oMyNewDoc = Documents.Add (Template:="0C:\Documents\MyDocument.docx")

DaVinney
04-13-2011, 01:41 PM
Thanks for the help guys.

This worked out nicely for my needs:
Set oNewDoc = Documents.Add (Template:="path:\filename")
I performed my operations on the resulting document object, then saved using the following since Dialogs(wdDialogFileSaveAs).Show kept defaulting to .docx which is not what users want.
With Dialogs(wdDialogFileSaveAs)
.Name = "Combined.doc"
.Format = wdFormatDocument
.Show
End With Gonna add a few more tweaks and put it into use.

:hi: