通过“For Each”结构打开多个文档。VBA

通过“For Each”结构打开多个文档。VBA,vba,ms-word,Vba,Ms Word,我想要一段代码,允许我打开文件夹中的3个文档-每次打开文档时,我希望有一个显示文件名的消息框。我想通过一个For-Next结构来完成这个循环 我知道application.documents.open filename:=指向文档的链接是我需要使用的代码,但我很难将其组合起来 Option Explicit Sub openthrice() Application.Documents.Open FileName:="C:\Users\John\Desktop\New folder\D

我想要一段代码,允许我打开文件夹中的3个文档-每次打开文档时,我希望有一个显示文件名的消息框。我想通过一个For-Next结构来完成这个循环

我知道application.documents.open filename:=指向文档的链接是我需要使用的代码,但我很难将其组合起来

Option Explicit

Sub openthrice()

    Application.Documents.Open FileName:="C:\Users\John\Desktop\New folder\Doc1.docx"
End Sub

尝试这个作为10的开始,它会打开文件并在一个消息框中显示文件路径,将返回给你进一步简化

Dim StrFile As String

StrFile = Application.GetOpenFilename
Workbooks.Open (StrFile), UpdateLinks:=False
MsgBox StrFile

尝试这个作为10的开始,它会打开文件并在一个消息框中显示文件路径,将返回给你进一步简化

Dim StrFile As String

StrFile = Application.GetOpenFilename
Workbooks.Open (StrFile), UpdateLinks:=False
MsgBox StrFile

此方法需要对Windows脚本主机对象模型的引用。要添加,请从VBA菜单中选择工具和参照。参考文献按字母顺序列出

' Requires reference: Windows Script Host Object Model.
Sub FindFile()
    Const ROOT_DIR As String = "C:\MI\Example"  ' Update with your folder.
    Dim fso As FileSystemObject                 ' Used to read from file system.
    Dim fle As File                             ' Used to loop over files.

    ' Ready object variable for use.
    Set fso = New FileSystemObject

    ' Loop over files.
    For Each fle In fso.GetFolder(ROOT_DIR).Files

        If fle.Name Like "*.docx" Then Application.Documents.Open fle.Name
    Next


    ' Release object vars before they leave scope.
    Set fso = Nothing
End Sub
是一个强大的类,由Microsoft提供。它允许您的代码与Windows文件系统交互

编辑

“未定义用户定义类型”错误意味着编译器无法识别您的数据类型之一。在本例中,它不知道FileSystemObject是什么,因此无法创建该类型的变量fso。要修复它,需要上面提到的参考。这包含了定义,告诉VBA什么是fso,以及它是如何工作的

从菜单条中,您可以单击“工具>>引用”并双击“Windows脚本主机对象模型”复选框。选中的引用应位于顶部,其余的按字母顺序列出

' Requires reference: Windows Script Host Object Model.
Sub FindFile()
    Const ROOT_DIR As String = "C:\MI\Example"  ' Update with your folder.
    Dim fso As FileSystemObject                 ' Used to read from file system.
    Dim fle As File                             ' Used to loop over files.

    ' Ready object variable for use.
    Set fso = New FileSystemObject

    ' Loop over files.
    For Each fle In fso.GetFolder(ROOT_DIR).Files

        If fle.Name Like "*.docx" Then Application.Documents.Open fle.Name
    Next


    ' Release object vars before they leave scope.
    Set fso = Nothing
End Sub

此方法需要对Windows脚本主机对象模型的引用。要添加,请从VBA菜单中选择工具和参照。参考文献按字母顺序列出

' Requires reference: Windows Script Host Object Model.
Sub FindFile()
    Const ROOT_DIR As String = "C:\MI\Example"  ' Update with your folder.
    Dim fso As FileSystemObject                 ' Used to read from file system.
    Dim fle As File                             ' Used to loop over files.

    ' Ready object variable for use.
    Set fso = New FileSystemObject

    ' Loop over files.
    For Each fle In fso.GetFolder(ROOT_DIR).Files

        If fle.Name Like "*.docx" Then Application.Documents.Open fle.Name
    Next


    ' Release object vars before they leave scope.
    Set fso = Nothing
End Sub
是一个强大的类,由Microsoft提供。它允许您的代码与Windows文件系统交互

编辑

“未定义用户定义类型”错误意味着编译器无法识别您的数据类型之一。在本例中,它不知道FileSystemObject是什么,因此无法创建该类型的变量fso。要修复它,需要上面提到的参考。这包含了定义,告诉VBA什么是fso,以及它是如何工作的

从菜单条中,您可以单击“工具>>引用”并双击“Windows脚本主机对象模型”复选框。选中的引用应位于顶部,其余的按字母顺序列出

' Requires reference: Windows Script Host Object Model.
Sub FindFile()
    Const ROOT_DIR As String = "C:\MI\Example"  ' Update with your folder.
    Dim fso As FileSystemObject                 ' Used to read from file system.
    Dim fle As File                             ' Used to loop over files.

    ' Ready object variable for use.
    Set fso = New FileSystemObject

    ' Loop over files.
    For Each fle In fso.GetFolder(ROOT_DIR).Files

        If fle.Name Like "*.docx" Then Application.Documents.Open fle.Name
    Next


    ' Release object vars before they leave scope.
    Set fso = Nothing
End Sub

我甚至不知道该在哪里插入,因为每次运行代码时,我都会自动收到一条错误消息,说明找不到方法或数据成员。我在下一个循环中需要它,我有很多困难。我甚至不知道在哪里插入它,因为每次运行代码时,我都会自动收到一条错误消息,说明找不到方法或数据成员。在下一个循环中我需要它,我有一大堆的困难。请仔细阅读。这个方便的小类公开了您正在寻找的功能。这个方便的小类公开了您正在寻找的功能。谢谢您,但是我收到了一条错误消息,声明为“用户定义类型未定义”,您知道哪一行返回错误吗?是否能够添加引用?Dim fso As FileSystemObject返回错误。未定义用户定义的类型。我可以添加我的引用是的,但我无法通过。该错误通常意味着没有正确添加引用。我将编辑我的答案以包含更多详细信息…谢谢您,但我收到一条错误消息,指出“用户定义类型未定义”,您知道哪行返回错误吗?是否能够添加引用?Dim fso As FileSystemObject返回错误。未定义用户定义的类型。我可以添加我的引用是的,但我无法通过。该错误通常意味着没有正确添加引用。我将编辑我的答案以包含更多细节。。。