0

I am getting an error in the below VBA. It is throwing an error on the Set Book = Workbooks.OpenXM(FilePath. Unfortunately, this is not my Code and the guy who wrote it is uncontactable. Can anyone please advise me as to what this line is looking for? The purpose of the Code is to import an XML file into an Excel File.

Sub importITE()

    Dim FilePath As String
    Dim Book As Workbook
    Dim oFSO As Object
    Dim oFolder As Object
    Dim oFile As Object
    Dim i As Integer

'   Load XML Data to New Workbook
    FilePath = "C:\Users\Joebloggs\Documents\Sample Reconciliations\ITE\*.xml"
    Set Book = Workbooks.OpenXML(FilePath)

   'Copy to active Worksheet
    Book.Sheets(1).UsedRange.Copy ThisWorkbook.Sheets("ITE").Range("A1")
    Set oFSO = CreateObject("Scripting.FileSystemObject")


    'Close New Workbook
    Book.Close False
    
    
End Sub
4
  • Workbooks.OpenXML doesn't work with a wildcard in the filepath. You can use Dir if you need to get the actual file name. Commented Feb 2, 2021 at 15:14
  • You need to specify a filename at the OpenXML-command - a wildcard will not work Commented Feb 2, 2021 at 15:14
  • Thank you both for your replies, that is very helpful. Commented Feb 2, 2021 at 16:30
  • How can I write this Dir to do as I would like? Commented Feb 2, 2021 at 16:32

1 Answer 1

1

Here's an outline of how you'd use Dir() to get the exact file name:

Const FPATH As String = "C:\Users\Joebloggs\Documents\Sample Reconciliations\ITE\" 
Dim f, Book

f = Dir(FPATH & "*.xml")
If Len(f) > 0 Then
    Set Book = Workbooks.OpenXML(FPATH & f)

    'other actions
Else
    Msgbox "No xml file found..."
End If
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.