Clone current active document without saving to a file, is this possible?

R

Ricky Marek

Hello,

I would like to make a duplicate of the currently open document via a VBA macro.

That is, when running the macro, it will use the current active document as source and make a copy of it without saving it to a file.

The current document may be modified (changes were not yet saved to a file), and it could be based on a specific template, with headers/footers/fields.

The new copy, should have an identical content (including headers/footers/fields) as the original, based on the same template but with a new name and not saved to a file yet. (The user may save it later if required)

Is this possible?

Thanks in advance.

--- Ricky Marek.
 
S

Stefan Blom

You can use Documents.Add with the name of the current document as the
"template name." The requirement is that the current document has been saved
to disk. For example:

Sub CreateNewDocBasedOnCurrentDoc()
If Documents.Count < 1 Then
Exit Sub
End If
If ActiveDocument.FullName <> "" Then
Documents.Add Template:=ActiveDocument.FullName
End If
End Sub

As you can tell, the macro also tests to make sure that there is at least
one open document.
 
S

Stefan Blom

To ensure that the attached template is correct, try this code instead:

Sub CreateNewDocBasedOnCurrentDoc()
Dim TemplateName As String
Dim NewDoc As Document
If Documents.Count < 1 Then
Exit Sub
End If
If ActiveDocument.FullName <> "" Then
TemplateName = ActiveDocument.AttachedTemplate.FullName
Set NewDoc = Documents.Add(Template:=ActiveDocument.FullName)
NewDoc.AttachedTemplate = TemplateName
End If
End Sub
 

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