How to set ActiveWindow.WindowState = olMaximized in late binding

I am trying to open outlook from msaccess using late binding. I want to display the mail message before sending. The code works but when I click send the message stays in the inbox until I manually open the outlook application.

Using objOutlook.ActiveWindow.WindowState = olMaximized in early binding solves this problem but it does not work in late binding.

What I'm I missing. Below is a snippet of my code:

Dim objOutlookApp  As Object
    Dim objOutlookMail As Object
      
    On Error Resume Next

    Set objOutlookApp = GetObject(, "Outlook.Application")
    
        If Err.Number = 429 Then
            Err.Clear
            
            Set objOutlookApp = CreateObject("Outlook.Application")
        End If

	On Error GoTo 0

    Set objOutlookMail = objOutlookApp.CreateItem(olMailItem)   
   
    With objOutlookMail
    .To = "email@email.com"
    .Body = "this is a test2"
    .Display
    
    End With
	
	objOutlookApp.ActiveWindow.WindowState = olMaximized

Open in new window

LVL 16
SheilsAsked:
Who is Participating?
 
SheilsAuthor Commented:
OK I ended up solving it myself. The missing bit was GetDefualtFolder. The code that works is as follows. Still need a bit of modification to make it a generic function but it works as is.

Option Explicit

Const olMailItem As Long = 0
Const olAppointmentItem As Long = 1
Const olContactItem As Long = 2
Const olTaskItem As Long = 3
Const olMaximized = 0
Const olMinimized = 1
Const olNormalWindow = 2
Const olFolderInbox = 6


Public Function openOutlook(DisplayBeforeSend As Boolean, strMsg As String)

    '##Use following code in late binding##
    '-----------------------------------------------------------------------------
    Dim objOutlookApp  As Object 'Comment out when testing early binding
    Dim objOutlookMail As Object
    '-----------------------------------------------------------------------------
        
    '##Use following code in early binding##
    '-----------------------------------------------------------------------------
'    Dim objOutlookApp  As Outlook.Application
'    Dim objOutlookMail As Outlook.MailItem
'    Dim objWin As Outlook.Explorer
'    Set objOutlookApp = New Outlook.Application
    '-----------------------------------------------------------------------------
    
   
     'Turn Error Handling OFF to attempt a call and test for errors.
    
    On Error Resume Next

    ' If Outlook is already open, then use GetObject to set a reference to it, first with a generic
    ' call ... then by specific application if need be

    Set objOutlookApp = GetObject(, "Outlook.Application")

    'Turn Error Handling back ON

        If Err.Number = 429 Then
            Err.Clear
            ' If code failed to "Get" an instance of Outlook, then it isn't currently open and we must use
            ' CreateObject to open and set a reference.

            Set objOutlookApp = CreateObject("Outlook.Application")
        End If

On Error GoTo 0

    Set objOutlookMail = objOutlookApp.CreateItem(olMailItem)
        
    With objOutlookMail
    
        .To = "sheilsbarra@gmail.com"
        .Body = strMsg
            
    End With
    
    If DisplayBeforeSend = True Then
    
    'Open inbox to prevent errors with security prompts
    objOutlookApp.Session.GetDefaultFolder(olFolderInbox).Display
    objOutlookApp.ActiveExplorer.WindowState = olMaximized
    
    objOutlookMail.Display
    
    Else
    
    objOutlookMail.send
    
    End If
  
End Function

Open in new window

0
 
Rgonzo1971Commented:
Hi,

Place

Const olMaximized = 0

at the beginnning of your code

Regards
0
 
SheilsAuthor Commented:
I actually do have that:

Option Compare Database

Option Explicit

Const olMailItem As Long = 0
Const olAppointmentItem As Long = 1
Const olContactItem As Long = 2
Const olTaskItem As Long = 3
Const olMaximized = 0
Const olMinimized = 1
Const olNormalWindow = 2

Public Function openOutlook()


    Dim objOutlookApp  As Object 'Comment out when testing early binding
    Dim strOutLookMsg As String
    Dim objOutlookMail As Object

Open in new window


It does not help
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Rgonzo1971Commented:
maybe because you set the OL obj as nothing at the end of the code
0
 
SheilsAuthor Commented:
no it's not set to nothing. I works find when I use early binding. But don't work with late binding. This means that there is an additional object that I need to declare and set I just don't know which one.
0
 
SheilsAuthor Commented:
I found the code required to make it work
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.