将文本发送到 Microsoft Word 2010 中的邮件合并字段 [英] Sending text to Mail-Merge Fields in Microsoft Word 2010

查看:15
本文介绍了将文本发送到 Microsoft Word 2010 中的邮件合并字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码将文本发送到一个简单的 word 模板,我目前仅使用一个 MergeField 进行设置,以测试我是否可以使其正常工作.
我使用的代码如下:

I'm using the following code to send a text to a simple word template I've set up just with a single MergeField at present to test I can get this working.
The code I am using is as follows:

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);
}

我从我的应用程序中调用以下内容:

Which I call from my application with the following:

string pWordDoc = @"C:UsersPete-LaptopDocumentsCMS Document MangementWord Template.dotx";
cDocument.ReplaceMailMergeField(pWordDoc, "fieldAddress1", "Put address here!");

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

But nothing happens. When I step through the code it gets as far as the app.Documents.Open and then seems to freeze. I believe this is because the application cannot find my Word document. Am I correct in sending the full file path to the filename parameter? If not, how else is the application going to find my Word Template?

推荐答案

我使用的最终代码如下:

The final code I used and which works for me is as follows:

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);
                }
            }
        }
    }

此代码最初来自 这里.

这篇关于将文本发送到 Microsoft Word 2010 中的邮件合并字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆