1

我想使用现有的 word 文档模板创建一个 word 文档。所以我尝试这样

private void CreateWordDocument(object fileName,
                                object saveAs)
{
    object missing = System.Reflection.Missing.Value;           
    var wordApp = new Application();         
    Microsoft.Office.Interop.Word.Document aDoc = null;

    if (File.Exists((string)fileName))
    {
        DateTime today = DateTime.Now;

        object readOnly = false;
        object isVisible = false;

        //Set Word to be not visible.
        wordApp.Visible = false;

        //Open the word document
        aDoc = wordApp.Documents.Open(ref fileName, ref missing,
            ref readOnly, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing,
            ref missing, ref isVisible, ref missing, ref missing,
            ref missing, ref missing);

        aDoc.Activate();

        this.FindAndReplace(wordApp, "<Name>", txtName.Text);
        this.FindAndReplace(wordApp, "<company>", txtcompanyName.Text);
    }
    else
    {
        return;
    }

    //Save the document as the correct file name.
    aDoc.SaveAs(ref saveAs, 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);

    //Close the document - you have to do this.
    aDoc.Close(ref missing, ref missing, ref missing);
}

在我的模板页脚和页眉部分有一些变量。但我不知道如何使用变量替换它..

我的标题是这样的。 在此处输入图像描述

怎么做?

4

1 回答 1

1

甚至我也有编辑现有 word 模板(包括页眉和页脚)的相同要求。这是我使用的解决方案,效果很好。

要替换单词模板中的单词:

    string destFile = Server.MapPath("~/Letters/sample.docx");
    object fileName = destFile; 
    Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application { Visible = false };
    Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(fileName, ReadOnly: false, Visible: false);
    object Unknown = Type.Missing;
    aDoc.Activate();
    try
    {
     //Replace any text inside the word document
     FindAndReplace(wordApp, "[Date]", DateTime.Now.ToString("MMMM dd, yyyy"));
    }

要替换单词模板页脚中的单词:

    object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
    foreach (Microsoft.Office.Interop.Word.Section section in aDoc.Sections)
                {
                    Microsoft.Office.Interop.Word.HeadersFooters footers = section.Footers;
                    foreach (Microsoft.Office.Interop.Word.HeaderFooter footer in footers)
                    {
                        Range footerRange = footer.Range;
                        footerRange.Find.ClearFormatting();
                        footerRange.Find.Replacement.ClearFormatting(); 
                        footerRange.Find.Text = "[Vendor Specialist Phone]";
                        footerRange.Find.Replacement.Text = ds.Tables[1].Rows[0]["phoneNumber"].ToString();
                        footerRange.Find.Wrap = WdFindWrap.wdFindContinue;
                        footerRange.Find.Execute(ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref replaceAll, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
                    }
                }
            aDoc.SaveAs(ref fileName,
            ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
            ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
            ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);

            }
            catch (Exception ex)
            {
            }
            finally
            {
                //Close the document
                aDoc.Close(WdSaveOptions.wdSaveChanges);
                //Close the instance of WINWORD.EXE
                ((_Application)wordApp).Quit(ref Unknown, ref Unknown, ref Unknown);
            }

要替换页眉,它与页脚相同。只需使用 section.Headers 而不是 section.Footers。休息一切都一样。

这工作正常,没有任何问题。

谢谢你

于 2017-09-14T15:23:52.747 回答