How to make an in-memory clone of a word document

R

robertf2

Using VB6 I have a requirement to create a separate copy of an in-memory word
document on disk (for subsequent processing) without writing overwriting the
original file or changing the name fullpath of the active document(cannot use
..save, this overwites the original, cannot use .saveas as this changes the
filename and this cannot be changed back to the original as .Fullname is a
readonly property).

So, what I though was to clone the document in memory and save the clone to
a temporary file. I have some proof of concept code that appears to work but
what I am worried about is that I am not copying the all the document and .
I see I have to copy the content, the builtindocumentproporties and the
customdocument properties.

Is there anything else that needs to be copied to make sure I have
everything (forms, scripts or whatever)?)

Here is my POC code:

Public Sub CopyDoc(docSource As Word.Document)
Dim docDestination As Word.Document
Dim i As Integer
Dim strProperties As String


Set docDestination = docSource.Application.Documents.Add

docDestination.Content.FormattedText = docSource.Content.FormattedText

' TODO:Temporary exception handler to cope with unset properties
On Error Resume Next
For i = 1 To docSource.BuiltInDocumentProperties.Count
docDestination.BuiltInDocumentProperties.Item(i) =
docSource.BuiltInDocumentProperties.Item(i)
Next i

' TODO: Custom properties

docDestination.SaveAs ("c:\junk1.doc")
docDestination.Close
Set docDestination = Nothing

End Sub
 
J

Jezebel

FullName is read-only because it's derived from other, editable properties,
like Name and Path. Why can't you Save the original, then do a SaveAs,
close, then re-open the original?

In any case, trying to extract the document from memory is likely to be a
herculean undertaking. a) an open document doesn't necessarily exist in
memory all at once -- Word uses it's own temporary file to save parts of it
(and there's no documentation for how this works); b) Word seems to do
things precisely to prevent other software getting at the object in memory,
for anti-virus reasons (eg to prevent you inserting your own macro code into
the document).
 

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