Create a variable for an excel workbook through Outlook VBA

AnthonyA755

New Member
Joined
Apr 19, 2017
Messages
7
Office Version
  1. 365
  2. 2021
Platform
  1. 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

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
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Use Getobject with the file path. It will grab a reference to the workbook if open, or open it first, if not.
 
Upvote 0
Solution

Forum statistics

Threads
1,223,885
Messages
6,175,179
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top