looking up a variable using index/match in an excel file using outlook VBA (code works in Excel but not in Outlook)

nils7

New Member
Joined
Oct 7, 2012
Messages
42
Good evening,

I am trying to write a VBA macro to be used within outlook. The macro picks the recipient from an email within an outlook folder (variable "recip"). I then want to use this variable to look something up in a list in an excel file. The return values for the lookup are in range E3:E870 in the tab "list", and the lookup values are in C3:C870 in the same tab. The lookup result is stored in the variable "fullpath".

I am pasting my code below. When I run this from within Excel VBA it runs correctly. However when I run it in Outlook VBA, I get an error message in the following line:

fullpath = Application.WorksheetFunction.Index(xlWkb.sheets("list").Range("E3:E870")..
. [the error is runtime error 438: Object doesn't support this property or method]

If i remove the xlWkb reference before .Sheets("list") in the same line, the error message i get is Sub or Function not defined

I am using MS Office 2010 on a Windows 7 machine. I can run other macros I have written in Outlook, i am just struggling to integrate Excel files in my VBA within Outlook.

For the purpose of this post i am just declaring a fixed value for "recip".

Thanks a lot for your help in advance.

Cheers,

Nico



Sub lookupinexcel()
Dim fullpath As String​
Dim xlApp As Object​
Dim xlWkb As Object​
Dim recip As String
recip = "A03"​

Set xlApp = CreateObject("excel.application")
xlApp.Visible = True

Set xlWkb = xlApp.Workbooks.Open("C:\Users\Nico\lookup.xlsx")

fullpath = Application.WorksheetFunction.Index(xlWkb.Sheets("list").Range("E3:E870"), Application.WorksheetFunction.Match(recip, xlWkb.Sheets("list").Range("C3:C870"), 0), 1)


MsgBox "looking up " & recip & " has returned the following path: " & fullpath

End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
This question has now been solved.

When using the Application.WorksheetFunction within Outlook, this refers to the Outlook application rather than Excel. That's the reason why the VBA code runs fine in Excel VBE but not in Outlook VBE.
Replacing "Application.WorksheetFunction" with xlApp.WorksheetFunction does the trick, and meanrs that the procedure uses Excel when referring to the WorksheetFunction.

Cheers

Nils
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,725
Members
448,987
Latest member
marion_davis

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