Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word 2003: Make copy of document

Status
Not open for further replies.

BrianWen

Programmer
Jun 8, 2009
102
DK
Can it really be true, that Word 2003 can't do like Word 2007 and copy an existing document with Documents.Add

In 2007, it's possible to type which template, the new document gets based on, and by using doc.fullpath from the existing (and open) document, I can make a copy with documents.add Template:=doc.fullpath

HOWEVER, it seems documents.add has no available parameters to set in Word 2003. Is that really true?

Is it possible in another way to have Word itself copy a document?
 
Of course, I could save the document again, but that requires the document to be open in some way and that might not always to the case.
 
In word 2003 I confirm that the Documents.Add method has a Template named argument

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Indeed, .Add takes parameters.
In 2007, it's possible to type which template, the new document gets based on, and by using doc.fullpath from the existing (and open) document, I can make a copy with documents.add Template:=doc.fullpath
No, that is not making a copy.


It is using .Add to make a new document. That is all .Add does. It makes a new document. Yes, you can make that new document take an expressed template.

Further...

1. Word does not have .FullPath (at least up to 2003, I do not know about 2007). It has .FullName (path and name), and it has .Path (without the "\" at the end).

2. If you are getting the fullpath of a document, it is a huge assumption that the attached template path is the same - i.e. the template file is in the same folder as the document. Most of the time this would not be the case.

You could make a copy of the current document, and save it - say - add _COPY to the name of the current document, like this:
Code:
Sub MakeACopy()
Dim ThisDoc As Document
Dim MyNewDoc As Document
   
Set ThisDoc = ActiveDocument
Set MyNewDoc = Documents.Add
MsgBox ThisDoc.Path
MyNewDoc.Range = ThisDoc.Range
MyNewDoc.SaveAs FileName:=ThisDoc.Path & _
   "\" & Left(ThisDoc.Name, Len(ThisDoc.Name) - 4) & _
   "_COPY.doc"
End Sub
which would create a c:\Yadda\Blah_COPY.doc from a
c:\Yadda\Blah.doc


Gerry
 
PHV:
I downloaded the reference for Word 2003 from Microsoft, and it doesn't mention any parameters. And I can't get it to work. Could you provide the exact syntax or some documention. I don't doubt you, but I can't find it myself and my testmachine with Office 2003 doesn't do it right with documents.add. It's probably just me making some stupid error.

fumei:
I meant .fullname, of course.

You're pointing out something I didn't realize: When I add a new document based on a document based on a template, what then happens. As I read your post, the new document doesn't know about the original template!?
The original template would always be available, but not in the same folder as the documents created from it, as you point out.
In 2007 it works just like I intend. I make a new document, based on my template. I save the document in a different folder than the template. I reopen the document and run a macro that makes a new document based on the first document with documents.add giving the first document's fullname as template-parameter.

Regarding the copy solution, as I see it I can't use it. Copying the entire document range would still leave customdocumentproperties behind, right? - I mean, they wouldn't be copied too, right?
 
fumei:
I may not get what you're writing, but from my experiments just now, it seems that when I do documents.add with a document as template, the new doc doesn't care about the doc it was created from. It has all the macros and customdocproperties from the original template available, after i remove the doc is was created from.
It seems the docs have no relation at all. activedocument.attachedtemplate also states the template and not the doc.
 
the new document doesn't know about the original template!?
Have a look at the AttachedTemplate property.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV:
That's what I figured out :)
I was pointing out how I interpreted his post.

To be perfectly clear:
It inherits the first documents template as it's own template, instead of getting the first document itself as it's template. So, that good!
 
it seems that when I do documents.add with a document as template, the new doc doesn't care about the doc it was created from.

1. you can NOT do Documents.Add with a document as template. I will repeat. Documents.Add creates a new document...period. If no template is explicitly attached - Template:=path_to_DOT_file - then Normal.dot is attached.

2. "the new doc doesn't care about the doc it was created from". It was NOT created from any document, so no, it does not care, because it was not created from any document.

Gerry
 
fumei:
Well, I don't know about Office 2003, because I haven't gotten it to work yet (I haven't tested my latest alterations).
However, in Office 2007, you're wrong. I CAN create make a new document, based of a doc (not DOT). It gets all content but attachedtemplate is the template the first document was created from...

Unless I'm completely misunderstanding what you're saying, you're wrong.

A simple example:
Create doc1.doc from normal.dot
Type a few words in it
save it run macro that execute documents.add with doc1.doc as template
Yuo got a new document with the words but with normal.dot as attachedtemplate
 
Let's start with the following...before I have to eat some humble pie.

from the standard installed Help....
Add method as it applies to the Documents object.

Returns a Document object that represents a new, empty document added to the collection of open documents.

expression.Add(Template, NewTemplate, DocumentType, Visible)

expression Required. An expression that returns a Documents object.

Template Optional Variant. The name of the template to be used for the new document. If this argument is omitted, the Normal template is used.

NewTemplate Optional Variant. True to open the document as a template. The default value is False.

DocumentType Optional Variant. Can be one of the following WdNewDocumentType constants: wdNewBlankDocument, wdNewEmailMessage, wdNewFrameset, or wdNewWebPage. The default constant is wdNewBlankDocument.

Visible Optional Variant. True to open the document in a visible window. If this value is False, Microsoft Word opens the document but sets the Visible property of the document window to False. The default value is True.
As you can see, .Add can take parameters.


Ok. Ahem. Ahem. Cough, cough.

Yes, you can indeed use:
Code:
Documents.Add Template:="c:\zzz\AfterBookmark2.[b]doc[/b]"
using a document as the "template".

But, IMO this is a very dangerous thing to do, and contrary to the whole point of using templates.

Why? Let's say you have a .DOT (a template, a real template) named Whatever.dot. To keep this simple, let's say it has the following as content.

Yadda yadda
<FORMTEXT> ' a BLANK (empty) text formfield[/color red]

Some other text yadda yadda.
<FORMTEXT> ' a BLANK (empty) text formfield[/color red]

OK? You use Whatever.dot to generate a new document, and you put some input into the formfields.

Yadda yadda
<Some text into this formfield>

Some other text yadda yadda.
<Some other text into this formfield>

Now you save it as c:\Blah\Test.doc. If you look at the File > Properties you can see the attached template is Whatever.dot.

Good.

OK, now execute:
Code:
Documents.Add Template:="c:\Blah\Test.[b]doc[/b]"

True, a new document is added. True, the attachedtemplate IS Whatever.dot...BUT, the content is:

Yadda yadda
<Some text into this formfield>

Some other text yadda yadda.
<Some other text into this formfield>

The content of c:\Blah\Test.doc, NOT the contents of the template you are attaching. Which, to me, is contrary to the point of using templates. I must admit though, you are technically correct. You can use .doc in the Template:= path parameter. And it does indeed attach the template attached to that document. But it does NOT have the contents of template. It has the contents of the document.

Gerry
 
First, let my point out that I really appreciate the time you guys take in helping me.

Now, we've come to an agreement regarding your last post. The problem you describe is not problem to me, because it is exactly what I'm looking for; The actual content of the document.Test.doc but attachedtemplate Whatever.dot

So that's perfect :)
 
I think this is poor use of Word, but if it works for you...what can I say?

Could please (for my own interest) explain to me what would be the circumstance for doing it this way?

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top