Paste Formatted Content to a Word Document
Hi,
I know, I have created this blog for new Silverlight developers, but since I could not find this easily in the web, I am posting a solution to the problem;
Scenario: Copy the complex contents from the word document to another word document, preserving the exact format;
- When we do a normal paste, we wont be getting the same font / size etc,
Solution: Use a Paste Special instead of a normal Paste
When using Paste Special, Change the Data type;
e.g:
OfficeWord.Application oWordApplication = new OfficeWord.Application();
OfficeWord.Document oExistingDocument = new OfficeWord.Document();
object oMissing = System.Reflection.Missing.Value;
object oFileName = "http://MyOfficeTest/2007/My%20Documents/Source.docx";
// Using file in the local system
//object oFileName = "C:\\Source.docx";
object oFileFormat = OfficeWord.WdOriginalFormat.wdOriginalDocumentFormat;
object oDataTypeMetafile = OfficeWord.WdPasteDataType.wdPasteRTF;
object oSaveChanges = false;
oExistingDocument = oWordApplication.Documents.Open(ref oFileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing,ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
oExistingDocument.ActiveWindow.Selection.WholeStory();
oExistingDocument.ActiveWindow.Selection.Copy();
//This is when we use a word add-in
Globals.ThisAddIn.Application.Selection.PasteSpecial(ref oMissing,ref oMissing,ref oMissing,ref oMissing, ref oDataTypeMetafile,ref oMissing,ref oMissing);
oExistingDocument.Close(ref oSaveChanges, ref oMissing, ref oMissing);
oWordApplication.Quit(ref oMissing, ref oMissing, ref oMissing);
I know, I have created this blog for new Silverlight developers, but since I could not find this easily in the web, I am posting a solution to the problem;
Scenario: Copy the complex contents from the word document to another word document, preserving the exact format;
- When we do a normal paste, we wont be getting the same font / size etc,
Solution: Use a Paste Special instead of a normal Paste
When using Paste Special, Change the Data type;
e.g:
OfficeWord.Application oWordApplication = new OfficeWord.Application();
OfficeWord.Document oExistingDocument = new OfficeWord.Document();
object oMissing = System.Reflection.Missing.Value;
object oFileName = "http://MyOfficeTest/2007/My%20Documents/Source.docx";
// Using file in the local system
//object oFileName = "C:\\Source.docx";
object oFileFormat = OfficeWord.WdOriginalFormat.wdOriginalDocumentFormat;
object oDataTypeMetafile = OfficeWord.WdPasteDataType.wdPasteRTF;
object oSaveChanges = false;
oExistingDocument = oWordApplication.Documents.Open(ref oFileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing,ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
oExistingDocument.ActiveWindow.Selection.WholeStory();
oExistingDocument.ActiveWindow.Selection.Copy();
//This is when we use a word add-in
Globals.ThisAddIn.Application.Selection.PasteSpecial(ref oMissing,ref oMissing,ref oMissing,ref oMissing, ref oDataTypeMetafile,ref oMissing,ref oMissing);
oExistingDocument.Close(ref oSaveChanges, ref oMissing, ref oMissing);
oWordApplication.Quit(ref oMissing, ref oMissing, ref oMissing);
Comments
Post a Comment