Word 宏,将文件名添加到文件夹中一系列 Word 文档的正文中

发布于 2025-01-05 02:18:46 字数 431 浏览 3 评论 0原文

我需要一个 Word 宏,将文档的文件名添加到该 Word 文档的第一行。但一次不仅仅是一份文档。我希望宏将这些文件名添加到特定文件夹中的一系列 Word 文档中(每个文档都有自己的文件名)。

将文件名添加到文档的宏很简单:

Selection.HomeKey Unit:=wdStory
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    Text:= "FILENAME  "
Selection.TypeParagraph

但是如何才能将文件名添加到文件夹中的整个文档系列呢?我想该文件夹可以在宏中命名。例如:C:\Users\用户名\Desktop\somefolder\。我还认为可以使用循环来遍历文件夹,直到循环到达文件夹中文档的末尾。

I need a Word macro that adds the file name of a document to the first line of that Word document. But not just one document at a time. I would like the Macro to add these file names to a series of Word documents in a particular folder (with each document getting their own file name).

A Macro that adds the file name to document is simple:

Selection.HomeKey Unit:=wdStory
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    Text:= "FILENAME  "
Selection.TypeParagraph

But how can I get it to add file names to an entire series of documents in a folder? I suppose the folder could be named in the Macro. For example: C:\Users\username\Desktop\somefolder\. I also suppose that a Loop could be used to go through the folder until the loop gets to the end of the documents in the folder.

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

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

发布评论

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

评论(1

微凉 2025-01-12 02:18:46

这至少应该让你开始。我还没有测试过它,但我之前已经写过几次这种类型的东西,所以基本的想法是有效的。

Dim filePath As String
Dim thisDoc As Document
Dim wordApp As Word.Application
Set wordApp = New Word.Application

'wordApp.Visible = True ' uncomment if you want to watch things happen

'Get first file that matches
filePath = Dir("C:\Users\username\Desktop\somefolder\*.doc") ' or *.whatever
Do While filePath <> ""
    Set thisDoc = wordApp.Documents.Open(filePath)

    'Add filename at top of document
    Selection.HomeKey Unit:=wdStory
    Selection.InsertAfter filePath
    'Selection.InsertAfter ThisDocument.FullName ' alternative
    'Your way using .Fields.Add is also fine if you want the file name as 
    ' a field instead of 
    ' plain text.

    thisDoc.Close SaveChanges:=True

    'Get next file that matches and start over
    filePath = Dir()
Loop

wordApp.Quit
Set wordApp = Nothing

This should at least get you started. I haven't tested it, but I've written this type of thing a few times before, so the basic idea works.

Dim filePath As String
Dim thisDoc As Document
Dim wordApp As Word.Application
Set wordApp = New Word.Application

'wordApp.Visible = True ' uncomment if you want to watch things happen

'Get first file that matches
filePath = Dir("C:\Users\username\Desktop\somefolder\*.doc") ' or *.whatever
Do While filePath <> ""
    Set thisDoc = wordApp.Documents.Open(filePath)

    'Add filename at top of document
    Selection.HomeKey Unit:=wdStory
    Selection.InsertAfter filePath
    'Selection.InsertAfter ThisDocument.FullName ' alternative
    'Your way using .Fields.Add is also fine if you want the file name as 
    ' a field instead of 
    ' plain text.

    thisDoc.Close SaveChanges:=True

    'Get next file that matches and start over
    filePath = Dir()
Loop

wordApp.Quit
Set wordApp = Nothing
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文