How to do a "Save Copy As" In Word

How to do a "Save Copy As" In Word

  • Comments 10

Ever wondered how can you do a "Save Copy As" in word? Or why it's not there?  well ... I don't know about why it's not there, but I can tell you how to do it using custom code

To do it, the first thing you need to know about is - IPersistFile interface, this interface provides methods that permit an object to be loaded from or saved to a disk file, rather than a storage object or stream. As this is one of the compound document interfaces, word implements it.

Getting back to the theory, whenever any component implements an interface it has to implement all of it's methods, which in turn gets added to  the vtable of the component, whenever you want to use this interface from an external client you need to obtain a it's pointer,  then you can call any of the methods from this interface.

Here is a sample code in C#. Yes! it's a simple code, the only reason is System.Runtime.InteropServices.ComTypes Namespace which contains methods that are defintions of COM functions for managed code including IPersistFile.

If this was not available, then I might have been required to play a lot of "Marshal." and/or Win32API games to get this thingy working  -

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
using System.Reflection;
using System.Runtime.InteropServices;
using COM = System.Runtime.InteropServices.ComTypes;

namespace IPersistFileFromWord
{
    public partial class Form1 : Form
    {

        Word.Application wdApp;
        Word._Document wdDoc;
        object o=Missing.Value;
        public Form1()
        {
             InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            wdApp= new Word.Application();
            wdDoc=wdApp.Documents.Add(ref o,ref o, ref o, ref o);
            wdDoc.Range(ref o, ref o).Text = "Hello World ...";
            wdApp.Visible = true;
            COM.IPersistFile pp = (COM.IPersistFile)wdDoc;
            pp.Save(@"C:\upload\Something.doc",false);
        }
    }
}

Enjoy and take care ...

 

del.icio.us tags: , , , ,

Leave a Comment
  • Please add 3 and 3 and type the answer here:
  • Post
  • PingBack from http://blogjocky.com/how-to-do-a-save-copy-as-in-word/

  • Ever wondered how can you do a "Save Copy As" in word? Or why it's not there? well ... I don't know about

  • I get following error on Save

    "Error HRESULT E_FAIL has been returned from a call to a COM component."

    Do you have any idea what might be wrong?

    Thanks in advance.

  • Just wanted to say thanks for this code snippet - was looking for a way to save a copy of an active document in Word without affecting the original path and this did just the trick.

    Hey Martin - 

    Thanks for thanks :)

  • Hey, have you tried this in Word 2007?  It's a great trick and I've been using it in my 2003 solution, but in 2007 I get an E_FAIL with a message that "You cannot save while the file is in use by another process.  Try saving the file with a new name." when I try to call IPersistFile.Save() on ActiveWindow.Document.

    Not actually, as soon as I get some time I will test it out

  • This was just what I needed. Thank you for sharing your discovery.

  • Does not work consistently in Office 2007! Beware!

  • I'd love a solution to this that works in both 2003 and 2007.

    Right now we are doing a .Save followed by a File Copy.

  • How do you change the range to save a copy of the entire document?

  • Thank you, this works great (Word 2007)

Page 1 of 1 (10 items)