【发布时间】:2014-01-21 22:59:35
【问题描述】:
我可以在 Excel 工作表中手动运行我的宏而不会出错,但是当我通过 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
这是我试图在 Excel 中运行的宏中代码的 sn-p:
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 对象库有关。
【问题讨论】:
-
可能不相关,但下一行也会报错,除非你
Dim OutMail as Object ' or As Outlook.MailItem -
感谢您的提醒!
-
看看THIS是否有帮助。
-
非常感谢!!它今天早上随机开始工作......我将使用您的资源来尝试防止问题再次发生。谢谢你的帮助!!!
-
正如任何关注的人的更新一样,当您以管理员身份运行 VB 时,似乎会弹出“429”错误。不知道为什么这是个问题,但是当我在没有管理员权限的情况下运行它时,它的工作原理!
标签: excel vba visual-studio