Force another application to be active window

O

OssieMac

I have the following code to send automated emails. The user can select
option buttons to send a purely automated text message or attach an invoice
with an accompanying automated text message.

In addition there is a checkbox option if they want to open the email to
edit the automated text message before it is sent and then send it manually.

All works fine except if the checkbox is set to open the email for editing,
it does not open as the top (Active) window ready for editing and the user
has to select it from the task bar.

Is there a way to force the email to the top to become the Active Window?

Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem
Dim strEmailAddr As String
Dim strRef As String
Dim strBody As String
Dim strInvoicePathAndFile As String

Set objOutlook = CreateObject("Outlook.Application")
Set objEmail = objOutlook.CreateItem(olMailItem)

With objEmail
.To = strEmailAddr

.Subject = strRef

.Body = strBody

If Me.txtInvOrTxt = "Invoice" Then
.Attachments.Add (strInvoicePathAndFile)
End If

.Display

If Me.chkEmailEdit = False Then
.Send
Else
'***********************************************
'At this point can I force the new email _
'to be the top (Active window)
'***********************************************
End If

End With

Set objEmail = Nothing

Set objOutlook = Nothing
 
P

Paolo

Hi OssieMac,

it's just an idea (I didn't test it), you can try with .visible=true i.e.

If Me.chkEmailEdit = False Then
.Send
Else
.visible=true
End If

HTH Paolo
 
O

OssieMac

Thanks for trying Paolo but it returns

Error 438:
Object doesn't support this property or method.
 
P

Paolo

Well, now I tested your code and without modifying it, on my pc the
automatically created email become the active window...
 
O

OssieMac

It does that some of the time for me also but it is completely unreliable. I
initially thought that it depended on what other applications were open at
the time but with extended testing that doesn't seem to have anything to do
with it. I can set the flag to open for editing without sending and it will
open on the top and a few minutes later I do the same thing without opening
or closing any other applications and it will remain under the existing
window and I have to select it from the task bar.

You might notice that I do not quit the Outlook application because most
people have it open all the time anyway so I am not even sure that the method
of calling it is correct but that part of the code works fine so I assume it
is correct.
 
O

OssieMac

Thanks for your efforts Paolo but I have finally found it. Just wasn't using
the correct criteria to search.


AppActivate(Title of the window to activate)

In my case it is as follows.
AppActivate (strRef & " - Message - Microsoft Word")

More info at http://msdn.microsoft.com/en-us/library/dyz95fhy(VS.80).aspx

Just for is interest as a proper test I created a separate sub and was able
to place the window at the back and it brings it to the front.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top