在邮件合并期间不会保留文档格式



我正在尝试自动化MailMerge,并设法使其几乎完成。我可以生成单个文档并合并它们。问题是在"InsertFile"调用期间,文档被重新格式化,并且会破坏文档。因为文档是一封信,并且地址信息必须打印在同一位置,所以我一直卡在我能更正格式之前。我正在使用的功能如下。如果有人能指出我正确的方向,我将不胜感激。在一天的搜索中,我无法在网上找到任何东西。

    public bool mailMergeWithTemplate(string strTemplatePath, string strDestinationPath, DataSet dsData, out string strError)
    {
        strError = "";
        object objMissing = Missing.Value; //null value
        object objTrue = true;
        object objFalse = false;
        object objTemplatePath = strTemplatePath;
        object objOutputPath = strDestinationPath;
        object objOutputPathTemp = strDestinationPath.Substring(0, strDestinationPath.IndexOf(".docx")) + "_temp.docx";
        object sectionStart = WdSectionStart.wdSectionNewPage;
        Application objWord = null;
        try
        {
            objWord = new Application { Visible = true };
            //create an empty document into which we will insert all the merge docs
            var objFinalWordDoc = objWord.Documents.Add(ref objMissing, ref objMissing, ref objMissing, ref objMissing);
            //for each record in the dataset
            var count = 1;
            foreach (DataRow dr in dsData.Tables[0].Rows)
            {
                //insert a document for each record
                var objWordDoc = objWord.Documents.Add(ref objTemplatePath, ref objMissing, ref objMissing, ref objMissing);
                if (objWordDoc.Fields.Count == 0)
                    return false;
                objWordDoc.Activate();
                // Perform mail merge of each field
                foreach (Field myMergeField in objWordDoc.Fields)
                {
                    var rngFieldCode = myMergeField.Code;
                    var strFieldText = rngFieldCode.Text;
                    // ONLY GET THE MAILMERGE FIELDS
                    if (!strFieldText.StartsWith(" MERGEFIELD")) continue;
                    var strFieldName = string.Empty;
                    if (!strFieldText.Contains("Account"))
                    {
                        var intEndMerge = strFieldText.IndexOf("\");
                        strFieldName = strFieldText.Substring(11, intEndMerge - 11);
                    }
                    else
                        strFieldName = "Account";
                    strFieldName = strFieldName.Trim().Replace(""", "");
                    //find a matching dataset column
                    foreach (DataColumn col in dsData.Tables[0].Columns)
                    {
                        var strKey = col.ColumnName;
                        var strValue = dr[strKey].ToString();
                        if (strFieldName != strKey) continue;
                        myMergeField.Select();
                        objWord.Selection.TypeText(strValue);
                    }
                }
                //SAVE THE DOCUMENT as temp
                objWordDoc.SaveAs(ref objOutputPathTemp, ref objMissing, ref objMissing, ref objMissing,
                                    ref objMissing, ref objMissing, ref objMissing, ref objMissing,
                                    ref objMissing, ref objMissing, ref objMissing, ref objMissing,
                                    ref objMissing, ref objMissing, ref objMissing, ref objMissing);
                //CLOSE THE DOCUMENT
                objWordDoc.Close(ref objFalse, ref objMissing, ref objMissing);
                Marshal.ReleaseComObject(objWordDoc);
                objWordDoc = null;
                //NOW ADD THE NEW DOC TO THE MAIN DOC
                objFinalWordDoc.Activate();
                objWord.Selection.InsertFile(objOutputPathTemp.ToString(),
                                                ref objMissing, ref objMissing, ref objMissing, ref objMissing);
                if(count < dsData.Tables[0].Rows.Count)
                    objWord.Selection.InsertBreak(ref sectionStart);
                count++;
            }
            //SAVE THE FINAL DOC
            objFinalWordDoc.SaveAs(ref objOutputPath, ref objMissing, ref objMissing, ref objMissing,
                                    ref objMissing, ref objMissing, ref objMissing, ref objMissing,
                                    ref objMissing, ref objMissing, ref objMissing, ref objMissing,
                                    ref objMissing, ref objMissing, ref objMissing, ref objMissing);
            //CLOSE THE FINAL DOC
            objFinalWordDoc.Close(ref objFalse, ref objMissing, ref objMissing);
            Marshal.ReleaseComObject(objFinalWordDoc);
            objFinalWordDoc = null;
            //now delete the temp file
            File.Delete(objOutputPathTemp.ToString());
            return true;
        }
        catch (Exception ex)
        {
            strError = ex.Message;
        }
        finally
        {
            //RELEASE WORD ITSELF
            if (objWord != null)
            {
                objWord.Quit(ref objMissing, ref objMissing, ref objMissing);
                Marshal.ReleaseComObject(objWord);
            }
            objWord = null;
            GC.Collect();
        }
        return false;
    }

问题出现在此处:

objWord.Selection.InsertFile(objOutputPathTemp.ToString(),
                                                ref objMissing, ref objMissing, ref objMissing, ref objMissing);

那么有人对我如何纠正这个问题有任何想法吗?

假设您可以控制插入文件的内容,

  • 确保插入的文件没有分节符。
  • 选择插入文件的全部内容,但最终内容除外段落标记("携带"节/文档格式信息)。假设您将书签称为"内容"
  • 指定"theContent"作为"范围"参数(即第二个参数)的插入文件调用

最新更新