Exact copy of a document in memory?

A

Alex

Hello people,

I am having an issue with a C# Word add-in (but a VBA solution could help as well).

I am trying to create a copy of the current document.

First naive attempt:

using Word = Microsoft.Office.Interop.Word;
object missing = System.Reflection.Type.Missing;
object no = false;

Word.Document doc = myWordApp.ActiveDocument;
Word.Document newDoc = myWordApp.Documents.Add(ref missing, ref missing, ref missing, ref no);
newDoc.Range(ref missing, ref missing).FormattedText = doc.Range(ref missing, ref missing);

That copied the text, formatting and bookmarks but not other elements like headers/footers, textboxes, graphics, etc.


Question: how do I create a copy that will include all the content and formatting in the original document?

Thank you.


Best wishes,
Alex.
 
J

Jezebel

The content of a Word document is contained in a set of StoryRange objects.
One of these is the MainStory, which is what you are getting now. The others
contain headers and footers, footnotes, textboxes, etc. You'll need to deal
with these separately. The VBA code is along these lines --

Dim pRange as Word.Range

For each pRange in ActiveDocument.StoryRanges

Do
pRange.Copy .... (or whatever you want to do with it)
set pRange = pRange.Next
Loop until pRange is nothing

Next

The point of the inner loop is that some of the StoryRanges (such as
textboxes and headers/footers) are in effect linked lists.

Surely a simpler approach would be to use SaveAs?




Hello people,

I am having an issue with a C# Word add-in (but a VBA solution could help as
well).

I am trying to create a copy of the current document.

First naive attempt:

using Word = Microsoft.Office.Interop.Word;
object missing = System.Reflection.Type.Missing;
object no = false;

Word.Document doc = myWordApp.ActiveDocument;
Word.Document newDoc = myWordApp.Documents.Add(ref missing, ref missing,
ref missing, ref no);
newDoc.Range(ref missing, ref missing).FormattedText = doc.Range(ref
missing, ref missing);

That copied the text, formatting and bookmarks but not other elements like
headers/footers, textboxes, graphics, etc.


Question: how do I create a copy that will include all the content and
formatting in the original document?

Thank you.


Best wishes,
Alex.
 
G

Greg Maxey

Completely understandable. I made that same mistake in a reply I posted
last week and then spent 30 minutes trying to help the OP sort out why his
footer fields wouldn't update in large documents while they would update in
simple documents.

I like "mea culpa." I think the term I used was "I feel like an idiot."
 
J

Jezebel

In Latin you can be as moronic as you like, and everyone remains courteous.
Try ordering a Crustum etruscum from Dominos.
 
A

Alex

Hello Jezebel,

Jezebel said:
The content of a Word document is contained in a set of StoryRange objects.
One of these is the MainStory, which is what you are getting now. The others
contain headers and footers, footnotes, textboxes, etc. You'll need to deal
with these separately. The VBA code is along these lines --

Dim pRange as Word.Range
For each pRange in ActiveDocument.StoryRanges
Do
pRange.Copy .... (or whatever you want to do with it)
set pRange = pRange.NextStoryRange
Loop until pRange is nothing
Next

The point of the inner loop is that some of the StoryRanges (such as
textboxes and headers/footers) are in effect linked lists.

Surely a simpler approach would be to use SaveAs?

Yes, making a copy on the disk and opening is should be simpler.
The problem with SaveAs is that I'll have to SaveAs it back to the original name.

Thanks.


Best wishes,
Alex.
 
P

Peter Huang [MSFT]

Hi

Thanks for your feedback.
If you still have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jezebel

Yes, making a copy on the disk and opening is should be simpler.
The problem with SaveAs is that I'll have to SaveAs it back to the original
name.

Why? What's the problem with

Doc.SaveAs FileName:=[newname]

??
 
A

Alex

Jezebel said:
Yes, making a copy on the disk and opening is should be simpler.
The problem with SaveAs is that I'll have to SaveAs it back to the original
name.

Why? What's the problem with
Doc.SaveAs FileName:=[newname]

The problem is that I first have to save the "oldname", then save_as "newname", then reopen "oldname".
Which is time consuming, especially if done in a loop on a 100 documents or so.
 
P

Peter Huang [MSFT]

Hi Alex,

I think after you saveas the file to a newname, t he newname document
should be same as the oldname.
Then if we need to additional copy, we just need to do saveas again, we do
not need to reopen the oldname.

Did I miss something?

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jezebel

Not really very time-consuming, and quicker than trying to copy the document
element by element.



Jezebel said:
Yes, making a copy on the disk and opening is should be simpler.
The problem with SaveAs is that I'll have to SaveAs it back to the
original
name.

Why? What's the problem with
Doc.SaveAs FileName:=[newname]

The problem is that I first have to save the "oldname", then save_as
"newname", then reopen "oldname".
Which is time consuming, especially if done in a loop on a 100 documents or
so.
 
A

Alex O.

Hello Peter,

(Posting from Google with another account)

Peter Huang" said:
Hi Alex,

I think after you saveas the file to a newname, t he newname document
should be same as the oldname.
Then if we need to additional copy, we just need to do saveas again, we do
not need to reopen the oldname.

Did I miss something?

The documents have to be processed and modified according to some rules
before they are saved.

Original --> transform accrording to rule 1 --> save as document 1
Original --> transform accrording to rule 2 --> save as document 2
....
Original --> transform accrording to rule N --> save as document N

Documents 1..N are (potentially) different.

Best wishes,
Alex.
 
P

Peter Huang [MSFT]

Hi Alex,

Thanks for your quick response.
If so, another way, I think you may try to make 100+ a copied via file
system.
e.g. copy file 100+ time ,open them all and then save them one by one, and
at last delete the necessary ones.

The may save a little time.



Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Alex

Hello Peter,

Peter Huang" said:
If so, another way, I think you may try to make 100+ a copied via file
system.
e.g. copy file 100+ time ,open them all and then save them one by one, and
at last delete the necessary ones.

The may save a little time.

Yes, that is what I am doing now.
Thanks.


Best wishes,
Alex.
 
P

Peter Huang [MSFT]

Hi

Thanks for your quickly reply!

If you have other question, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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