【问题标题】:VBA Word - Nested IF Function, A Macro Running Other Macros in all Documents in a FolderVBA Word - 嵌套 IF 函数,一个在文件夹中的所有文档中运行其他宏的宏
【发布时间】:2018-09-16 06:28:12
【问题描述】:

我是编码初学者。所以我想知道如何使用嵌套来编写一个宏(用于 VBA Word),该宏在指定文件夹中的所有文档中运行多个其他宏。我试图通过让外部循环打开文件夹中的所有文档来使用嵌套(用户将使用 InputBox 输入文件夹的位置),并且在此循环中,将应用所有宏。

到目前为止,我知道这是完美的(代码打开了指定文件夹中的所有文档);

Sub nestingMacro()

    Dim currentFile As String
    Dim location As String
    location = InputBox("Location of folder")
    If Right(location, 1) <> "\" Then location = location + "\"
    currentFile = Dir(location & "*.doc*")
    Do While (currentFile <> "")
        Documents.Open FileName:=location & currentFile
        currentFile = Dir()
    Loop

End Sub

我尝试添加以下内容;

Sub nestingMacro()

    Dim currentFile As String
    Dim location As String
    location = InputBox("Location of folder")
    If Right(location, 1) <> "\" Then location = location + "\"
    currentFile = Dir(location & "*.doc*")
    Do While (currentFile <> "")
        Documents.Open FileName:=location & currentFile
        currentFile = Dir()

    If currentFile <> "" Then

    'the name of the macros below
    Call findReplaceStyle
    Call countErrorsQuality
    Call saveClose

    End If

    Loop

End Sub

是的,它会打开一个文件夹中的所有文档,但是,它只在其中两个文档上运行宏,而其他文档则没有任何反应。我该如何解决这个问题?

是否有更好的方法来编写函数 IF,以便使用嵌套在所有文档上运行宏?

还有没有办法运行宏而不实际调用它们的名字?

谢谢!

【问题讨论】:

  • 我们不明白您所说的“嵌套”是什么意思。请解释。 FWIW 我怀疑您可能遇到了竞争情况 - 打开文件的代码可能正在执行,而无需等待宏在每个文档上运行/完成。我也不知道你最后一个问题的确切含义,但你可以(并且可能应该)省略关键字Call - VBA 不需要它。但是您确实需要使用宏的名称。
  • 我所说的嵌套是指当一个结构包含在另一个结构中时——(对于 VBA)是指代码在彼此内部具有 IF 分支和 Do-While 循环。所以......在Do-While中,您可能有一个IF分支,或者在IF中您可能有一个Do-While,或者Do-while中的Do-While,或者IF中的IF。谢谢!
  • 测试一下:在 Loop 行放一个断点,这样代码每次碰到这一行就停止。每次只需按 F5 即可。那么事情是否如你所愿?如果是,那么在宏“全速”运行时,在打开下一个文档之前,各种调用都没有完成。要缩小范围,您需要注释掉代码,直到找到为止。

标签: vba function if-statement ms-word


【解决方案1】:

你不需要 if 语句。

Sub nestingMacro()

    Dim currentFile As String
    Dim location As String
    location = InputBox("Location of folder")
    If Right(location, 1) <> "\" Then location = location + "\"
    currentFile = Dir(location & "*.doc*")
    Do While (currentFile <> "")
        Documents.Open FileName:=location & currentFile


        Call findReplaceStyle
        Call countErrorsQuality
        Call saveClose

        currentFile = Dir()

    Loop

End Sub

【讨论】:

  • 无法得到您的确切要求。
【解决方案2】:

到目前为止,您不需要嵌套您所描述的内容。如果代码仅在文件夹中的几个文件上运行,那很可能是因为您正在从存储在同一文件夹中的文档中运行代码,并且一旦它自行处理,它就会被关闭并杀死宏。尝试以下方式。

Sub Demo()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, strDocNm As String, wdDoc as Document 
strDocNm = ActiveDocument.Fullname
strFolder = GetFolder
If strFolder = "" Then Exit Sub 
strFile = Dir(strFolder & "\*.doc*", vbNormal)
While strFile <> ""
  If strFolder & "\" & strFile <> strDocNm Then
    Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
    Call findReplaceStyle
    Call countErrorsQuality
    Call saveClose
  End If
  strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub

Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function

您的潜在问题

Call findReplaceStyle
Call countErrorsQuality

lines 是您没有将刚刚打开的文档作为参数传递。如果这些 subs 中的任何内容更改了 activedocument,您可能会遇到问题。将要处理的文档作为参数传递是一种很好的编码习惯,如下所示:

Call findReplaceStyle(wdDoc)
Call countErrorsQuality(wdDoc)

借鉴您之前关于相关主题的线程,接受和使用这样的参数,后一个子可能会按照以下方式编码:

Sub countErrorsQuality(wdDoc As Document)
Dim Rng As Range
With wdDoc
  Set Rng = .Range(0, 0)
  If .SpellingErrors.Count > 0 Then
    With Rng
      .Text "REJECTED" & vbCr
      .Font.Size = 14
      .Font.ColorIndex = wdRed
      .Font.Bold = True
    End With
  End If
End With
Set Rng = Nothing
End Sub

请注意,没有选择任何内容。这减少了屏幕闪烁,使代码运行得更快。

我同样怀疑你不需要你的

Call saveClose

代码和你真正需要的是:

wdDoc.Close SaveChanges:=True

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-19
    • 2019-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-16
    • 1970-01-01
    相关资源
    最近更新 更多