Share via


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 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: Pranav+Wagh, Microsoft+Blogger, Word, C#, HowTo

Technorati tags: Pranav+Wagh, Microsoft+Blogger, Word, C#, HowTo

Comments

  • Anonymous
    April 03, 2008
    PingBack from http://blogjocky.com/how-to-do-a-save-copy-as-in-word/

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

  • Anonymous
    June 29, 2008
    The comment has been removed

  • Anonymous
    July 20, 2008
    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 :)

  • Anonymous
    December 03, 2008
    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

  • Anonymous
    December 05, 2008
    This was just what I needed. Thank you for sharing your discovery.

  • Anonymous
    March 04, 2009
    Does not work consistently in Office 2007! Beware!

  • Anonymous
    May 17, 2009
    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.

  • Anonymous
    August 16, 2009
    How do you change the range to save a copy of the entire document?

  • Anonymous
    January 07, 2010
    Thank you, this works great (Word 2007)