VBA:从 Outlook 中保存的 xls 文件中提取单元格 (1,1) VBA
VBA: Extract cells(1,1) from a saved xls file in Outlook VBA
注意:我在Outlook中写了下面的代码VBA。基本上,代码在 ThisOutlookSession 中,我扫描以 "new" 开头的电子邮件,将附件保存在 Desktop 中,并将值 debug.print 保存在 Cells(1,1) 中。但是,Outlook 崩溃并在“Debug.Print sourceSH.Cells(1,1)”行上显示 "Object doesn't support this property or method"。怎么了?
Public WithEvents myOlItems As Outlook.Items
Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Dim f As Folder
Set f = Application.Session.Folders.item("me").Folders.item("Inbox")
Set myOlItems = f.Items
Call LoadForm
End Sub
Private Sub myOlItems_ItemAdd(ByVal item As Object)
On Error GoTo ErrorHandler
Dim Msg As Outlook.MailItem
Dim name As String
Dim res As String
Dim sourceWB As Workbook
Dim sourceSH As Worksheet
Dim NewFileName As String
Dim xlApp As Object
If TypeName(item) = "MailItem" Then
Set Msg = item
' do stuff with item
If Left(Msg.Subject, 3) = "New" And Msg.Attachments.Count > 0 Then
NewFileName = "C:\Desktop\" & Msg.Attachments.item(1).Filename
' save file
Msg.Attachments.item(1).SaveAsFile NewFileName
Set xlApp = CreateObject("Excel.Application")
xlApp.Application.Visible = True
Set sourceWB = xlApp.Workbooks.Open(NewFileName, True, True)
Set sourceSH = sourceWB.Worksheets(1)
Debug.Print sourceSH.Cells(1,1)
End If
End If
ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub
Debug.Print sourceSH.Cells(1,1)
Print 方法将消息直接输出到window。您需要传递一个字符串,而不是 Range 对象。尝试将 Text 属性 值传递给 Print 方法。
Debug.Print sourceSH.Cells(1,1).Text
注意:我在Outlook中写了下面的代码VBA。基本上,代码在 ThisOutlookSession 中,我扫描以 "new" 开头的电子邮件,将附件保存在 Desktop 中,并将值 debug.print 保存在 Cells(1,1) 中。但是,Outlook 崩溃并在“Debug.Print sourceSH.Cells(1,1)”行上显示 "Object doesn't support this property or method"。怎么了?
Public WithEvents myOlItems As Outlook.Items
Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Dim f As Folder
Set f = Application.Session.Folders.item("me").Folders.item("Inbox")
Set myOlItems = f.Items
Call LoadForm
End Sub
Private Sub myOlItems_ItemAdd(ByVal item As Object)
On Error GoTo ErrorHandler
Dim Msg As Outlook.MailItem
Dim name As String
Dim res As String
Dim sourceWB As Workbook
Dim sourceSH As Worksheet
Dim NewFileName As String
Dim xlApp As Object
If TypeName(item) = "MailItem" Then
Set Msg = item
' do stuff with item
If Left(Msg.Subject, 3) = "New" And Msg.Attachments.Count > 0 Then
NewFileName = "C:\Desktop\" & Msg.Attachments.item(1).Filename
' save file
Msg.Attachments.item(1).SaveAsFile NewFileName
Set xlApp = CreateObject("Excel.Application")
xlApp.Application.Visible = True
Set sourceWB = xlApp.Workbooks.Open(NewFileName, True, True)
Set sourceSH = sourceWB.Worksheets(1)
Debug.Print sourceSH.Cells(1,1)
End If
End If
ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ProgramExit
End Sub
Debug.Print sourceSH.Cells(1,1)
Print 方法将消息直接输出到window。您需要传递一个字符串,而不是 Range 对象。尝试将 Text 属性 值传递给 Print 方法。
Debug.Print sourceSH.Cells(1,1).Text