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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Rgonzo1971Commented:
maybe because you set the OL obj as nothing at the end of the code
0
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

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:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SheilsAuthor Commented:
I found the code required to make it work
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Outlook

From novice to tech pro — start learning today.