0

1行で構成されるWord文書にフッターをワンクリックで追加する方法を実装する必要があります。最初の部分はドキュメントへの絶対パスである必要があり、それは左にバインドされている必要があります。これに加えて、実際のページ番号を右揃えにする必要があります。

これはExcelでは問題ではありませんでした。そこで、LeftFooter、CenterFooter、RightFooterを使用できます。ただし、Wordでは、アクセスするそのようなプロパティはありません。

編集:いくつかのバグがあり、適切な方法がまだ見つからなかったために適切に設計されていない、半ば機能するソリューションを見つけました。

Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
        foreach (Word.Section wordSection in doc.Sections)
        {
            Word.Range PageNumberRange = wordSection.Range;
            PageNumberRange.Fields.Add(PageNumberRange, Word.WdFieldType.wdFieldEmpty ,"PAGE  Arabic ", true);


            Word.Range footer = wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
            footer.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
            footer.Tables.Add(footer, 1, 3);

            Word.Table tbl = footer.Tables[1];

            tbl.Cell(1, 1).Range.Text = doc.FullName;
            tbl.Cell(1, 3).Range.Text = PageNumberRange.Text;
            /**/

            footer.Font.ColorIndex = Word.WdColorIndex.wdBlack;
            footer.Font.Size = 6;

            PageNumberRange.Text = "";

これの問題は次のとおりです。既存のフッターを上書きすることはありません。「document1...1」と書かれていて、もう一度クリックすると、ドキュメントを保存したため、フッターが変更されません。さらに:複数のページがある場合、ページ1を除くすべてのページが削除されます。

こんなに簡単な作業を実行するのがこんなに難しいとは想像もしていませんでした。

4

1 に答える 1

0

スタイルを使用した代替アプローチ

Document doc = this.application.ActiveDocument;
Section wordSection = doc.Sections[1];    
Range footer = wordSection.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;                              
footer.Fields.Add(footer, WdFieldType.wdFieldEmpty, @"PAGE \* ARABIC", true);                               
footer.Collapse(WdCollapseDirection.wdCollapseStart);
footer.InsertBefore("\t \t");
footer.InsertBefore(doc.FullName);                                                          
footer.Font.Name = "Arial";
于 2013-03-04T10:39:38.417 に答える