将文本发送到Microsoft Word 2010中的邮件合并字段

时间:2012-05-24 09:35:28

标签: 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模板?

2 个答案:

答案 0 :(得分:4)

我使用的最终代码对我有用,如下:

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

答案 1 :(得分:3)

您可以尝试:

_doc = _app.Documents.Add(ref _docName , ref _missing , ref _missing , ref _missing );

而不是

_doc = _app.Documents.Open(.......);

然后检查此行是否正确:

if (f.Code.Text.IndexOf("MERGEFIELD  \"" + pMergeField + "\"") > -1) 

我有一个以这种方式运作的功能正常的代码

        mailMerge = doc.MailMerge;         
        foreach (Word.MailMergeField f in mailMerge.Fields)         
        {    
             // Extract the name of the MergeField starting from the 11 character
             // and looking for the first space after the name 
             // (this means that you avoid MergeFields names with embedded spaces)
             string fieldName = f.Code.Text.Substring(11).Trim();
             int  pos = fieldName.IndexOf(' ');
             if (pos >= 0) fieldName = fieldName.Substring(0, pos).Trim();

             if (fieldName == pMergeField) 
             {
                   f.Select();                  
                   app.Selection.TypeText(pValue);  
             }