PDA

View Full Version : Copying selected text from the Reading Pane into a variable



acantor
04-25-2014, 07:30 AM
I want to copy text selected in the Reading Pane, and assign it to a string variable.

I use a similar script that works nicely when the email message is open in a window. The script sometimes works in the Reading Pane; but usually, for reasons I don't understand, the script stops with a Run Time error '91' on the first line:


Set myItem = ActiveInspector.CurrentItem

Select Case myItem.GetInspector.EditorType
Case "4" ' Word Editor
Body = myItem.GetInspector.WordEditor.Application.Selection.Range.Text

Case Else ' There may be a need to trap other cases separately. (1 - 4 seems to be the range)
Body = myItem.GetInspector.HTMLEditor.Selection.createRange.Text

End Select

acantor
04-30-2014, 11:26 AM
Any thoughts on how to copy selected text in the Reading Pane to a variable? It can be done with text that has been selected in an open email message. The challenge is to do the same to text in the Reading Pane.

westconn1
05-13-2014, 02:57 AM
The challenge is to do the same to text in the Reading Paneas the editortype is method of an inspector, the above methods will not work at all
2 possible methods
activate the Copy commandbarbutton
or
use virtual keyboard commands to pass ctrl C

acantor
05-14-2014, 09:07 AM
Thank you for the suggestion. So here are two test macros...




Sub Test1()

' Copy to the clipboard...

SendKeys "^c", True



When I run this macro in the Reading pane, the clipboard contains the selected text. So far, so good.

In the next example, I begin by sending Ctrl + C, as above, and then copy the clipboard content to a string variable:




Sub Test2()
' Add Reference to "Microsoft Forms 2.0 Object Library"
' If not available, find it by browsing to "c:\Windows\SysWOW64\FM20.DLL"

' Copy to the clipboard...

SendKeys "^c", True

' Copy Clipboard to a String variable:

Dim MyData As DataObject
Dim strClip As String

Set MyData = New DataObject
MyData.GetFromClipboard
strClip = MyData.GetText

MsgBox strClip



In this case, the variable contains what was previously in the clipboard. In other words, Ctrl + C is not copying the selected text to the clipboard.

What is happening? How perplexing that Ctrl + C works in the first example, but not in the second!

westconn1
05-14-2014, 02:27 PM
i tested your code, worked correctly for me in winXP

acantor
05-16-2014, 10:53 AM
That the code works under XP, and not Windows 7, may be a clue.

There are timing problems with SendKeys in Vista, Windows 7, and apparently, Windows 8. Although the command works, it does not necessarily work as reliably as it has in the past.

Is there a way to copy the selected text to the clipboard without simulated keystrokes? Or is there another method for sending keystrokes to the application?

I would prefer using a programmatic approach rather than interacting with the UI via keyboard or mouse. If this was Word VBA, I would use selection.copy; but I have been unsuccessful in getting this to work in Outlook.

westconn1
05-17-2014, 02:27 AM
Or is there another method for sending keystrokes to the application?you could try using findwindow, then send windows message for the copy menu item


There are timing problems with SendKeys in Vista, Windows 7, and apparently, Windows 8try putting a pause between crtl c and getting from clipboard

acantor
05-20-2014, 06:56 AM
I tried inserting a pause by adding this to the top of the module:



Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)


Then this:




SendKeys "^c", True ' Send keystrokes to copy selection...

' Exit Sub ' Enable this line for testing

Sleep (1000) ' Wait 1000 ms

' Copy Clipboard to a String variable:

Dim MyData As DataObject
Dim strClip As String

Set MyData = New DataObject
MyData.GetFromClipboard
strClip = MyData.GetText

MsgBox strClip


The problem persists: Ctrl + C copies to the clipboard only if I enable the "Exit Sub" after the SendKeys command. When the entire macro is enabled, the selected text does not make it to the clipboard... Is it possible that the code to assign the content of clipboard to a string variable is undermining the Ctrl + C?


you could try using findwindow, then send windows message for the copy menu item


Could you provide a code sample? This may be beyond my ability to do without assistance.

westconn1
05-20-2014, 02:18 PM
i have tested your code, it works fine for me, without sleep

how are you calling the procedure?
do you have a button or shortcut?

acantor
05-20-2014, 02:36 PM
None (or very few) of my SendKeys commands that worked under XP are reliable under Windows 7. (However, most of those macros are written in other VBA-like languages that offer viable alternatives, e.g., SendSystemKeys.)

I am invoking the command via the Macros screen (Alt + F8).

I think know what a button is, but what do you mean by it?

By shortcut, do you mean the commands added to the Quick Access Toolbar (QAT) that can be invoked by pressing Alt + number of object from the left boundary of the QAT, e.g., Alt + 1 for the first item from the left?

westconn1
05-21-2014, 02:19 AM
you should try invoking the macro from a toolbar button or shortcut in the main outlook window, if some other window has foreground, that window will receive the sendkeys

there are more reliable methods to send keystrokes, look into the vk virtual key codes
there was a thread in this forum recently that contained all the code for that option http://www.vbaexpress.com/forum/showthread.php?48983-Outlook-2010-macro-quot-Print-1st-page-quot-Problem-with-Num-Lock-turning-off