Add If condition in footer using Word Interop in C#

痴心易碎 提交于 2019-12-13 07:23:52

问题


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

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.

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

condition is:

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

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();

回答1:


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.

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;
    }


来源:https://stackoverflow.com/questions/34936032/add-if-condition-in-footer-using-word-interop-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!