多个文档(Word 2003 或 2007)使用一个中央页眉/页脚

发布于 2024-08-16 05:16:37 字数 223 浏览 3 评论 0原文

在 Word(2003 或 2007)中,是否有一种方法可以让多个文档使用一个页眉/页脚?

我希望能够在一处更改页眉/页脚并使其影响多个文档。

即我有 50 个文档,它们都有相同的页眉/页脚。有没有办法将 50 个文档链接(OLE?)到一个主文档,而只需更改主文档,而不是打开所有 50 个文档进行更改?

如果没有内置的方法,有人用VBA做过这个吗?

Inside Word (2003 or 2007), is there a way to have one Header/Footer that is used by Multiple documents?

I want to be able to change the header/footer in one spot and have it affect multiple documents.

i.e. I have 50 documents and they all have the same header/footer. Instead of opening all 50 documents to make the change, is there a way to link (OLE?) the 50 documents to a main document and only have to change the main document?

If there is not a built in way, has anyone done this using VBA?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小红帽 2024-08-23 05:16:37

我不确定这在实践中会如何运作,但您可以将其他文件作为链接插入到 Word 文档中。

首先创建包含页眉/页脚内容的文档,内容位于文档正文中。保存起来。

然后转到 50 个文档之一,进入页眉/页脚。转到插入|文件。找到第一个文件,然后单击“插入文件”对话框中“打开”按钮旁边的小下拉箭头。从下拉列表中选择“插入为链接”。内容现在应该显示在文档中。如果您单击内容,通常它会有灰色背景,表明它确实是一个 Word 字段。

现在,当您更改第一个文档时,您可以打开第二个文档,更新字段(单击其中的任意位置并按 F9),新内容将被拉入。您还可以非常简单地以编程方式更新字段,或者在“工具”|“工具”|“工具”下更新字段。选项 |打印,有一个框可以在每次打印文档时自动更新字段。

I'm not sure how will this will work in practice, but you can insert other files into a Word document as a link.

First create the document with the header/footer content, with the content in the body of the document. Save it.

Then go to one of your 50 documents, go into the header/footer. Go to INSERT | FILE. Locate the first file, then click the little drop-down arrow next to the OPEN button in the Insert File dialog. From the drop-down, select INSERT AS LINK. The content should now show up in the document. If you click in the content, normally it will have a grey background, to indicate it's really a Word field.

Now when you change the first document, you can open the second document, update the field (click anywhere in it and hit F9) and the new content will be pulled in. You can also update fields programmatically pretty easy, or under TOOLS | OPTIONS | PRINT, there's a box to auto update the fields every time the document is printed.

别把无礼当个性 2024-08-23 05:16:37

据我所知,(简单地)更改文档标题必须通过打开文档来完成。这就是说你有几个选择。首先,如果文档以 Office XML 格式保存,那么您可以使用 MSXML 库打开文件并更改标题中的数据。 (或者改变本质上是文本文件的数十种其他方法中的任何一种。)如果文件仍然是二进制格式,那么您实际上只有两个选择之一。第一种是通过 vba 打开文件并通过文档对象模型更改标头。第二个是找出二进制格式(已记录)并使用 VB6/VBA 本机二进制 IO 更改它(非常重要)。

除非我认为我可以获得更多时间,否则我会失去编写直接更改文档的代码的机会,否则我可能会循环遍历文件夹中的所有文件,打开它们并更改它们。至于将标题存储在某处......您可以将标题数据放入文本文件中并将其拉入。或者将文档模板保留在某处。

这是一个非常简单的例子:

Public Sub Example()
    Dim asFiles() As String
    Dim lFile As Long
    Dim docCrnt As Word.Document
    asFiles = GetFiles("C:\Test\", "*.doc")
    For lFile = 0& To UBound(asFiles)
        Set docCrnt = Word.Documents.Open(asFiles(lFile))
        docCrnt.Windows(1).View.SeekView = wdSeekCurrentPageHeader
        Selection.Text = "I am the header."
        docCrnt.Close True
    Next
End Sub

Public Function GetFiles( _
    ByVal folderPath As String, _
    Optional ByVal pattern As String = vbNullString _
    ) As String()

    Dim sFile As String
    Dim sFolder As String
    Dim asRtnVal() As String
    Dim lIndx As Long

    If Right$(folderPath, 1&) = "\" Then
        sFolder = folderPath
    Else
        sFolder = folderPath & "\"
    End If
    sFile = Dir(sFolder & pattern)
    Do While LenB(sFile)
        ReDim Preserve asRtnVal(lIndx) As String
        asRtnVal(lIndx) = sFolder & sFile
        lIndx = lIndx + 1&
        sFile = Dir
    Loop
    If lIndx = 0& Then
        ReDim asRtnVal(-1& To -1&) As String
    End If
    GetFiles = asRtnVal
    Erase asRtnVal
End Function

AFAIK to alter a documents header (simply) must be done by having the document open. That said you have a few options. First if the documents are saved in the office XML format then you could open the files using the MSXML library and alter the data in the header. (Or any of the dozens of other ways to alter what is essentially a text file.) If the file(s) are still in the binary format you really only have one of two options. The first is to open the file via vba and alter the header via the document object model. The second would be to figure out the binary format (which is documented) and alter it using the VB6/VBA native binary IO (very non-trivial).

Unless I thought I could gain more time then I was going to lose writing code to alter the documents directly I would probably just loop through all the file in the folder, open them and alter them. As for storing the header somewhere... You could just put the header data in a text file and pull it in. Or keep a document template somewhere.

Here is a very trivial example:

Public Sub Example()
    Dim asFiles() As String
    Dim lFile As Long
    Dim docCrnt As Word.Document
    asFiles = GetFiles("C:\Test\", "*.doc")
    For lFile = 0& To UBound(asFiles)
        Set docCrnt = Word.Documents.Open(asFiles(lFile))
        docCrnt.Windows(1).View.SeekView = wdSeekCurrentPageHeader
        Selection.Text = "I am the header."
        docCrnt.Close True
    Next
End Sub

Public Function GetFiles( _
    ByVal folderPath As String, _
    Optional ByVal pattern As String = vbNullString _
    ) As String()

    Dim sFile As String
    Dim sFolder As String
    Dim asRtnVal() As String
    Dim lIndx As Long

    If Right$(folderPath, 1&) = "\" Then
        sFolder = folderPath
    Else
        sFolder = folderPath & "\"
    End If
    sFile = Dir(sFolder & pattern)
    Do While LenB(sFile)
        ReDim Preserve asRtnVal(lIndx) As String
        asRtnVal(lIndx) = sFolder & sFile
        lIndx = lIndx + 1&
        sFile = Dir
    Loop
    If lIndx = 0& Then
        ReDim asRtnVal(-1& To -1&) As String
    End If
    GetFiles = asRtnVal
    Erase asRtnVal
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文