2010-10-06 20:29:03 +0000 2010-10-06 20:29:03 +0000
97
97
Advertisement

どのようにしてWord文書のすべてのフィールドを更新するのですか?

Advertisement

私はWord 2013文書のすべてのフィールドを更新する方法が欲しいです。(他のバージョンでも動作するのであれば、それはそれで良いのですが、私はもともとWord 2007でこの問題を抱えていましたが、それ以来、何も変わっていないようです)。これには相互参照、ページ番号、目次、索引、ヘッダーなどが含まれます。F9を押して更新できるなら更新して欲しい

(理論的には、フィールドを更新すると他のフィールドも更新が必要になる可能性があります。例えば、目次が長くなると本文のページ番号が変わったりします。よくあるケースに注意を払うだけで十分です。実際、マクロが安定するまでに2~3回実行しなければならない場合でも大丈夫です。(1つのマクロで全てを検索できるようにしたいだけです。)

今までの私の試みでは、図形内のテキストボックスのフィールドが更新されませんでした。どのようにしてそれらを更新し、他に何を見落としているのでしょうか?


EDIT . 私がすでに持っていたものと与えられた答えを組み合わせると、すべてを更新するように見えるマクロが得られます(既知の欠陥で)。

Advertisement
Advertisement

回答 (5)

82
82
82
2010-10-06 21:07:05 +0000

私は、Ctrl+A - すべてを選択して、then F9で更新しています。

しかし、これはヘッダーとフッターを見逃していますが、私の記憶では、印刷/印刷プレビューの時に更新されます。軽くテストしてみると、目次、段落内のフィールド、ヘッダーとフッター内のフィールド、フローティングテキストボックスの図内のフィールドが更新されていました。http://www.gmayor.com/installing_macro.htm ](http://www.gmayor.com/installing_macro.htm)

Sub UpdateAll()
    Dim oStory As Range
    For Each oStory In ActiveDocument.StoryRanges
        oStory.Fields.Update
        If oStory.StoryType <> wdMainTextStory Then
            While Not (oStory.NextStoryRange Is Nothing)
                Set oStory = oStory.NextStoryRange
                oStory.Fields.Update
            Wend
        End If
    Next oStory
    Set oStory = Nothing
End Sub
37
37
37
2015-09-15 13:27:49 +0000

印刷設定に入り、フィールドの更新を選択します。その後、印刷に行くか、またはドキュメントのプレビューを印刷します。

4
Advertisement
4
4
2013-07-18 19:24:25 +0000
Advertisement

Word 2010:

リボンを右クリックし、リボンをカスタマイズし、"すべてのコマンド “からコマンドを選択し、"更新 "を検索し、必要な場所に追加します。

このボタンは選択したフィールドのみを更新します。

3
3
3
2015-02-18 19:32:08 +0000

すべてのヘッダとフッタを適切に更新したい場合は、以下のようにしてください:

Dim oStory As Range
    Dim oSection As Object
    Dim oHeader As Object
    Dim oFooter As Object

    For Each oStory In ActiveDocument.StoryRanges
        oStory.Fields.Update
    Next oStory

        For Each oSection In ActiveDocument.Sections
             For Each oHeader In oSection.Headers
                 oHeader.Range.Fields.Update
             Next oHeader

             For Each oFooter In oSection.Footers
                 oFooter.Range.Fields.Update
             Next oFooter
        Next oSection
2
Advertisement
2
2
2016-06-15 20:59:23 +0000
Advertisement

C#の場合:

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Office.Interop.Word;

class Program
{
    static void Main(string[] args)
    {
        List<string> path = new List<string>(args);

        string filePathstr = string.Join(" ", path.ToArray());
        //System.Windows.Forms.MessageBox.Show("filepathstr: " + filePathstr);

        string folderPathstr = Path.GetDirectoryName(filePathstr);
        //System.Windows.Forms.MessageBox.Show("folderPathstr: " + folderPathstr);

        try
        {
            Application ap = new Application();
            Document document = ap.Documents.Open(filePathstr);
            document.Fields.Update();

            foreach (Section section in document.Sections)
            {
                document.Fields.Update(); // update each section

                HeadersFooters headers = section.Headers; //Get all headers
                foreach (HeaderFooter header in headers)
                {
                    Fields fields = header.Range.Fields;
                    foreach (Field field in fields)
                    {
                        field.Update(); // update all fields in headers
                    }
                }

                HeadersFooters footers = section.Footers; //Get all footers
                foreach (HeaderFooter footer in footers)
                {
                    Fields fields = footer.Range.Fields;
                    foreach (Field field in fields)
                    {
                        field.Update(); //update all fields in footers
                    }
                }
            }    

            document.Save();
            document.Close();

        }
        catch (NullReferenceException)
        {
            System.Windows.Forms.MessageBox.Show("A valid file was not selected.");
        }
    }
}
Advertisement

関連する質問

13
3
7
10
10
Advertisement