本文介绍了在C#中使用Word Interop在页脚中添加If条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio Express 2015和Word Interop 14.0.

I am using visual studio express 2015 and word interop 14.0.

我需要使用C#中的Word Interop在Word文档最后一页的页脚中添加If条件.我在其他论坛中搜索了代码,但无法在C#中运行它.请帮忙.

I need to add a If condition in the footer of the last page of a word document using Word Interop in C#. I searched for the code and also in other forums, but couldn't get it work in C#. Please help.

我的问题是如何在页脚部分添加IF条件,使其仅浸入最后一页.

My question is how to add a IF condition in the footer section so that it only dipslays in the last page.

条件是:

if page = numpages then "Last Page Footer Text" else "Other page footer text"

我使用了下面的代码,但是它显示在所有页面中,并且if条件也显示在页脚中.

I used the below code, but it displays in all the page and also the if condition appears in the footer.

object fieldPages = WdFieldType.wdFieldPage;
object fieldNumPages = WdFieldType.wdFieldNumPages;
object fieldMerge = WdFieldType.wdFieldMergeField;
object fieldAuthor = WdFieldType.wdFieldAuthor;
object fieldIF = WdFieldType.wdFieldIf;
object collapseDirection = WdCollapseDirection.wdCollapseStart;
object txt = string.Empty;
var field = Rng.Fields.Add(Rng, ref fieldAuthor, ref txt, true);
Rng.InsertAfter("\"");
Rng.InsertBefore("\"");
Rng.Collapse(ref collapseDirection);

oDoc.Fields.Add(Rng, ref fieldNumPages, ref txt, true);
Rng.InsertBefore(" = ");
Rng.Collapse(ref collapseDirection);

oDoc.Fields.Add(Rng, ref fieldPages, ref txt, true);
Rng.InsertBefore(" IF ");
Rng.Collapse(ref collapseDirection);
oWord.ActiveWindow.ActivePane.View.ShowFieldCodes = true;
field.Update();

推荐答案

使用对象模型创建嵌套字段很复杂-其中没有任何东西可以简化此过程.尝试通过创建最里面的字段,选择它们,然后插入字段括号来模仿UI,这有点棘手,必须为每种组合编写代码.

Creating nested fields is complicated using the object model - there's nothing in it to facilitate the process. Trying to mimic the UI by creating the innermost field(s), selecting them, then inserting field brackets is a bit tricky and code for every combination must be written.

使用对象模型,创建最外层的字段写入占位符以使字段嵌套在其中更有意义.然后,Word的Range.Find功能可以选择占位符,并在其位置插入域代码.

Using the object model, it makes more sense to create the outermost field writing placeholders for the fields to nest within it. Then Word's Range.Find functionality can pick up the placeholders and insert field codes in their place.

下面是一些示例代码,用于创建您描述的条件页脚文本:

Here's some sample code to create the conditional footer text you describe:

    //Returns the changed field code
    private string GenerateNestedField(Word.Field fldOuter,
                         string sPlaceholder)
    {
        Word.Range rngFld = fldOuter.Code;
        Word.Document doc = (Word.Document) fldOuter.Parent;
        bool bFound;
        string sFieldCode;

        //Get the field code from the placeholder by removing the { }
        sFieldCode = sPlaceholder.Substring(1, sPlaceholder.Length - 2); //Mid(sPlaceholder, 2, Len(sPlaceholder) - 2)
        rngFld.TextRetrievalMode.IncludeFieldCodes = true;
        bFound = rngFld.Find.Execute(sPlaceholder);
        if (bFound) doc.Fields.Add(rngFld, Word.WdFieldType.wdFieldEmpty, sFieldCode, false);

        return fldOuter.Code.ToString();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        getWordInstance(); //Object defined as a class member for Word.Application
        Word.Document doc = wdApp.ActiveDocument;
        Word.View vw = doc.ActiveWindow.View;
        Word.Range rngTarget = null;
        Word.Field fldIf = null;
        string sIfField, sFieldCode;
        string sQ = '"'.ToString();
        bool bViewFldCodes = false;

        sIfField = "IF {Page} = {NumPages} " + sQ + "Last" + sQ + " " + sQ + "Other" + sQ;

        rngTarget = doc.Sections[1].Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
        bViewFldCodes = vw.ShowFieldCodes;
        //Finding text in a field codes requires field codes to be shown
        if(!bViewFldCodes) vw.ShowFieldCodes = true;

        //Create the nested field: { IF {Pages} = {NumPages} "Last" "Other" }
        fldIf = doc.Fields.Add(rngTarget, Word.WdFieldType.wdFieldEmpty, sIfField, false);
        sFieldCode = GenerateNestedField(fldIf, "{Page}");
        sFieldCode = GenerateNestedField(fldIf, "{NumPages}");
        rngTarget.Fields.Update();
        vw.ShowFieldCodes = bViewFldCodes;
    }

这篇关于在C#中使用Word Interop在页脚中添加If条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 13:12