Tuesday, November 20, 2007

Word SaveCopyAs

Rory:

Background:


While writing C# based Add-in components for Office 2003 I noticed that the SaveCopyAs function that works so well in Excel, PowerPoint, Outlook, etc was absent from the Word interop assembly. After digging around for hours I found the following solution.

Solution:

public void SaveCopyAs(Word.Document wordDoc)

{

string fileName = TempFolderPath + @"\" + wordDoc.Name;

object saveFormat = Word.WdSaveFormat.wdFormatDocument;


if (Path.GetExtension(fileName).ToLower() != ".doc")

{

fileName += ".doc";

}


// There is no SaveCopyAs function in the ActiveDocument API

// Here is the workaround for that problem

object index = 1;

wordDoc.HTMLProject.HTMLProjectItems.Item(ref index).SaveCopyAs(fileName);

}





wordDoc.HTMLProject.HTMLProjectItems.Item(ref index).SaveCopyAs("c:\\1.doc");