AnthonyA755
New Member
- Joined
- Apr 19, 2017
- Messages
- 7
- Office Version
- 365
- 2021
- Platform
- Windows
Hi All,
I am writing a macro in outlook vba to save attachments in an email to a specific folder, but I need corresponding information from an excel workbook. My code will work if the workbook isn't open (i.e it opens the workbook and does what it needs to do) however when the workbook is already open, I can't work out a way to create a variable that represents the workbook. This is the code I use to open excel, using the worksheet variable to manipulate data within the workbook
Here is the code I have for when the workbook is already open
I get an error on the line where I am setting the variable for the Workbook. I get Run-time error "9": Subscript Out Of Range. I have tried to have the full filepath in the brackets, but that doesn't work either.
How can I create a variable for an excel workbook without opening it first?
Thank you so much in advance, this issue has been bugging me for over a month now
I am writing a macro in outlook vba to save attachments in an email to a specific folder, but I need corresponding information from an excel workbook. My code will work if the workbook isn't open (i.e it opens the workbook and does what it needs to do) however when the workbook is already open, I can't work out a way to create a variable that represents the workbook. This is the code I use to open excel, using the worksheet variable to manipulate data within the workbook
VBA Code:
Dim xExcelFile As String: xExcelFile = "C:\Users\AAntoun\Desktop\QUOTES\QuoteTrackerNew.xlsm" 'create a string variable for the file name of the target workbook
Dim xExcelApp As Excel.Application: Set xExcelApp = CreateObject("Excel.Application") 'create an excel application variable
Dim xWb As Excel.Workbook: Set xWb = xExcelApp.Workbooks.Open(xExcelFile) 'create a workbook variable and open the workbook following the specified file path
Dim xWs As Excel.Worksheet: Set xWs = xWb.Worksheets("Window Quotes") 'create worksheet variable for the target worksheet
'from here, i can use xWs to manipulate data within that worksheet
Here is the code I have for when the workbook is already open
VBA Code:
Dim xExcelApp As Excel.Application: Set xExcelApp = CreateObject("Excel.Application") 'create an excel application variable
Dim xWb As Excel.Workbook: Set xWb = xExcelApp.Workbooks(xExcelFile) 'create a workbook variable - THIS IS WHERE I GET AN ERROR
Dim xWs As Excel.Worksheet: Set xWs = xWb.Worksheets("Window Quotes") 'create worksheet variable for the target worksheet
I get an error on the line where I am setting the variable for the Workbook. I get Run-time error "9": Subscript Out Of Range. I have tried to have the full filepath in the brackets, but that doesn't work either.
How can I create a variable for an excel workbook without opening it first?
Thank you so much in advance, this issue has been bugging me for over a month now