我有一个Outlook宏处理电子邮件并将其粘贴到Excel中,然后调用Excel宏进行进一步处理。当单独调用时,这两个宏按预期工作。但是,如果我尝试从Outlook宏中调用Excel宏,电子邮件将而不是粘贴到Excel工作簿中,然后在调用Excel宏时由于没有数据而生成错误。任何想法为什么Outlook VBA调用Excel宏
xlApp.Run ("PERSONAL.XLSB!Commissions_Report_Format")
会导致数据不能从Outlook粘贴到Excel?只有当这行代码存在时才会出现错误。提前致谢!
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Option Explicit
Sub PasteToExcel(item As Outlook.MailItem)
Dim activeMailMessage As MailItem
Dim xlApp As Excel.Application
Dim Wb As Excel.Workbook
Dim Ws As Excel.Worksheet
'Get a handle on the email
Set activeMailMessage = ActiveExplorer.Selection.item(1)
'Copy the formatted text:
activeMailMessage.GetInspector().WordEditor.Range.FormattedText.Copy
'Ensure Excel Application is open
Set xlApp = CreateObject("Excel.Application")
'Make Excel Application visible
xlApp.Visible = True
'Open the Personal Macro Workbook, or the Excel macro won't run
xlApp.Workbooks.Open ("C:\Users\AppData\Roaming\Microsoft\Excel\XLSTART\PERSONAL.xlsb")
'Name the Excel File
Set Wb = xlApp.Workbooks.Add
'Paste the email
Set Ws = xlApp.Sheets(1)
Ws.Activate
Ws.Range("A1").Select
Sleep 3000
Selection.PasteSpecial xlPasteValues
Sleep 3000 'wait for 3 seconds
'Run the Excel macro to clean up the file
xlApp.Run ("PERSONAL.XLSB!Commissions_Report_Format")
End Sub
我看到你在你的代码中使用睡眠,你睡了足够长的时间来粘贴邮件吗? – user2676140
好的 - 所以看起来这不是修复。这段代码我得到了间歇性的运行时错误91。有时代码可以正常工作,但有时候不会。有什么想法吗? – battery514
你可以在你的问题上添加你的xlsb代码吗? – 0m3r