C# Microsoft.Office.Interop.Word“;打开此文档将运行以下SQL命令";

C# Microsoft.Office.Interop.Word“;打开此文档将运行以下SQL命令";,c#,office-interop,mailmerge,C#,Office Interop,Mailmerge,在使用Word中的邮件合并功能重新编写一些代码时,打开保存的Word文档后,我得到了以下信息。此代码使用了Microsoft.Office.Interop.Word 我所期望的是,在将数据文件(.txt)合并到模板文件(.dot)中,然后调用SaveAs方法后,我将能够打开word文档,而无需访问数据源,在本例中是.txt文件 public void MergeAndSaveAs(string template_path, string datasource_path, string save

在使用Word中的邮件合并功能重新编写一些代码时,打开保存的Word文档后,我得到了以下信息。此代码使用了
Microsoft.Office.Interop.Word

我所期望的是,在将数据文件(.txt)合并到模板文件(.dot)中,然后调用SaveAs方法后,我将能够打开word文档,而无需访问数据源,在本例中是.txt文件

public void MergeAndSaveAs(string template_path, string datasource_path, string saveAsFilePath = "")
    {
        // Constants
        int wdFormatXMLDocument = 12;
        int wdSendToNewDocument = 0;

        string filePath = saveAsFilePath.Remove(saveAsFilePath.LastIndexOf("."));

        // Extract the MailMerge property into an object
        Object wrdMailMerge = this.objDocument.GetType().InvokeMember("MailMerge", BindingFlags.GetProperty, null, this.objDocument, new Object[] { });
        
        this.objAppWord.GetType().InvokeMember("Selection", BindingFlags.GetProperty, null, this.objAppWord, new Object[] { });
        
        // Open the data source and point to the source file (.txt)
        wrdMailMerge.GetType().InvokeMember("OpenDataSource", BindingFlags.InvokeMethod, null, wrdMailMerge, new Object[] { datasource_path });

        // Set the destination
        wrdMailMerge.GetType().InvokeMember("Destination", BindingFlags.SetProperty, null, wrdMailMerge, new Object[] { wdSendToNewDocument });

        // Execute the Mail merge command
        wrdMailMerge.GetType().InvokeMember("Execute", BindingFlags.InvokeMethod, null, wrdMailMerge, new Object[] { false });


        try
        {

            this.objDocument.GetType().InvokeMember("SaveAs", BindingFlags.InvokeMethod, null, this.objDocument, new Object[] { filePath, wdFormatXMLDocument });
            this.objDocument.GetType().InvokeMember("Saved", BindingFlags.SetProperty, null, this.objDocument, new Object[] { true });
        }
        catch (Exception e)
        {
            string error = e.Message;
        }
        finally
        {
            this.objDocument.GetType().InvokeMember("Close", BindingFlags.InvokeMethod, null, this.objDocument, new Object[] { false });
            this.objDocument = null;
        }
    }
一个新的Word文档保存在我指定的位置,但它不是一个普通的Word文档,因为它需要与源文件的持续连接

如何以编程方式将合并文件保存到不需要数据源连接的新word文档中

更新 目前,我的代码还提示我保存文件。如果我在对话框中单击“是”,它会要求我更新格式。也许这也需要通过编程来完成