0

我正在尝试自动化 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);

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

4

1 回答 1

0

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

  • 确保插入的文件没有分节符。
  • 选择插入文件的全部内容,除了最后的段落标记(“携带”部分/文档格式信息)。假设您将书签称为“theContent”
  • 将“theContent”指定为 InsertFile 调用的“范围”参数(即第二个参数)
于 2013-09-20T08:59:31.607 回答