I'm able to run my macros manually in the excel sheet without an error, however when I run the code via VB, I get the "429" ActiveX error, which is related to the Outlook 15.0 reference. I have the outlook 15.0 object library referenced in both VB and the Excel file. Not sure what to do from here. Here's the code in 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
and here is the snippet of code in the macro I'm trying to run in 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
The error occurs on the line:
Set OutApp = CreateObject("Outlook.Application")
which leads me to believe it is related to the Outlook 15.0 object library.
Dim OutMail as Object ' or As Outlook.MailItem
– David Zemens