Saving Word document in temp file

D

David

I am working on an MS Word add-in written in C# using IDTExtensibility2.

I would like to save a copy of the ActiveDocument at certain times. I would
like for this saving to be similar to Word's auto-recover saving where it is
completely transparent to the user. This means that when a new document is
saved by my code the filename does not appear in the title bar and the user
is prompted to save changes to a new file (not the one I saved to). Also,
when my code saves from an existing document, the user is still prompted to
save their changes.

I tried to save using Document.SaveAs(), but it is just as if the user saves
the document. Is there another function that can do this save for me?

One other idea I've had is to make a copy of the Document object, and then
save that Document to a file. I've not found any way to copy the Document,
other than the shallow object.MemberwiseClone.

Thanks for the help.
David
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?RGF2aWQ=?=,

There is no other possibility in the object model than SaveAs.

you would have the option of
1. customizing the application's .Caption property (so it appears the name
hasn't changed)
2. Setting the document's .Saved property to false, so that the user is
prompted to save his changes
3. Substitute your own buttons for the File and File Save menus and buttons
to show the dialog box pre-set with a DIFFERENT file name than the one to which
you saved. Your code would also have to rewrite the keyboard shortcut Ctrl+S
for the current document context (be sure to set the context, otherwise you'll
wreck the user's environment).

If this were Word 2003, and the document were marked up with XML tags (even one
that encompassed the entire file) you could extract the text directly from the
XML tags.

You could also extract just the text using the various .Ranges supplied by the
object model.

Another possibility would be to SelectAll then do a Copy/Paste into a new
document and save that.
I am working on an MS Word add-in written in C# using IDTExtensibility2.

I would like to save a copy of the ActiveDocument at certain times. I would
like for this saving to be similar to Word's auto-recover saving where it is
completely transparent to the user. This means that when a new document is
saved by my code the filename does not appear in the title bar and the user
is prompted to save changes to a new file (not the one I saved to). Also,
when my code saves from an existing document, the user is still prompted to
save their changes.

I tried to save using Document.SaveAs(), but it is just as if the user saves
the document. Is there another function that can do this save for me?

One other idea I've had is to make a copy of the Document object, and then
save that Document to a file. I've not found any way to copy the Document,
other than the shallow object.MemberwiseClone.

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 :)
 
D

David

Thanks for those ideas Cindy.

I have been looking into adding a new Document using Documents.Add(). I
would get the AttachedTemplate.FullName string and use that as the template
for the new document. I'd also make it invisible. I can then select all the
text and copy it. Then save and close the new, invisible document.

However, this does not maintain headers and footers. I need the whole
document to copy over. Also, even if I get headers and footer, I may be
missing other things.

Thanks again.
David
 
D

David

If I select all the text of a document, I do get the header and footer.

Two problems occur however:
1. The selection remains on the original document. I need to get the
original selection and cursor location before selecting and then reset those
two. I can't find a way to do that.
2. The clipboard now contains data that I do not want there, and the user
has lost their previous copy. Next time a paste is done, it is the entire
Word document.

Thanks for the help.
David
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?RGF2aWQ=?=,
Two problems occur however:
1. The selection remains on the original document. I need to get the
original selection and cursor location before selecting and then reset those
two. I can't find a way to do that.>
I usually work with object variables specific to each document (rather than
using ActiveDocument). In VBA-speak
//Assume this could be for any document
Set doc1 = ActiveDocument
Set doc2 = Documents.Add("TemplateName")

Now you can safely move between the documents (using .Activate, if you must;
you might have to for Copy to work correctly - test it!) by referring to the
object variable.
2. The clipboard now contains data that I do not want there, and the user
has lost their previous copy. Next time a paste is done, it is the entire
Word document.
Depending on the version of Office, the user may still be able to get at the
previous copy via the Office Clipboard, but I understand what you're saying. If
memory serves, it might be possible to save what's on the WINDOWS clipboard,
then restore it. Problem is, Office uses its own Clipboard and there's no
object model/interface for that.

Best I can think of would be to use the API to find out what kind of
information is on the Clipboard. Paste/store it somewhere appropriate, then
restore it after the save process.

Otherwise, you have no choice but to go the .Range.FormattedText route. But
that will NOT pick up headers, footers, and any other information stored in the
last section break of the document. This would include many of the settings in
Page Setup, as well as number of newspaper columns. VERY complicated.

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

Eugene E. Starostin

Hi David,

You can use the IPersistFile interface for backgroud saving.
This interface is supported by the Word.Document object.

Regards from Belarus
Eugene Starostin
 
D

David

Well, I've been playing around with the copy and paste idea, but I really
don't like it. There seem to be too many problems that may occur.

If I go with the first option you presented, where I:
1. Change the caption
2. Set .Saved = false
3. Create new Save/SaveAs buttons and rewrite Ctrl+S.

How do I accomplish item 3?
How can I replace the buttons and change the shortcut key?
Also, what is meant by setting the context to avoid wrecking the user's
environment?

Thanks again.
PS. Are there any good books out there that speak to this topic and doing
other workarounds in Word?
 
D

David

Thanks!
This really helps. I was doing a lot of work to copy and paste text and
preserve the clipboard and all sorts of stuff.
Now, I just write

Document doc = app.ActiveDocument;
UCOMIPersistFile file
file = doc as UCOMIPersistFile
if(file != null)
{
file.Save(filename, false);
}

How's that? Is there anything else I should do to ensure it saves correctly?

Thanks again!!!
 
E

Eugene E. Starostin (Add-in-Express.com)

Dave,
How's that? Is there anything else I should do to ensure it saves
correctly?

My thoughts followed this way.

1. Word supports OLE. Its documents are structured storages.

2. Ok, then Words should use storages (IStorage) for documents.

3. If it is so I can ask Word to give me any OLE or COM fundamental
intrefaces.

4. A document turned out to be able to return IPersistFile.

But you ask me whether this will work. It must work till Word is an OLE
server that supports embeding for OLE objects :)

Eugene.
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?RGF2aWQ=?=,
1. Change the caption
2. Set .Saved = false
3. Create new Save/SaveAs buttons and rewrite Ctrl+S.

How do I accomplish item 3?
How can I replace the buttons and change the shortcut key?
Also, what is meant by setting the context to avoid wrecking the user's
environment?
Are you still interested in this, or was Eugene's answer all you need :)?
PS. Are there any good books out there that speak to this topic and doing
other workarounds in Word?
None that I'm aware of. Most book publishers hear the term "Word" and run
away, screaming. Word at an advanced level apparently doesn't sell. These
groups, the word.vba groups, MSDN and, to an extent, the Word MVP website's
are your best resource.

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 :)
 
D

David

Well, I'm sort of interested in how to change the Save/SaveAs buttons and
keyboard shortcut, but I'm going with Eugene's solution. I've already
implemented it and it works great.

Thanks for the help!
David
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?RGF2aWQ=?=,
Well, I'm sort of interested in how to change the Save/SaveAs buttons and
keyboard shortcut, but I'm going with Eugene's solution. I've already
implemented it and it works great.
Glad to hear it :) It's not something I'd ever heard about before (being an
Office VBA person), but I was sure happy to see him come up with it!

In order to change how a default button works (rather than substituting
one's own button) is to have a procedure in the Word document, it's attached
template, or a global template with the same name as the built-in command.
To replace File/Save with my actions, I'd name the procedure FileSave; the
other would be FileSaveAs. This would also replace the action for the
keyboard shortcut, and anything else that calls the default command.

Keyboard shortcuts can only be assigned to procedures in the Word
environment. In order to have them call procedures in an addin, you need, at
the very least, a callback in the Word environment.

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 :)
 

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