通过VBA运行excel宏,返回运行时错误'429'

时间:2014-01-21 22:59:35

标签: excel vba visual-studio

我能够在excel表中手动运行我的宏而没有错误,但是当我通过VB运行代码时,我得到“429”ActiveX错误,这与Outlook 15.0参考有关。我在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

以下是我试图在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

错误发生在该行:

  

设置OutApp = CreateObject(“Outlook.Application”)

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

1 个答案:

答案 0 :(得分:1)

似乎当你以管理员身份运行VB时,&#34; 429&#34;弹出错误。不确定为什么这是一个问题,但是当我没有管理员权限运行它的工作! - Hughberto