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

如何更新Word文档中的所有字段?

Advertisement

我想在Word 2013文档中更新所有字段。(如果它在其他版本中可以使用,那就更好了;我最初在Word 2007中遇到这个问题,后来似乎没有什么变化。) 这包括交叉引用、页码、目录表、索引、标题等。如果可以按F9键更新的话,我希望它能更新。

(理论上更新字段会导致其他字段需要更新,比如较长的目录表会改变正文中的一些页码。照顾到这些常见的情况,对我来说已经很好了。事实上,如果我需要运行两三次宏才会稳定下来,也是可以的。我只希望有一个宏可以找到所有的东西。)

我的尝试到现在为止没有更新数字内的文本框中的字段。 结合我已经给出的答案,我已经有了一个宏,似乎可以更新一切(有一个已知缺陷)。

Advertisement
Advertisement

答案 (5)

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

我只需按Ctrl+A键—选择所有的东西–然后按F9键更新。

虽然这样做会错过页眉和页脚,但当你打印/打印-预览时,它们会更新。在快速测试中,它更新了目录表,段落内的字段,页眉和页脚内的字段,以及浮动文本框内的字段。 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

相关问题

14
13
3
10
7
Advertisement
Advertisement