尝试这种允许多选的方法。添加到集合后,您可以通过
xDoc1
,
xDoc2
...:
密码
Option Explicit
Dim coDocXML As New Collection
' Declare xDoc variable as of type DOMDocument60
Dim xDoc As MSXML2.DOMDocument60
Sub test_xmlcol()
' Declare a variable to contain the path of each selected item.
' Even though the path is aString, the variable must be a Variant because
' For Each...Next routines only work with Variants and Objects.
Dim vrtSelectedItem As Variant
dim cnt As Integer
'Declare a variable as a FileDialog object.
Dim cnt As Integer
Dim fd As FileDialog
'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)
' Set xDoc to memory
Set xDoc = New MSXML2.DOMDocument60
xDoc.validateOnParse = False
' allow XPath (if DOMDocument vers. 3.0, can omit it if vers. 6.0)
xDoc.setProperty "SelectionLanguage", "XPath"
'Use a With...End With block to reference the FileDialog object.
With fd
'Allow the selection of multiple file.
.AllowMultiSelect = True
'Add a filter that includes GIF and JPEG images and make it the first item in the list.
.Filters.Add "XML-Files", "*.xml", 1
'Use the Show method to display the File Picker dialog box and return the user's action.
'The user pressed the button.
If .Show = -1 Then
'Step through each string in the FileDialogSelectedItems collection
For Each vrtSelectedItem In .SelectedItems
cnt = cnt + 1
'vrtSelectedItem is aString that contains the path of each selected item.
'You can use any file I/O functions that you want to work with this path.
'This example displays the path in a message box.
MsgBox "Selected item's path: " & vrtSelectedItem
coDocXML.Add vrtSelectedItem, "xDoc" & cnt
Next vrtSelectedItem
'The user pressed Cancel.
Else
End If
End With
' Have an informative look at every file
For cnt = 1 To coDocXML.Count
Debug.Print cnt, "xDoc" & cnt & ": " & coDocXML.Item("xDoc" & cnt)
Next cnt
'' load some file and do something via
' cnt = 1 ' only example
' If xDoc.Load coDocXML.Item("xDoc" & cnt) then
'' ... do something
' End If
'Set the object variable to Nothing.
Set fd = Nothing
End Sub