从Outlook VBA激活工作表

时间:2019-04-02 12:49:54

标签: excel vba outlook outlook-vba

在Windows 10 PC上,我使用的是Office Professional Plus 2013-32位,包括Outlook和Excel。我在Outlook中有一个宏可以发送电子邮件。

该宏使用两个工作簿:一个作为数据源,另一个作为日志。

我无法激活数据源工作表。我没有收到VBA错误,但是在测试时发现激活失败。

查看以下行:varTest = ActiveSheet.Name。这是宏的一些部分:

 Public objEmailWorkbook         As Excel.Workbook
 Public objEmailWorksheet        As Excel.Worksheet
 Public objLogWorkbook           As Excel.Workbook
 Public objLogWorksheet          As Excel.Worksheet
Sub Send_Emails()
 Dim objExcelApp                 As Excel.Application
 Dim strLogFullName              As String
 'Instantiate an instance of the Excel application.
 Set objExcelApp = CreateObject("Excel.Application")
 strXlsPathName = Environ("USERPROFILE") & "\Documents\"
 strLogFileName = "email_log.xlsx"
 strLogFullName = strXlsPathName & strLogFileName
 'Instantiate the Log Workbook object
 Set objLogWorkbook = objExcelApp.Workbooks.Open(strLogFullName, True, False)
 'There's only one worksheet in the Log workbook.  Identify the Log sheet name.
 strLogSheetName = objLogWorkbook.Sheets(1).Name
 'Instantiate the Log Worksheet object
 Set objLogWorksheet = objLogWorkbook.Sheets(strLogSheetName)
 objLogWorksheet.Activate

 'Instantiate the eMail Address Workbook object
 strEmailFileName = "emailTest.csv"
 strEmailFullName = strXlsPathName & strLogFileName
  'Instantiate the Email Worksheet object
 Set objEmailWorkbook = objExcelApp.Workbooks.Open(strEmailFullName, True, False)
 'There is only one sheet within a workbook.
 'Determine the eMail address sheet name.
 strEmailSheetName = objEmailWorkbook.Sheets(1).Name

 'Instantiate the eMail address worksheet object
 Set objEmailWorksheet = objEmailWorkbook.Sheets(strEmailSheetName)
 objEmailWorksheet.Activate
 '***Other Processing Code Here
 '***lngLastSheetCol and strSheetFieldName values are set here
 'Re-activate objEmailWorksheet
 '***The following ACTIVATE command doesn't appear to work
 objEmailWorksheet.Activate
 varTest = ActiveSheet.Name
 'Define rngSearchText as the header row in the worksheet
 Set rngSearchText = ActiveSheet.Range(Cells(1, 1), Cells(1, lngLastSheetCol))
 With rngSearchText
   'Find the column in the worksheet for strSheetFieldName
   Set rngFoundText = .Find(What:=strSheetFieldName, LookAt:=xlWhole, _
                                         MatchCase:=False, SearchFormat:=False)
   If Not rngFoundText Is Nothing Then
     lngFoundTextCol = rngFoundText.Column
   End If
 End With
Exunt:
Set objEmailWorksheet = Nothing
Set objEmailWorkbook = Nothing
Set objLogWorksheet = Nothing
Set objLogWorkbook = Nothing
Set objExcelApp = Nothing
Set rngSearchText = Nothing
Set rngFoundText = Nothing

End Sub

objEmailWorksheet.Activate命令从不起作用。当我单步执行代码时,varTest的值为“ log”。我知道“电子邮件”工作表的值是“ emailTest”。

由于未激活“电子邮件”工作表,因此以下搜索未返回正确的结果。

还有另一种方法来激活工作表或以其他方式解决问题吗?

1 个答案:

答案 0 :(得分:4)

您必须指定Excel应用程序objExcelApp

varTest = objExcelApp.ActiveSheet.Name

我建议始终激活Option Explicit:在VBA编辑器中,转到工具选项 Require Variable Declaration

但与此相反,我建议以下内容

'objEmailWorksheet.Activate 'you don't need activate at all
'varTest = objEmailWorksheet.Name 'this line is not needed too

Set rngSearchText = objEmailWorksheet.Range(objEmailWorksheet.Cells(1, 1), objEmailWorksheet.Cells(1, lngLastSheetCol))

每个 RangeCells指定范围或单元格所在的工作表。否则Excel无法知道。如果这样做,则根本不需要.Activate任何工作表。

您可能会受益于阅读 How to avoid using Select in Excel VBA。对于.Activate也是一样,应尽可能避免。


这3行

 strLogSheetName = objLogWorkbook.Sheets(1).Name
 Set objLogWorksheet = objLogWorkbook.Sheets(strLogSheetName)
 'objLogWorksheet.Activate 'don't need to activate!

只能减少到1行

 Set objLogWorksheet = objLogWorkbook.Sheets(1)

和这两行

strEmailSheetName = objEmailWorkbook.Sheets(1).Name
Set objEmailWorksheet = objEmailWorkbook.Sheets(strEmailSheetName)

只能减少到1行

Set objEmailWorksheet = objEmailWorkbook.Sheets(1)