Micah Lankford
Micah Lankford

Reputation: 17

Open an excel workbook after code executes

I know this has been asked before, but for the life of me I cannot get this simple code to work. I keep getting the compile error "User-defined type not defined" on Dim wbopen As Workbook line. I know the workbook has to be open to define it, and the file path should be Z:\Manufacturing\02- Schedules\01- Buffer Prep while the file name is the only .xls in the folder. Why is this happening? Also, this is executing in a Word file. Not sure if that matters. Thanks everyone!

Sub fileopen()

Dim wbopen As Workbook
Dim strFileName As String
Dim strFilePath As String

strFilePath = "Z:\Manufacturing\02- Schedules\01- Buffer Prep\"

strFileName = Dir(strFilePath & "*.xls")

Set wbopen = Workbook.Open(strFileName)

End Sub

Upvotes: 0

Views: 55

Answers (1)

Sobigen
Sobigen

Reputation: 2179

It does matter that it is a Word Document. You have to add a reference to Microsoft Excel Object Library. From the top bar:
Tools -> References -> find Microsoft Excel [version number] Object Library and check it.

Upvotes: 2

Related Questions