Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Outlook在Excel中查找值_Excel_Vba_Outlook - Fatal编程技术网

从Outlook在Excel中查找值

从Outlook在Excel中查找值,excel,vba,outlook,Excel,Vba,Outlook,从Outlook执行Excel vlookup时遇到问题 以下是我试图实现的目标: -循环浏览收件箱中的每封电子邮件 -如果主题行符合标准,则获取值 -进入excel并查看该值 -如果该值存在,请激活该单元格并切换以获取相邻单元格的文本值,并将其带回outlook以插入电子邮件(尚未插入) 这就是我一直坚持的代码(不断获取“object required”错误): 我认为objExcel将取代“应用程序”,因为这将默认为Outlook 我想说的是,我有一些代码可以打开excel文件,并在每封电子

从Outlook执行Excel vlookup时遇到问题

以下是我试图实现的目标:
-循环浏览收件箱中的每封电子邮件
-如果主题行符合标准,则获取值
-进入excel并查看该值
-如果该值存在,请激活该单元格并切换以获取相邻单元格的文本值,并将其带回outlook以插入电子邮件(尚未插入)

这就是我一直坚持的代码(不断获取“object required”错误):

我认为objExcel将取代“应用程序”,因为这将默认为Outlook

我想说的是,我有一些代码可以打开excel文件,并在每封电子邮件中循环以成功获取主题行值,但我似乎无法控制它来执行功能


更新-我尝试了这一点,但它起了作用,但当它应该为true时,该值被设置为false。你能确认这是正确的吗

Dim wrksht As Object 
Set wrksht = objExcel.ActiveWorkbook.Sheets("Sheet1") 
Dim res As Boolean 

sourceWB.Activate 
With objExcel 
    res = Not IsError(objExcel.Match(result, wrksht.Range("A:A"), 0)) 
End With

如果只想检查是否存在某些内容,可以使用Match

注意:如果删除
工作表函数
,则可以测试返回值以查看是否有错误:如果包含
工作表函数
,则如果不匹配,代码将生成运行时错误

 Dim objExcel As Object, wb As Object, sht As Object
 Dim result As String, found As Boolean

 Set objExcel = CreateObject("Excel.Application")
 '
 '
 Set wb = objExcel.Workbooks.Open("path_goes_here.xlsx")
 Set sht = wb.Sheets(1) 'or use the sheet tab name
 '


 result = Right(subject, 7)
 found = Not IsError(objExcel.Match(result, sht.Range("A:A"), 0))
编辑:如果希望从另一列中获得相应的值,则可以执行以下操作-

 Dim m, answer
 result = Right(subject, 7)

 m = objExcel.Match(result, sht.Range("A:A"), 0)

 If Not IsError(m) Then
     answer = sht.Cells(m, "B").Value
 Else
     answer = "Not found!"
 End If

我决定使用以下方法:

For Each cell In objExcel.Range("A:A")

    If CStr(cell) = result Then
           .
           .
           .
感谢@TimWilliams与我一起完成这项工作

这是一个完整的例子

Sub vLookup()

    Dim objExcel As Object
    Set objExcel = CreateObject("Excel.Application")

    objExcel.Visible = True

    Dim xlWB As Object
    Set xlWB = objExcel.Workbooks.Add

    Dim xlSheet As Object
    Set xlSheet = xlWB.Sheets("Sheet1")

    Dim formula As String
    Dim result As String

    xlSheet.Range("a3") = "abc123"
    xlSheet.Range("a4") = "abc"
    xlSheet.Range("a5") = "123"

Stop   ' press F8 to single-step from here

    result = "abc123"                                        ' this will be found
    formula = "IFNA(MATCH(""" & result & """,A:A,0),0)"      ' cell formula: =IFNA(MATCH("abc123",A:A,0),0)   return 0 if not found
    Debug.Print objExcel.Evaluate(formula)                   ' returns 3 in this example

   ' press ctrl-G to see output of Debug.Print

    result = "aaa"                                           ' this will not be found
    formula = "IFNA(MATCH(""" & result & """,A:A,0),0)"      ' cell formula: =IFNA(MATCH("aaa",A:A,0),0)   return 0 if not found
    Debug.Print objExcel.Evaluate(formula)                   ' returns 0 in this example

    Set xlSheet = Nothing
    Set xlWB = Nothing
    Set objExcel = Nothing

End Sub

嗨@TimWilliams,谢谢你的回复。虽然我喜欢你的建议,但我仍然得到这个“需要对象”错误。代码在outlook中,所以我必须激活excel工作簿才能获得该控件并执行该操作吗?我仅有的另外两段与excel对象相关的代码如下:With objExcel.Visible=True.EnableEvents=False End With strFile=path to file Set sourceWB=objExcel.Workbooks.Open(strFile)sourceWB.Activate我应该为工作表创建一个对象吗?我尝试了这个方法,它成功了,但是当它应该为true时,该值被设置为false。你能确认这是正确的吗@TimWilliams Dim wrksht作为对象集wrksht=objExcel.ActiveWorkbook.Sheets(“Sheet1”)Dim res作为布尔源WB。使用objExcel res=Not iError激活(objExcel.Match(结果,wrksht.Range(“A:A”),0))以什么类型的值结束
result
?字符串值@TimWilliams
Sub vLookup()

    Dim objExcel As Object
    Set objExcel = CreateObject("Excel.Application")

    objExcel.Visible = True

    Dim xlWB As Object
    Set xlWB = objExcel.Workbooks.Add

    Dim xlSheet As Object
    Set xlSheet = xlWB.Sheets("Sheet1")

    Dim formula As String
    Dim result As String

    xlSheet.Range("a3") = "abc123"
    xlSheet.Range("a4") = "abc"
    xlSheet.Range("a5") = "123"

Stop   ' press F8 to single-step from here

    result = "abc123"                                        ' this will be found
    formula = "IFNA(MATCH(""" & result & """,A:A,0),0)"      ' cell formula: =IFNA(MATCH("abc123",A:A,0),0)   return 0 if not found
    Debug.Print objExcel.Evaluate(formula)                   ' returns 3 in this example

   ' press ctrl-G to see output of Debug.Print

    result = "aaa"                                           ' this will not be found
    formula = "IFNA(MATCH(""" & result & """,A:A,0),0)"      ' cell formula: =IFNA(MATCH("aaa",A:A,0),0)   return 0 if not found
    Debug.Print objExcel.Evaluate(formula)                   ' returns 0 in this example

    Set xlSheet = Nothing
    Set xlWB = Nothing
    Set objExcel = Nothing

End Sub