Solved Open Outlook Maximized (1 Viewer)

HillTJ

To train a dog, first know more than the dog..
Local time
Yesterday, 18:30
Joined
Apr 1, 2019
Messages
731
Hi, I'm using the below code to open outlook from a command button on a popup form. All works well but outlook always opens minimized. I have another routine that uses the sendobject method (for different reason) and it works well. So what I do is check whether outlook is installed, if so use below code, but if outlook is not installed I use the sendobject method to open the default email browser (that may not be outlook).

Code:
Option Compare Database
Option Explicit


Public Function vcSendEmail_Outlook(sSubject As String, sTo As String, Optional sCC As String, Optional sBcc As String, Optional sAttachment As String, Optional sBody As String)
    Dim OutApp As Object
    Dim OutMail As Object
    


On Error GoTo ErrorHandler:


    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
  
    Set OutMail = OutApp.CreateItem(0)


    OutMail.To = sTo
    If sCC <> "" Then OutMail.CC = sCC
    If sBcc <> "" Then OutMail.BCC = sBcc
    OutMail.Subject = sSubject
    OutMail.HTMLBody = sBody
    


     If sAttachment <> "" Then
        OutMail.Attachments.Add (sAttachment)
     Else
        
    
End If
    
    OutMail.Display  'Send | Display
    Set OutMail = Nothing
    
ExitError:
    Exit Function
ErrorHandler:
  Select Case Err.Number
Case 440
    MsgBox "Outlook Cancelled by User", vbOKCancel
    Exit Function
        Resume Next
    Case 999
    MsgBox "Error Description", vbOKCancel
    Exit Function
        Resume Next
   Case Else
        Call LogError(Err.Number, Err.Description, "Email Module")
        Resume ExitError
    End Select
End Function
 

sonic8

AWF VIP
Local time
Today, 07:30
Joined
Oct 27, 2015
Messages
998
The first and easiest thing to to try would be:
Code:
OutApp.ActiveWindow.WindowState = olMaximized
 

HillTJ

To train a dog, first know more than the dog..
Local time
Yesterday, 18:30
Joined
Apr 1, 2019
Messages
731
@sonic8 , cool. I thought it had to be simple but could not find it. Thanks. Will let you know how It goes.
 

HillTJ

To train a dog, first know more than the dog..
Local time
Yesterday, 18:30
Joined
Apr 1, 2019
Messages
731
I put set OutApp.ActiveWindow.WindowState = olMaximized directly under line Set OutMail = OutApp.CreateItem(0) & got 'error 91 Öbject Variable or with block variable not set'. Don't understand it. Assistance would be appreciated.
 

bastanu

AWF VIP
Local time
Yesterday, 22:30
Joined
Apr 13, 2010
Messages
1,402
Maybe try ActiveExplorer (I tried it just before .Display):
Code:
    OutApp.ActiveExplorer.WindowState = 0 'olMaximized
    OutMail.Display  'Send | Display

The original function uses late binding so no reference to Outlook is necessary, but you need to use the actual value (0) of the olMaximized constant.

Cheers,
 

HillTJ

To train a dog, first know more than the dog..
Local time
Yesterday, 18:30
Joined
Apr 1, 2019
Messages
731
Cheers Vlad, you may recognise the code?
 

sonic8

AWF VIP
Local time
Today, 07:30
Joined
Oct 27, 2015
Messages
998
I put set OutApp.ActiveWindow.WindowState = olMaximized directly under line Set OutMail = OutApp.CreateItem(0) & got 'error 91 Öbject Variable or with block variable not set'. Don't understand it. Assistance would be appreciated.
I have to admit, the cause of this problem is not obvious at first glance.

There is no ActiveWindow when Outlook is started by VBA automation, unless you explicitly display one in your own VBA code.
If you want to display an Outlook Explorer window, you can do so either be explicitly calling the Explorers.Add method of the Outlook Application, or implicitly by calling Folder.Display.

I just published a more extensive explanation with sample code: Outlook Automation – Showing the Outlook Main Window
 

HillTJ

To train a dog, first know more than the dog..
Local time
Yesterday, 18:30
Joined
Apr 1, 2019
Messages
731
@sonic8 , thanks. I'm away from my pc for a few days & will try Vlads code & have a look at your link. Will let you know. Interestingly, if i use the sendobject method to open the default browser all is well.
 

sonic8

AWF VIP
Local time
Today, 07:30
Joined
Oct 27, 2015
Messages
998
Interestingly, if i use the sendobject method to open the default browser all is well.
This makes me wonder...
SendObject does not show the Outlook main window at all. Maybe I misunderstood your question.

Maybe you were just looking for this:
Code:
OutMail.Display
OutMail.GetInspector().WindowState = olMaximized
 

HillTJ

To train a dog, first know more than the dog..
Local time
Yesterday, 18:30
Joined
Apr 1, 2019
Messages
731
Gents, Vlads code worked. All good. Appreciate the help. Thank you. I move on....
 

Users who are viewing this thread

Top Bottom