MS Word: Save a copy?

E

Evan Stone

Is it possible to save a copy of (or duplicate) a document that a user is
currently working on without saving the actual one they're editing through
automation?

let's say the user is working on Document A. I'd like to save a copy of
Document A to the user's temp directory under a new name for previewing
purposes, but I'd like to leave Document A untouched.

Another scenario I could imagine is being able to create a Document B
either directly from A in its current state or copy everything from Document
A to B, but I'm not sure if that can be done either, and still maintain all
the formatting information.

Any help, information, or alternative suggestions will be greatly
appreciated!

Thanks!

evan stone | software engineer
 
E

Evan Stone

let's say the user is working on Document A. I'd like to save a copy of
Document A to the user's temp directory under a new name for previewing
purposes, but I'd like to leave Document A untouched.

I think what I meant to say here was, "I'd like to leave the ORIGINAL
Document A untouched."

Hopefully that makes more sense... If not, feel free to request more info!

Thanks!

evan stone | software engineer
 
W

wilsond

I'm a noobie without a clue for the most part but it seems to me if you
wanted to save a copy of Document A under a new name for previewing while
Document A is still "open" under Word that maybe through automation code you
do a "select all", then a "Edit|Copy", open a new blank document, paste, save
to new file name?
 
E

Evan Stone

Hi wilsond,
I'm a noobie without a clue for the most part but it seems to me if you
wanted to save a copy of Document A under a new name for previewing while
Document A is still "open" under Word that maybe through automation code you
do a "select all", then a "Edit|Copy", open a new blank document, paste, save
to new file name?

Yes, I also thought of that (or something similar), but I'm concerned that
it may not bring over *everything* -- i.e. document margin settings and
other formatting information.

I'll keep it in mind though, since I may have to do just that.

Thanks for your help!

-Evan
 
E

Evan Stone

....well it looks like what I'm going to have to do here is SaveAs to the
user's temp directory. Not ideal, but it will probably work for what I need
right now.

If anyone has any notion of how to clone a document via automation I'm still
open to suggestions, however.

;)

evan stone | software engineer
 
C

Cindy M -WordMVP-

Hi Evan,
If anyone has any notion of how to clone a document via automation I'm still
open to suggestions, however.
there isn't really a way to do this, short of the copy/paste suggestion made
earlier. If you really do a Select All, then Selection.Copy, and paste into a
brand new document, this should bring across everything (unlike, for example,
Range.FormattedText).

Personally, though, I'd probably feel "safer" using SaveAs.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
S

Shannon McCord

What if you (programmatically) stored the document name and location into a
variable, and then did a save as, closed the new document and then opened
the original document from the saved variable?

I have never tried, that, but couldn't the code be saved to the normal.dot
template to make it work when the documents are being switched?

Shannon
 
E

Evan Stone

What if you (programmatically) stored the document name and location into
a
variable, and then did a save as, closed the new document and then opened
the original document from the saved variable?

An interesting suggestion. Since I'm doing this from an external application
it's certainly a possibility.

Additionally, I suppose I could "fake" a dirty state of the document by
inserting and deleting a space character at the end of the document, so it
would appear as if it was in the same state in which the user left it.
Unfortunately, I'd lose their place in the document, and there's no way to
tell if the document was dirty to start with (that I know of). That might
not work after all... Bummer.

Hmmmm.... well, it's sort of working the way I have it now (saving to the
temp directory), and I cleared the workflow with the person who is
responsible for approving changes in the design (until they want to change
it again ;).

But I'll definitely keep your suggestion in mind, since it's the best one
I've heard so far, and I'm not really crazy about how I'm handling it at
present.

Thanks!

evan stone | software engineer
 
C

Cindy M -WordMVP-

Hi Evan,
Unfortunately, I'd lose their place in the document, and there's no way to
tell if the document was dirty to start with (that I know of).
1. You can test doc.Saved before you perform your save. Then you'd know if
it was dirty.

2. You should be able to set a RANGE before making the document dirty and
return to it.

Word.Range rng = Selection.Range;
//Do something
rng.Select();

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
E

Evan Stone

Hi Cindy,
1. You can test doc.Saved before you perform your save. Then you'd know if
it was dirty.

2. You should be able to set a RANGE before making the document dirty and
return to it.

Word.Range rng = Selection.Range;
//Do something
rng.Select();

This is great information - thanks! (especially the Saved property!)

I have a couple of quick and easy questions (hopefully): 1) how do I
deselect a range, and 2) is there a way to move the document to the
beginning (i.e. show page 1, if the current page is 3)?

Thanks!

evan stone | software engineer
 
E

Evan Stone

I have a couple of quick and easy questions (hopefully): 1) how do I
deselect a range, and 2) is there a way to move the document to the
beginning (i.e. show page 1, if the current page is 3)?

I think I have figured it out on my own -- the HomeKey method of the
Selection object in following code seems to solve both problems:

// C# Automation Code
Word.Selection selection =
this.wordDocumentPreview.ActiveWindow.Selection;
object units = WdUnits.wdStory;
object move = WdMovementType.wdMove;
selection.HomeKey(ref units, ref move);

....and it seems to be working in Word 2000 & 2003. Whew!

;)

evan stone | software engineer
 
C

Cindy M -WordMVP-

Hi Evan,

Sorry, I was "out" for a few days :)

What you found will work. My preference for (1) would be:
Selection.Collapse wdCollapseStart
I think I have figured it out on my own -- the HomeKey method of the
Selection object in following code seems to solve both problems:

// C# Automation Code
Word.Selection selection =
this.wordDocumentPreview.ActiveWindow.Selection;
object units = WdUnits.wdStory;
object move = WdMovementType.wdMove;
selection.HomeKey(ref units, ref move);

....and it seems to be working in Word 2000 & 2003. Whew!

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
E

Evan Stone

Sorry to resurrect this dead horse only to start flogging it again, but it's
an issue that's coming back to haunt me now (of course -- a month after I
implemented this as a "Save As...")...

Anyway back to the discussion. My original question was (the short version):

"Is it possible to save a copy of (or duplicate) a document
that a user is currently working on without saving the actual
one they're editing through automation?"

....and Cindy M's reply was:
there isn't really a way to do this, short of the copy/paste
suggestion made earlier. If you really do a Select All, then
Selection.Copy, and paste into a brand new document,
this should bring across everything (unlike, for example,
Range.FormattedText).

So what I'm hearing is that the Selection.Copy() might actually get me 99%
of the way (or thereabouts). What other document information would I be
missing using the Copy() method, and how would I go about retrieving the
*rest* of the information that I need?

IOW, what pieces of the document would be left out through a Copy/Paste
action?

Thanks!

evan k. stone | software engineer
 
C

Cindy M -WordMVP-

Hi Evan,
So what I'm hearing is that the Selection.Copy() might actually get me 99%
of the way (or thereabouts). What other document information would I be
missing using the Copy() method, and how would I go about retrieving the
*rest* of the information that I need?

IOW, what pieces of the document would be left out through a Copy/Paste
action?
This is difficult to answer, without knowing what kinds of things you're
specifically concerned about. As best I can remember (been a while since
I've tested this in any detail), as long as the target file is pristine, you
should pick up everything that has to do with the document text (margins,
headers, footers included).

You won't get macros, or anything like that. I don't think custom document
properties will come across, but you'd have to test. Nor document variables.
Styles that have been applied to text in the document will transfer.

If the target file has ever been edited, in any way, you'd lose the
document/section-relevant information "saved in" the last paragraph mark.
And you'd lose the style definitions of styles present in the target file
(Normal, Heading 1, etc.).

Some testing with Ctrl+A, Ctrl+C, switch to a new document and Ctrl+V should
give you a picture of what you're dealing with. Pay special attention to
settings in File/Page Setup and, if relevant, Tools/Options. (Note: some of
these are application-specific and won't transfer; others are
document-specific and I'm not sure if they'll transfer, but you might be
concerned that they would).

Besides what's in Page Setup, the only other properties that come to mind
you'd need to double-check would be Format/Columns (for the last section of
the document).

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
E

Evan Stone

As best I can remember (been a while since
I've tested this in any detail), as long as the target file is pristine, you
should pick up everything that has to do with the document text (margins,
headers, footers included).

OK... it looks like I'm going to try the Copy/Paste methodology, but I was
wondering if there was already an article or information available on
Clipboard maintendance & cleanup. I'd like to be able to capture whatever's
in the clipboard before my massive Copy/Paste, perform the action, and then
restore the clipboard to its original state.

So now I have some more questions (of course)...

1. Is this doable, or will I have to resort to blowing away whatever's in
the user's clipboard?
2. What are the ramifications with Office's multiple-object clipboard?
3. Might I have Office version compatibility issues with the clipboard
maintenance?

Any links/info will be greatly appreciated.

Thanks!

evan k. stone | software engineer
 
C

Cindy M -WordMVP-

Hi Evan,
OK... it looks like I'm going to try the Copy/Paste methodology, but I was
wondering if there was already an article or information available on
Clipboard maintendance & cleanup. I'd like to be able to capture whatever's
in the clipboard before my massive Copy/Paste, perform the action, and then
restore the clipboard to its original state.

So now I have some more questions (of course)...

1. Is this doable, or will I have to resort to blowing away whatever's in
the user's clipboard?
2. What are the ramifications with Office's multiple-object clipboard?
3. Might I have Office version compatibility issues with the clipboard
maintenance?
As far as I know, no one has ever found an exposed interface to the OFFICE
clipboard, which is a problem. Therefore, no documentation that I know of. If
you use the internal copy/paste commands, I think you have no way of
affecting what the user is going to be exposed to via the Office Clipboard.
Although using the Windows API you should be able to affect what goes OUTSIDE
of the Office apps, so that you could avoid losing stuff from the Windows
Clipboard.

The user, then, could only lose the oldest Office-internal objects on the
Clipboard. I think it saves 12 items, max (definitely in Word 2002, I don't
think it changed for 2003, but I could be wrong).

So, the only compatibility issues should be at the Windows level.

(Note: a couple of times, I've been on the verge of suggesting extracting the
document's XML, but there are a couple of issues with this: 1. It doesn't
pick up EVERYTHING (mostly, document properties and things at this level). 2.
It's only supported in Word 2003. But I thought it worth mentioning...)

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
T

Tony Jollans

A couple of points re: the clipboard.

1. If you do a copy operation you will overwrite whatever was on the Windows
clipboard - but this is common practice and nobody really seems to think it
is a problem.

2. A copy operation will also add a new item at the 'top' of the Office
clipboard and remove the oldest one if need be to make space. Again there
isn't really much you can do about it and common practice is not to worry
about it.

3. There is some limited programmatic access to the Office clipboard in Word
2000, and if you really really really want to return the clipboards to their
original state I *think* it could be done.

4. About the only thing I know how to do in 2002 and 2003 is find out how
many items are on the Office clipboard.

5. If you try and program the Office clipboard, yes you will have version
issues.
 
E

Evan Stone

As far as I know, no one has ever found an exposed interface to the OFFICE
clipboard, which is a problem.

Correct.

It's documented in the following KB article:

OFF2000: Using the Office 2000 Clipboard (PSS ID Number: Q221190)

....where it states, "There is no Visual Basic for Applications object model
for the Office Clipboard, so there is no way to programmatically manipulate
it."

So I came up with a rather cheesy workaround, which doesn't *fully* preserve
the clipboard, but it does a pretty decent job. I use three documents: the
original document (document1), the new document (document2), and a "buffer
document" (document3).

I create document3, paste the current contents of the clipboard into it,
create document2, "clear" the clipboard by copying the contents document2 (i
know, it's not really clearing it...), copy the contents of document1, paste
into the document2. When finished, I then copy document3, thus sorta
restoring the clipboard. There are definite problems with this methodology,
but for the most part it worked in simple cases - it was just when there was
something like a Visio chart on the clipboard beforehand that things got a
little ugly.

All of this is moot anyway -- since we reverted back to the way I was
originally handling things: with the SaveAs. Why? because the copy/paste
methodology just didn't cut it, and the SaveAs route just works much better.
So we adjusted the UI a bit so that the user knows that a save is going to
happen.

Thanks for the reply, however!

evan k. stone | software engineer
 

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