c# - 在 Microsoft Word 2010 中将文本发送到邮件合并字段

标签 c# winforms ms-word mailmerge word-2010

我正在使用以下代码将文本发送到一个简单的单词模板,我目前只使用一个 MergeField 设置来测试我能否正常工作。
我使用的代码如下:

public static void ReplaceMailMergeField(string pWordDoc, string pMergeField, string pValue)
{
    object docName = pWordDoc;
    object missing = Missing.Value;
    Word.MailMerge mailMerge;
    Word._Document doc;
    Word.Application app = new Word.Application();
    app.Visible = false;
    doc = app.Documents.Open(ref docName, ref missing, ref missing, ref missing, ref missing,
                                          ref missing, ref missing, ref missing, ref missing,
                                          ref missing, ref missing, ref missing, ref missing,
                                          ref missing, ref missing, ref missing);
    mailMerge = doc.MailMerge;
    foreach (Word.MailMergeField f in mailMerge.Fields)
    {
        if (f.Code.Text.IndexOf("MERGEFIELD  \"" + pMergeField + "\"") > -1)
        {
            f.Select();
            app.Selection.TypeText(pValue);
        }
    }
    object saveChanges = Word.WdSaveOptions.wdSaveChanges;
    doc.Close(ref saveChanges, ref missing, ref missing);
    app.Quit(ref missing, ref missing, ref missing);
}

我通过以下方式从我的应用程序中调用:

string pWordDoc = @"C:\Users\Pete-Laptop\Documents\CMS Document Mangement\Word Template.dotx";
cDocument.ReplaceMailMergeField(pWordDoc, "fieldAddress1", "Put address here!");

但是没有任何反应。当我逐步执行代码时,它会到达 app.Documents.Open 然后似乎卡住了。我相信这是因为应用程序找不到我的 Word 文档。我将完整文件路径发送到文件名参数是否正确?如果没有,应用程序还能如何找到我的 Word 模板?

最佳答案

我使用的最终代码如下:

public static void TextToWord(string pWordDoc, string pMergeField, string pValue)
    {
        Object oMissing = System.Reflection.Missing.Value;
        Object oTrue = true;
        Object oFalse = false;
        Word.Application oWord = new Word.Application();
        Word.Document oWordDoc = new Word.Document();
        oWord.Visible = true;
        Object oTemplatePath = pWordDoc;
        oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
        foreach (Word.Field myMergeField in oWordDoc.Fields)
        {
            Word.Range rngFieldCode = myMergeField.Code;
            String fieldText = rngFieldCode.Text;
            if (fieldText.StartsWith(" MERGEFIELD"))
            {
                Int32 endMerge = fieldText.IndexOf("\\");
                Int32 fieldNameLength = fieldText.Length - endMerge;
                String fieldName = fieldText.Substring(11, endMerge - 11);
                fieldName = fieldName.Trim();
                if (fieldName == pMergeField)
                {
                    myMergeField.Select();
                    oWord.Selection.TypeText(pValue);
                }
            }
        }
    }

此代码最初来自here .

关于c# - 在 Microsoft Word 2010 中将文本发送到邮件合并字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10734817/

相关文章:

c# - ASP.NET - 在类库引用中使用 ScriptManager

c# - 在 .NET Core 控制台应用程序中使用 appsettings.ENV.json

.net - 验证签名的 Word 文档的正确方法是什么?

python - 我可以使用 Python 将 MS Word 文档中的软回车转换为硬回车吗?

c# - ChangeLog OriginalValue 与 EF Core 更新实体上的 CurrentValue 相同

C#:如何从 GAC 加载程序集?

c# - 如何在 Windows 窗体中创建半透明或模糊的背景色

c# - 遍历所有 MDI 子级并关闭除当前窗体之外的所有其他窗体

c# - 将带有颜色名称的字符串转换为颜色“id”

正则表达式匹配一些可能的字符串与可能的前导和/或尾随空格