Excel 从Outlook VBA激活工作表

Excel 从Outlook VBA激活工作表,excel,vba,outlook,Excel,Vba,Outlook,在Windows 10 PC上,我使用Office Professional Plus 2013-32位,包括Outlook和Excel。我在Outlook中有一个发送电子邮件的宏 该宏使用两个工作簿:一个作为数据源,另一个作为日志 激活数据源工作表时遇到问题。我没有收到VBA错误,但当我测试时发现激活失败 请参见以下行:varTest=ActiveSheet.Name。以下是宏的一些部分: Public objEmailWorkbook As Excel.Workbook

在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
objemailsheet.Activate
命令永远不起作用。当我单步执行代码时,varTest的值为“log”。我知道电子邮件工作表的价值是“emailTest”

由于电子邮件工作表未激活,以下搜索不会返回正确的结果


是否有其他方法激活工作表或以其他方式解决问题?

您必须指定Excel应用程序
objExcelApp

varTest = objExcelApp.ActiveSheet.Name
我建议始终激活
选项显式
:在VBA编辑器中,转到工具›选项›

但我不建议这样做,而是推荐以下内容

'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))
为每个范围和单元格指定范围或单元格所在的工作表。否则Excel就不会知道这一点。如果您这样做,您根本不需要激活任何工作表

你可能会从阅读中受益 .
也一样。激活
应尽可能避免


还有这三条线

 strLogSheetName = objLogWorkbook.Sheets(1).Name
 Set objLogWorksheet = objLogWorkbook.Sheets(strLogSheetName)
 'objLogWorksheet.Activate 'don't need to activate!
只能减少到1行

 Set objLogWorksheet = objLogWorkbook.Sheets(1)
Set objEmailWorksheet = objEmailWorkbook.Sheets(1)
这两行呢

strEmailSheetName = objEmailWorkbook.Sheets(1).Name
Set objEmailWorksheet = objEmailWorkbook.Sheets(strEmailSheetName)
只能减少到1行

 Set objLogWorksheet = objLogWorkbook.Sheets(1)
Set objEmailWorksheet = objEmailWorkbook.Sheets(1)

这就解决了问题。谢谢仅供参考,我始终使用Option Explicit,包括此应用程序。我只是想为这次讨论提供一个代码的简短部分。再次感谢!