简体   繁体   中英

How to run a macro from C#?

I have a macro (.docm) that opens an rtf file and saves it in.doc format. This macro needs to be run from a C# application. How to do it?

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {

        // Создание экземпляра Word
        Word.Application app = new Word.Application();
        app.Visible = true;

        Word.Documents doc = app.Documents;

        // Открытие файлов
        MessageBox.Show("add file (.docm)");
        OpenFileDialog macroFile = new OpenFileDialog();
        if (macroFile.ShowDialog() != DialogResult.OK)
        {
            MessageBox.Show("Error");
            return;
        }
       
    }

}

After you have got a file name you can use the Documents.Open method which opens the specified document and adds it to the Documents collection. The Document.SaveAs2 method allows saving the specified document with a new name or format. Some of the arguments for this method correspond to the options in the Save As dialog box (File tab). The format in which the document is saved. Can be any WdSaveFormat constant. It seems you are interested in the wdFormatDocument value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM