Use Outlook VBA to manipulate data in an open Excel workbook

AnthonyA755

New Member
Joined
Apr 19, 2017
Messages
7
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi All,

I seem to have hit a brick wall with my code. The main intention of what I am trying to achieve is run a macro in Outlook that saves attachments into a new folder, and the name for that folder and the files need to be based off information in some cells within an excel workbook. Currently, It works if the workbook is not open - here is the working code for that (note that I have declared the variables in two instances in the below examples just for ease of understanding this post, my actual code declares the variables once and sets them separately)

VBA Code:
Dim xExcelFile As String: xExcelFile = "C:\Users\AAntoun\Desktop\QUOTES\QuoteTrackerNew.xlsm"
Dim xExcelApp As Excel.Application: Set xExcelApp = CreateObject("Excel.Application")
Dim xWb As Excel.Workbook: Set xWb = xExcelApp.Workbooks.Open(xExcelFile)
Dim xWs As Excel.Worksheet: Set xWs = xWb.Worksheets("Window Quotes")
'from here, i can use xWs to manipulate data within that worksheet

The part that I cannot get to work is when the workbook is already open. Here is the code I am using for that

VBA Code:
Dim xExcelApp As Excel.Application: Set xExcelApp = CreateObject("Excel.Application")
Dim xWb As Excel.Workbook: Set xWb = xExcelApp.Workbooks(xExcelFile)
Dim xWs As Excel.Worksheet: Set xWs = xWb.Worksheets("Window Quotes")

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.

I guess, in summary, all I am asking is how can I declare a variable for an open Excel Workbook through Outlook Vba? I am sure I am making a simple mistake somewhere but my rummaging through the internet has returned nothing of value.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
[Deleted because has already been dealt with at cross-post]
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,203
Members
452,617
Latest member
Narendra Babu D

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