Determining if a user has selected text in Microsoft Outlook

Posted on 2004-04-26
Last Modified: 2010-05-02
Is there a way to determine programmatically if a user has selected text in an Outlook message (i.e. email)?

I need to copy the text from an Outlook message (email).  Using the "SendKeys" command for "Select All" and "Copy", I can capture ALL text.  But if the user has selected or highlighted just part of the message, I only want to copy that portion.  If I can determine first if there is selected text, then I could omit the "Select All" command and only use the "Copy" command.  

Is there something, which can be checked using VB, that indicates if the user has selected part of the text?
Question by:TSClark251
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
  • 3
  • 2
  • 2
LVL 10

Accepted Solution

ADSaunders earned 500 total points
ID: 10917269
Hi TSClark251,
You could copy regardless, and check whether anything was copied to the clipboard with:
if trim(clipboard.GetText) = "" then
(select all and copy)
End If

Regards .. Alan
LVL 19

Expert Comment

ID: 10917637
Isn't this question in a wrong TA!?!?! Its a VB question!!!
LVL 19

Expert Comment

ID: 10917668

It is very very possible, but involves a lot of API calls. Are you ready to get into that? Okay, let me brief you on how to go about it.

1) Firstly you have to detemine whether such a window is open or not. Use FindWindow API call for the same. To use this call you will also need to know the window class of the window. You will have to use a tool like SPY++ to do the same. Or alternatively, you may find on the basis of the Window Title. If the call suceeds, it will return you the handle to the window, else the handle would be NULL.
2) If you successfully find the window, go ahead and find the control containing the message text. The approach would be similar to finding the main window.
3) Once you have the handle to the window instance containing the message text, you can easily get its selectedText property.

You see, this is possible, but requires some work.

Good luck!

~ Ranjeet Rain

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

LVL 19

Expert Comment

ID: 10917676
Please ignore my first comment!

Author Comment

ID: 10924825

What an absolutely elegant solution.  I never considered a straightforward approach like this.  This not only works with Outlook, but also with any text window that permits Copy and Paste functions.
This is the actual code I ended up using:

     'Copy User-Selected Text to the clipboard    
     SendKeys "^c", True                        
     'Check for presence of User-Selected Text    
     If Trim(Clipboard.GetText) = "" Then  
           'If none exists, then copy the entire document
           SendKeys "^a", True
           SendKeys "^c", True
     End If

Respectfully, T.C.

Author Comment

ID: 10924861

Thanks for your input.   Fortunately I don't need to delve into API calls just yet...

r/ T.C.
LVL 10

Expert Comment

ID: 10926064
Elegant Eh? .. Very Gracious of you .. Thanks

.. Alan

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

726 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