?
Solved

SetFocus for built-in Outlook mail form with VBA

Posted on 2003-03-05
13
Medium Priority
?
2,740 Views
Last Modified: 2012-06-27
As part of a Send and File mail message routine I've implemented using a custom toolbar button in Outlook XP, I attempt to invoke the ActiveInspector.CommandBars("Tools").Controls("Spelling...").Execute method before performing an ActiveInspector.CurrentItem.Send method.  Unfortunately, the Tools/Spelling menu item is inactive (and thus fails when called by VBA code) if the focus of the built-in Outlook mail form is not on the Subject or Body form controls (i.e. if it's instead in the From, To, CC fields).  So, prior to invoking the "Spelling..." menu item, I first need to set the focus of the form to the Subject or Body form control.

This sounds simple enough, but it is not.  Let me tell you a few things that DO NOT work:

1) ActiveInspector.CurrentItem.Body.SetFocus - this does NOT work because the MailItem.Body property is NOT the same as the body form control, and SetFocus will not work
2) ActiveInspector.ModifiedFormPages("Message").Controls("Message").SetFocus - this does NOT work because the ModifiedFormPages collection is empty when you are using the default/built-in Outlook forms.  This option would work if I was using custom forms, but I am not.
3) Prompting the user to place the cursor in the Subject/Body fields if it's not there - this will NOT work as a solution because the customer wants a TRUE solution (nice try though)

So, here's the question, for which an answer would be enormously appreciated

1) How can I set the focus of the default/built-in (NOT custom) Outlook mail message form to the Subject or Body fields using Outlook VBA from the VbaProject.OTM project?

Thanks!
-------------------------------------------------------
P.S.  The reason I'm attempting to use the menus to invoke the spell checker is that I could not figure out a way to invoke it any other way.  There does not appear (in the research I've done at least) to be a way perform the .CheckSpelling method in Outlook like you can in Word or Excel.  If you can tell me how to invoke the spell checker without the menu, which bypasses the SetFocus problem I face, you get the points, a blue ribbon, and a free pizza.
0
Comment
Question by:brenns
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +1
13 Comments
 

Author Comment

by:brenns
ID: 8075808
I should also state that another possible solution is to call Microsoft Word externally, paste the MailItem.Body text into a blank document, perform a .checkspelling, and then paste the text BACK into Outlook.  This is kinda messy and not particularly pleasant since I should be able to simply do this within Outlook (you would think).
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 8076259
0
 

Author Comment

by:brenns
ID: 8076293
vinnyd79, thanks, but that's the same solution that I described in my first follow-up comment.  Although that solution works, it wont work in this instance because:

a) too slow
b) confusing for the end user
c) ugly

But, as we both know, it does work, so thanks for the comment, even though I had anticipated that response.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Accepted Solution

by:
Diveblue earned 1500 total points
ID: 8077377
Have you considered/tried using SendKeys to send a tab keystroke in a loop until the Tools/Spelling menu item is available?


Scott
0
 

Author Comment

by:brenns
ID: 8077891
Scott,

Good thought, and yes I have tried that.  The problem is that SendKeys appears (and I could be wrong here) to just be sending keystrokes into never-never-land.  It's as if the code needs to first activate the form somehow before SendKeys knows where to place the keystrokes.  I've tried having SendKeys send vbTab and just plain old text.  If I place my cursor in the "To" field before executing the SendKeys code, I can't get ANY text to be placed in that field, much less tabs.  The problem might stem from the fact that the code for the "Send and File" button is completely independent of the built-in form, but I don't really know.  I just know it doesn't appear to work.

Although I (me, personally) can't get this to work, it's possible that it's a workable solution.  It's still not as good as simply invoking a checkspelling or manually setting the form's focus, but it's more doable than the external Word spell check.

On a related note, I've also tried to use something like Selection.TypeText vbTab (which is how Word records a Tab press in a Macro) to no avail.

If you have some ideas how I can pursue this Scott, I'd be happy to give them a shot.
0
 

Author Comment

by:brenns
ID: 8078074
Scott,

Let me add something about SendKeys that I didn't mention before.  If I "manually" execute a macro (using Tools/Macro/Macros... and then selecting my public sub) that houses the line "SendKeys vbTab", I can shift the focus of the form to the next field.  So I know, in theory, SendKeys does work in my scenario.

Hold the presses!

I did in fact figure out how to get the SendKeys procedure to work from my code.  I'm such a dumb-ACK sometimes. If I do a "SendKeys vbTab, 1" and wait for the procedures to process the keypress before continuing (which is what the boolean wait is for in SendKeys string[, wait]), it actually performs the tab press.

Unfortunately, even after the tab keypress moves the cursor into the message body field, the Tools menu is not being updated.  Take a look at my loop below.  I tried adding in a DoEvents hoping that would allow the menu to update, but that doesn't work.  I have to manually break out of the loop with the loopcount variable because the commandbar execute keeps failing (which adds a bunch of tab to the message body).  After falling out of the loop, the final commandbar execute fails.

                On Error Resume Next
                loopcount = 0
                Do
                    Err.Clear
                    SendKeys vbTab, True
                    ActiveInspector.CommandBars("Tools").Controls("Spelling...").Execute
                    loopcount = loopcount + 1
                    DoEvents
                Loop Until Err.Number = 0 Or loopcount > 50
                On Error GoTo 0
                ActiveInspector.CommandBars("Tools").Controls("Spelling...").Execute

However, if after I run the code and it fails, I then manually select Tools/Spelling... or simply click my button again, it works.  We're getting close here.
0
 

Author Comment

by:brenns
ID: 8078127
Something new to report. ActiveInspector.CommandBars("Tools").Reset does the trick.  The following code now actually works for my testing.  

On Error Resume Next
loopcount = 0
Do
     Err.Clear
     SendKeys vbTab, True
     ActiveInspector.CommandBars("Tools").Reset
     ActiveInspector.CommandBars("Tools").Controls("Spelling...").Execute
     loopcount = loopcount + 1
Loop Until Err.Number = 0 Or loopcount > 50

This isn't the prettiest code, but it does actually work.  I've been trying to create a situation in which it doesn't work (i.e. setting focus on buttons first), but I can't.  So, Scott, unless someone can tell me how to actually invoke the checkspelling method within Outlook in the next day or so, this solution is "good enough".  And since your thought actually prompted me to take another look at why SendKeys was failing for me, the thanks will go to you.
0
 
LVL 3

Expert Comment

by:Diveblue
ID: 8080788
Glad it worked. I don't do any outlook atomation myself, so I'm not intimate with It's behavior. That's why I gave you the "old school" suggestion.

scott
0
 

Expert Comment

by:djrickp
ID: 8091101
Brenn,

The solution is fairly simple.  

On Error Resume Next
loopcount = 0
Do
    Err.Clear
    SendKeys vbTab, True
    ActiveInspector.CommandBars("Tools").Reset
    ActiveInspector.CommandBars("Tools").Controls("Spelling...").Execute
    loopcount = loopcount + 1
Loop Until Err.Number = 0 Or loopcount > 50

I'll take the 500pts now.
0
 
LVL 3

Expert Comment

by:Diveblue
ID: 8091129
nice try dj
0
 

Author Comment

by:brenns
ID: 8091476
Well, since we didn't get an honest to goodness .CheckSpelling solution, and Scott forced me to go back and look at my SendKeys attempt, thanks go to Scott.
0
 
LVL 3

Expert Comment

by:Diveblue
ID: 8091514
thanks brenns!
0
 

Expert Comment

by:djrickp
ID: 8092479
Brenn,

I thought we were buddies? :-)
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question