VBA批量更新带有Excel链接的MS Word文件的文件夹

发布于 2025-01-09 10:17:23 字数 2807 浏览 0 评论 0原文

我有一个充满 MS Word 文档的文件夹,所有文档都具有相同的标题,其中包含几个链接到 Excel 文件的字段,用于在一个位置控制项目阶段和发布日期。

我正在尝试找出一种方法,使用 VBA 循环遍历此文件夹中的所有单词文档,打开它们,更新字段,保存并关闭以避免逐一遍历并手动执行。

这里是 VBA 的新手,不太确定我在做什么(或做错了什么)。这是我迄今为止根据我看到的与此任务相关的响应拼凑而成的代码。对于如何改进/解决问题的任何帮助,我们表示赞赏。如果有帮助的话,很乐意提供更多信息。

在“Set oWordDoc = oWordApp.Documents.Open(sFileName)”行上收到错误“未设置对象变量或 With 块变量”

谢谢!

更新:感谢大家的帮助,下面添加了工作代码。

Sub Sample()
Dim oWordApp As Object, oWordDoc As Object, rngStory As Object
Dim sFolder As String, strFilePattern As String
Dim strFileName As String, sFileName As String

'> Change this to the folder which has the files
sFolder = Dir(Range("A20").Value)

'> This is the extention you want to go in for
strFilePattern = "*.doc"

'> Establish Word application object
On Error Resume Next
Set oWordApp = GetObject(, "Word.Application")

If Err.Number <> 0 Then
    Set oWordApp = CreateObject("Word.Application")
End If
Err.Clear
On Error GoTo 0

oWordApp.Visible = True

'> Loop through the folder to get the word files
strFileName = Dir$(sFolder & strFilePattern)
Do Until strFileName = ""
    sFileName = sFolder & strFileName

    '> Open the word doc
    Set oWordDoc = oWordApp.Documents.Open(sFileName)

    '> Update Fields
   
     oWordDoc.Fields.Update
        


    '> Close the file after saving
    oWordDoc.Close SaveChanges:=True

    '> Find next file
    strFileName = Dir$()
Loop

'> Quit and clean up
oWordApp.Quit

Set oWordDoc = Nothing
Set oWordApp = Nothing
End Sub

更新的工作代码:

Sub UpdateSpecHeaders()
Dim oWordApp As Object, oWordDoc As Object
Dim sFolder As String, strFilePattern As String
Dim strFileName As String, sFileName As String

'> Folder containing files to update
sFolder = Range("A20").Value


    '> Identify file extension to search for
strFilePattern = "*.doc"


'> Establish a Word application object
On Error Resume Next
Set oWordApp = GetObject(, "Word.Application")

If Err.Number <> 0 Then
    Set oWordApp = CreateObject("Word.Application")
End If
Err.Clear
On Error GoTo 0

oWordApp.Visible = True


'> Loop through the folder to get the word files
strFileName = Dir$(sFolder & strFilePattern)
Do Until strFileName = ""
    sFileName = sFolder & strFileName

    '> Open the word doc
    Set oWordDoc = oWordApp.Documents.Open(sFileName)
    
    Application.DisplayAlerts = False


    '> Update Fields
    oWordApp.ActiveDocument.Fields.Update
     

    '> Save and close the file
    oWordDoc.Save
    oWordDoc.Close SaveChanges:=True
    
        
    '> Find next file
    strFileName = Dir$()
Loop

'> Quit and clean up
oWordApp.Quit

Set oWordDoc = Nothing
Set oWordApp = Nothing

End Sub

I've got a folder full of MS word docs, all with the same header, containing a couple of fields linked to an excel file to control the project phase and issue date in one spot.

I'm trying to figure out a way to use VBA to loop through all the word docs in this folder, opening them, updating the fields, saving and closing to avoid going through one by one and doing it manually.

Brand new to VBA here and not quite sure what I'm doing (or doing wrong). Here's the code I've pieced together so far based on responses I've seen related to this task. Any help is appreciated on how to improve this/tackle the problem. Happy to provide more info if it helps.

Receiving error "Object variable or With block variable not set" on line "Set oWordDoc = oWordApp.Documents.Open(sFileName)"

Thanks!

Update: Thank you everyone for the help, working code added below.

Sub Sample()
Dim oWordApp As Object, oWordDoc As Object, rngStory As Object
Dim sFolder As String, strFilePattern As String
Dim strFileName As String, sFileName As String

'> Change this to the folder which has the files
sFolder = Dir(Range("A20").Value)

'> This is the extention you want to go in for
strFilePattern = "*.doc"

'> Establish Word application object
On Error Resume Next
Set oWordApp = GetObject(, "Word.Application")

If Err.Number <> 0 Then
    Set oWordApp = CreateObject("Word.Application")
End If
Err.Clear
On Error GoTo 0

oWordApp.Visible = True

'> Loop through the folder to get the word files
strFileName = Dir$(sFolder & strFilePattern)
Do Until strFileName = ""
    sFileName = sFolder & strFileName

    '> Open the word doc
    Set oWordDoc = oWordApp.Documents.Open(sFileName)

    '> Update Fields
   
     oWordDoc.Fields.Update
        


    '> Close the file after saving
    oWordDoc.Close SaveChanges:=True

    '> Find next file
    strFileName = Dir$()
Loop

'> Quit and clean up
oWordApp.Quit

Set oWordDoc = Nothing
Set oWordApp = Nothing
End Sub

Updated Working Code:

Sub UpdateSpecHeaders()
Dim oWordApp As Object, oWordDoc As Object
Dim sFolder As String, strFilePattern As String
Dim strFileName As String, sFileName As String

'> Folder containing files to update
sFolder = Range("A20").Value


    '> Identify file extension to search for
strFilePattern = "*.doc"


'> Establish a Word application object
On Error Resume Next
Set oWordApp = GetObject(, "Word.Application")

If Err.Number <> 0 Then
    Set oWordApp = CreateObject("Word.Application")
End If
Err.Clear
On Error GoTo 0

oWordApp.Visible = True


'> Loop through the folder to get the word files
strFileName = Dir$(sFolder & strFilePattern)
Do Until strFileName = ""
    sFileName = sFolder & strFileName

    '> Open the word doc
    Set oWordDoc = oWordApp.Documents.Open(sFileName)
    
    Application.DisplayAlerts = False


    '> Update Fields
    oWordApp.ActiveDocument.Fields.Update
     

    '> Save and close the file
    oWordDoc.Save
    oWordDoc.Close SaveChanges:=True
    
        
    '> Find next file
    strFileName = Dir$()
Loop

'> Quit and clean up
oWordApp.Quit

Set oWordDoc = Nothing
Set oWordApp = Nothing

End Sub

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文