通过VB运行Excelmacros,返回运行时错误'429'

我可以在excel表格中手动运行我的macros而不出错,但是当我通过VB运行代码时,出现与Outlook 15.0引用相关的“429”ActiveX错误。 我有在VB和Excel文件中引用的outlook 15.0对象库。 不知道从这里做什么。 这里是VB中的代码:

Imports System Imports System.Threading Imports System.IO Imports Microsoft.Office.Core Imports System.Runtime.InteropServices Imports Microsoft.Office.Interop Module module1 Sub Main() Dim bloomberg As String Dim xlApp Dim wkbk As String Dim macro As Strign Dim oWorkBook Dim boolexists As Boolean oWorkBook = Nothing bloomberg = "C:\blp\API\Office Tools\BloombergUI.xla" xlApp = CreateObject("Excel.Application") 'ReloadXLAddins(xlApp) 'I want to see what happens xlApp.Visible = True ''Loading in the Bloomberg add-in xlApp.Workbooks.Open(bloomberg) ' Disable Excel UI elements xlApp.DisplayAlerts = True xlApp.AskToUpdateLinks = True xlApp.AlertBeforeOverwriting = True ' Iterate through the list by using nested loops. Dim i As Integer For i = 1 To 5 Select Case i Case 1 wkbk = "E:\Screening\IPO Lockup" macro = "ipolockup" Case 2 wkbk = "E:\Screening\UsBuybacksQ" macro = "UsBuybacksQ" Case 3 wkbk = "E:\Screening\UsBuybacksY" macro = "UsBuybacksY" Case 4 wkbk = "E:\Screening\GlobalBuybacksQ" macro = "GlobalBuybacksQ" Case 5 wkbk = "E:\Screening\GlobalBuybacksY" macro = "GlobalBuybacksY" End Select oWorkBook = xlApp.Workbooks.Open(wkbk) oWorkBook = xlApp.ActiveWorkbook.VBProject.References.AddFromGuid("{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}", 0, 0) oWorkBook = xlApp.ActiveWorkbook.VBProject.References.AddFromGuid("{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}", 0, 0) Thread.Sleep(Seconds(7)) On Error Resume Next ' Run the macro xlApp.Run(macro) If Err.Number <> 0 Then ' Error occurred - just close it down. MsgBox("run macro Error occurred") End If Err.Clear() On Error GoTo 0 Thread.Sleep(Seconds(20)) ' Clean up and shut down oWorkBook.Close() oWorkBook = Nothing Next ' Don't Quit() Excel if there are other Excel instances ' running, Quit() will shut those down also If xlApp.Workbooks.Count = 0 Then xlApp.Quit() End If xlApp.Quit() xlApp = Nothing MsgBox("Completed Email Draft Creation Succesfully") End Sub Private Function Seconds(ByVal value As Integer) As Integer Return value * 1000 End Function 

这里是macros中的代码片段我试图在Excel中运行:

 Sub refreshed() Dim rng As Range Dim i As Integer Dim OutApp As Object Dim OutMail As New Outlook.Application With Application .DisplayAlerts = False .Calculate End With i = Sheets("IPO").Range("numberofipo").Value Sheets("IPO").Range("IPO").Select Set rng = Nothing Set rng = ActiveCell.Resize(i + 1, 16) Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(0) On Error Resume Next With OutMail .To = "" .CC = "" .BCC = "" .Subject = "Monthly IPO Data" .HTMLBody = RangetoHTML(rng) .Save End With On Error GoTo 0 Set OutMail = Nothing Set OutApp = Nothing ThisWorkbook.Saved = True End Sub 

错误发生在线上:

Set OutApp = CreateObject(“Outlook.Application”)

这导致我相信它与Outlook 15.0对象库有关。

看来当你以pipe理员身份运行VB时,popup“429”错误。 不知道为什么这是一个问题,但是当我没有pipe理员权限运行它的作品! – Hughberto