Outlook VBA调用Excelmacros

我有一个Outlookmacros处理电子邮件并将其粘贴到Excel中,然后调用Excelmacros进行进一步处理。 当单独调用时,这两个macros按预期工作。 但是,如果我尝试从Outlookmacros中调用Excelmacros,则电子邮件将不会粘贴到Excel工作簿中,然后在调用Excelmacros时会因为没有数据而生成错误。 任何想法为什么

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的build议工作。 我把睡眠时间改为15秒,这已经成功了。 谢谢!

如果要从剪贴板中xlPasteValues格式化文本,则不能使用xlPasteValues 。 用这个代替:

 Selection.PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:=False 

请注意,这将以纯文本的forms粘贴内容。 如果您需要格式化,可以将“ Format参数更改为"HTML"