Outlook - Searching Excel and returning offset value

frateg8r

Board Regular
Joined
Mar 2, 2005
Messages
221
Office Version
  1. 365
Platform
  1. Windows
I have a script for searching an Excel sheet for an input value, which, if found, returns the file number associated with it, using
VBA Code:
sfilenum = rngFound.Offset(0, 0).EntireRow.Range("A1").Value
.

This code works well from Excel, but I found that when trying to use it from Outlook, I run into a compile error:
Method or Data Member Not Found
with reference to the .offset (item? I'm not sure what it is called)... Of course, it doesn't matter if it is .offset, or anything else after rngFound, because none of those are apparently used in Outlook vba, without some other qualification that I am unaware of.

Can someone help point me in the right direction? My interwebs search has been fruitless - not a lot of people trying to search Excel from Outlook, and those that do, aren't doing the more extensive type of search I'm doing. I did encounter some references to needing to fully qualify everything, but it seems to me that is accomplished prior to the error.

Here's my full script:
VBA Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim strSearchString As String
    Dim sfilenum As String
    strSearchString = Trim(TextBox1.Value)
    If InStr(strSearchString, " ") Then
        strSearchString = Replace(strSearchString, " ", "")
    End If
    If InStr(strSearchString, "-") Then
        strSearchString = Replace(strSearchString, "-", "")
    End If
    Dim xlapp As Excel.Application
    Dim xlwb As Excel.Workbook
    Dim xlws As Excel.Worksheet
    Set xlapp = GetObject(, "Excel.Application")
    Set xlwb = xlapp.Workbooks("GBOOK.xlsx")
    If xlwb Is Nothing Then
        xlapp.Workbooks.Open ("C:\ONEDRIVE\GBOOK.XLSX")
        Set xlwb = xlapp.Workbooks("GBOOK.xlsx")
       Else
    End If
    Set xlws = xlwb.Sheets("MASTER")
    If xlwb Is Nothing Then
        MsgBox "Required file is not available"
    End If
    
    Dim rngFound As Range
    Set rngFound = xlws.Cells.Find(What:=strSearchString, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False)
    If rngFound Is Nothing Then
        MsgBox "Value Not Found.", vbOKOnly, "GBook"
        Exit Sub
    Else
        sfilenum = rngFound.Offset(0, 0).EntireRow.Range("A1").Value    'ERRORS HERE: METHOD OR DATA MEMBER NOT FOUND
        
    End If
    If sfilenum = "FILENUM" Then Exit Sub
    '10/26/19 Adding multiple use clipboard function
    With New MSForms.DataObject
        .SetText sfilenum
        .PutInClipboard
    End With
'    Call MsgBoxTimeout(0, "File# " & sfilenum & " copied to clipboard.", "GBook", vbInformation, 0, 3000)

    TextBox1.Value = "SEARCH BOX"
    Set rngFound = Nothing
End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
sfilenum = rngFound.Offset(0, 0).EntireRow.Range("A1").Value

That works for me.

In my tests it means you are getting the value of column A, please try the following:

VBA Code:
sfilenum = xlws.Range("A" & rngFound.Row).Value
 
Upvote 0
That returns the same error at .Row. Combined with your indication that it does work for you suggests that I might not have a needed reference. However, I'm not finding one that would be obvious. Can you show me what references you have installed?

Here's what I have active in Outlook:
1673191906772.png


Or, perhaps I need a reference in Excel instead? Here's what I have in Excel:
1673192405205.png
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,185
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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