on vba:
Dim outlookAPP as Object
On Error Resume Next
'get an Instance of outlook, if already open
Set outlookApp = GetObject(, "Outlook.Application")
'if there is an error, then Outlook is not yet Open.
If Err.Number <> 0 Then
'create New Instance
Set outlookApp = CreateObject("Outlook.Application")
End If
Err.Clear
On Error Goto 0
i copied your code into a module and then let the module run with the opening screenCode:on vba: Dim outlookAPP as Object On Error Resume Next 'get an Instance of outlook, if already open Set outlookApp = GetObject(, "Outlook.Application") 'if there is an error, then Outlook is not yet Open. If Err.Number <> 0 Then 'create New Instance Set outlookApp = CreateObject("Outlook.Application") End If Err.Clear On Error Goto 0
Public Function GetOutlook() As Object
Static ol As Object
On Error Resume Next
If ol Is Nothing Then
Set ol = GetObject(, "Outlook.Application")
If Err.Number <> 0 Then
Set ol = CreateObject("Outlook.Application")
End If
End If
Err.Clear
On Error GoTo 0
Set GetOutlook = ol
End Function
Public Function ShowOutlook(ol As Object)
Dim ns, fldr
Const oFolderInbox As Integer = 6
Set ns = ol.GetNamespace("MAPI")
Set fldr = ns.GetDefaultFolder(oFolderInbox)
fldr.display
End Function
' just to test this
Private Sub test()
Dim o As Object
Set o = GetOutlook()
ShowOutlook o
End Sub
i am not sure how to put this in a public functionyou also need to put the code in a Public Sub/Function.
then call it in autoexec macro or in form.
Code:Public Function GetOutlook() As Object Static ol As Object On Error Resume Next If ol Is Nothing Then Set ol = GetObject(, "Outlook.Application") If Err.Number <> 0 Then Set ol = CreateObject("Outlook.Application") End If End If Err.Clear On Error GoTo 0 Set GetOutlook = ol End Function Public Function ShowOutlook(ol As Object) Dim ns, fldr Const oFolderInbox As Integer = 6 Set ns = ol.GetNamespace("MAPI") Set fldr = ns.GetDefaultFolder(oFolderInbox) fldr.display End Function ' just to test this Private Sub test() Dim o As Object Set o = GetOutlook() ShowOutlook o End Sub
I did that, still when I run the Module, all it does is open VB, it does not open Outlookpress Alt-F11 (vba)
on Menu, Insert->Module
paste the code.
Ctrl-S to save.
I dont have any other code except the one arnelgp supplied in post #8GetObject for office apps is very glitchy to me, or has been depending on the pc's. (just my personal experience). I would recommend simply using CreateObject - then do your thing, then close the app you opened. Will work every time without a function.
Why don't you post your current code, what you are using to send the object?
You don't have a current method that you're using for the form to send an email?before a forms sends an email?
dim objOutlook as object
set objOutlook = createobject("Outlook.Application")
...your code to send the mail continues here.
' See if Outlook is open, otherwise open it
'If fIsOutlookRunning = False Then
Set objOutlook = CreateObject("Outlook.Application")
'Call OpenOutlook
'Pause (5)
' Else
'Set objOutlook = GetObject(, "Outlook.Application")
'End If
fIsOutlookRunning gives a invalid outside procedureI posted this in another thread, and it works for me whether I have Outlook open (the norm) and even if I do not.
You can see that I tried the 'is it open method', now all commneted out.
Code:' See if Outlook is open, otherwise open it 'If fIsOutlookRunning = False Then Set objOutlook = CreateObject("Outlook.Application") 'Call OpenOutlook 'Pause (5) ' Else 'Set objOutlook = GetObject(, "Outlook.Application") 'End If
Well mine is commented out? as can be seen by the code I copied and pasted? and it was only dong the same thing arnelgp's code was doing?fIsOutlookRunning gives a invalid outside procedure
#
' See if Outlook is open, otherwise open it
If fIsOutlookRunning = False Then
Set objOutlook = CreateObject("Outlook.Application")
Call OpenOutlook
Pause (5)
Else
Set objOutlook = GetObject(, "Outlook.Application")
End If#
I am not sure how to call the function, could you please assist?Well mine is commented out? as can be seen by the code I copied and pasted? and it was only dong the same thing arnelgp's code was doing?
Geez, I even mentioned that in the post.!
You really need to start looking at what any code is doing?. All those lines were commented out for a reason, and all you have done is uncomment them?
As you do not have the functions being called, those lines are always going to fail?
Set objOutlook = CreateObject("Outlook.Application")
I'm a bit curious on why you believe this is the case. Given a few seconds of time to be open, why would it not send the email, once the appropriate code to send it has fired?The only drawback I see to my way is that the email will not be sent until outlook is opened again and a Send & Receive is initiated IF Outlook is not open in the first place?