none
Programmatically Enforcing Merge Formatting RRS feed

  • Question

  • Hello,

    I have a custom c# reporting tool that creates a Word document. During testing it was discovered that when text is copied from one document and pasted into the documented created by the tool the preferred font is only maintained if the Merge Formatting is selected. We're using Word 2010 or higher.

    Instead of training the users to select Merge Formatting I wanted to see if there is a way to programmatically set this in c# as part of the reporting tool. In VBA I found that this can be accomplished using the following paste and format option:

    wdUseDestinationStylesRecovery Uses the styles that are in use in the destination document.


    Is there a similar paste and format option in c#?

    To further clarify, I want to be able to set the following using c# code rather than setting within Word.

    1. Click the Microsoft Office Button
    2. Click Advanced, and then scroll down to the Cut, copy, and paste section.
    3. Select the settings that you want with the following boxes.
      • Pasting within the same document
      • Pasting between documents
      • Pasting between documents when style definitions conflict
      • Pasting from other programs

    For more information about these options, see the Options for pasting within and between documents, and from other programs of this article.

    1. To use settings for the smart cut-and-paste option, select the Use smart cut and paste check box, click Settings, and then select the settings that you want. For more information, see the Settings for the smart cut-and-paste option section of this article.
    2. Click OK.

    Thank you in advance for any information.

    Regards,

    Chris

    Wednesday, August 26, 2015 12:28 AM

Answers

  • Hi Chris,

    >>Do you know if there is a similar option available in c#?

    Yes, it is available in C#, and the code in my first reply is C# code. With using the code, you will need to add the Assembly Microsoft.Office.Interop.Word (in Microsoft.Office.Interop.Word.dll) reference to your project.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Marked as answer by Chris Eckert Thursday, August 27, 2015 10:48 PM
    Thursday, August 27, 2015 5:53 AM

All replies

  • Hi Chris,

    >> In VBA I found that this can be accomplished using the following paste and format option:

    Based on your description, it seems that you want to set the option in the Word, and the styles that are in the destination document is you want. If so, you could set the Options Properties which you want with the WdPasteOptions.

    #Options.PasteFormatBetweenDocuments Property
    https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.options.pasteformatbetweendocuments(v=office.14).aspx
    # WdPasteOptions enumeration
    https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.wdpasteoptions(v=office.15).aspx

    A simple code like below:

    using Microsoft.Office.Interop.Word;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using Word = Microsoft.Office.Interop.Word;
    
    namespace OfficeAPI.Class.WordAutomation
    {
        class setOptionClass
        {
            public static void setOptions()
            {
                Word.Application wd = new Word.Application();
                wd.Visible = true;
                wd.Documents.Add();
                wd.Options.PasteFormatBetweenDocuments = WdPasteOptions.wdMatchDestinationFormatting;
                MessageBox.Show("ok");
            }
            
        }
    }
    

    In addition, this Option is application level, if you change it, it will apply all of the documents.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, August 26, 2015 7:07 AM
  • Thank you Edward,

    I appreciate the detailed response. Do you know if there is a similar option available in c#?

    Regards,

    Chris

    Wednesday, August 26, 2015 2:21 PM
  • Hi Chris,

    >>Do you know if there is a similar option available in c#?

    Yes, it is available in C#, and the code in my first reply is C# code. With using the code, you will need to add the Assembly Microsoft.Office.Interop.Word (in Microsoft.Office.Interop.Word.dll) reference to your project.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Marked as answer by Chris Eckert Thursday, August 27, 2015 10:48 PM
    Thursday, August 27, 2015 5:53 AM
  • Thank you Edward. That worked brilliantly.

    Regards,

    Chris

    Thursday, August 27, 2015 10:48 PM