Sending text to Notepad requires two SetFocus instructions

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
evangelmike
Pro Scripter
Posts: 56
Joined: Sun May 11, 2008 9:39 pm

Sending text to Notepad requires two SetFocus instructions

Post by evangelmike » Sat Jun 09, 2012 7:48 pm

Greetings! I have a strange problem. I wrote the below macro to send those names I select on an Excel Spreadsheet to Notepad. The macro works perfectly as long as I use two "SetFocus>Untitled - Notepad" instructions in a row when I need for the focus to be on Notepad so that text can be written to it. If I only use one "SetFocus>Untitled - Notepad" instruction, text will not be written into Notepad. Any help or suggestions will be appreciated. Please note that this macro is executed while an Excel Worksheet containing names is open.

Code: Select all

// HotKey Ctrl-Alt-w
   MessageModal>Click OK, select the name you wish to send to Notepad, and press the TAB key.
// The TAB key activates a later WaitKeyDown instruction
   Wait>0.5
   SetFocus>Microsoft Excel - BookDates.xlsm
   WaitReady>0
   Wait>1
   WaitKeyDown>TAB
   Wait>0.3
// Since pressing the TAB sends the cursor one cell to the right, we need a 
// Press Left to move the cursor back to the originally selected cell.
   Press Left
   Wait>0.3
   Press Ctrl
   Wait>0.5
   Send>c
   Wait>1
   Release Ctrl
   Wait>0.5
   GetClipBoard>IssuerNm
   Wait>1
   Press Esc
    IfWindowOpen>Untitled - Notepad
      Goto>NotepadOpen0
    Else
      Run>Notepad.exe
      Wait>0.2
      WaitWindowOpen>Untitled - Notepad
    Endif
    Label>NotepadOpen0
    // Send>Issuer Name to Notepad
    Wait>0.5
    // Logic does not work unless the focus is set to Notepad twice!!!
    SetFocus>Untitled - Notepad
    Wait>1
    SetFocus>Untitled - Notepad
    Wait>1
    Send>IssuerNm
    Wait>0.5
    // Deselect Cell:
    SetFocus>Microsoft Excel - BookDates.xlsm
    Wait>0.5
    Press Esc
    Exit>0
May you have a blessed day!

Michael D Fitzpatrick
Reg. US Patent Agent

User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Post by Rain » Sun Jun 10, 2012 11:02 pm

Hi Mike,
ObjectSendKeys eliminates the focus and send functions.

Code: Select all


// HotKey Ctrl-Alt-w
   MessageModal>Click OK, select the name you wish to send to Notepad, and press the TAB key.
// The TAB key activates a later WaitKeyDown instruction
   Wait>0.5
   SetFocus>Microsoft Excel - BookDates.xlsm
   WaitReady>0
   Wait>1
   WaitKeyDown>TAB
   Wait>0.3
// Since pressing the TAB sends the cursor one cell to the right, we need a 
// Press Left to move the cursor back to the originally selected cell.
   Press Left
   Wait>0.3
   Press Ctrl
   Wait>0.5
   Send>c
   Wait>1
   Release Ctrl
   Wait>0.5
   GetClipBoard>IssuerNm
   Wait>1
   Press Esc
    IfWindowOpen>Untitled - Notepad
      Goto>NotepadOpen0
    Else
      Run>Notepad.exe
      Wait>0.2
      WaitWindowOpen>Untitled - Notepad
    Endif
    Label>NotepadOpen0
    // Send>Issuer Name to Notepad
    Wait>0.5
    // Use ObjectSendKeys instead of SetFocus and Send
    GetWindowHandle>Untitled - Notepad,hWndParent
    FindObject>hWndParent,Edit,,1,hWnd,X1,Y1,X2,Y2,result
    ObjectSendKeys>hWnd,{"IssuerNm"}
    Wait>0.5
    // Deselect Cell:
    SetFocus>Microsoft Excel - BookDates.xlsm
    Wait>0.5
    Press Esc
    Exit>0

ObjectSendKeys>handle,keystroke_list

Sends keystrokes directly to the given control. keystroke_list is a comma delimited list of characters or virtual key codes. Virtual key codes should be decimal values preceded by "VK".

For a list of virtual key codes see: http://www.mjtnet.com/vkcodes.htm

Use the Send Keys to Object Wizard to help create code that uses ObjectSendKeys.

Unlike the SendText command ObjectSendKeys sends directly to the control and does not require that the object has focus.

See also: ObjectSendText, FindObject


Example:

//Created by Send Keys to Object Wizard - sends Ctrl+Home, Shift+Ctrl+End, Delete to Notepad's editor

GetWindowHandle>Untitled - Notepad,hWndParent

FindObject>hWndParent,Edit,,1,hWnd,X1,Y1,X2,Y2,result

ObjectSendKeys>hWnd,CTRL_DN,VK36,CTRL_UP,SHIFT_DN,CTRL_DN,VK35,SHIFT_UP,CTRL_UP,VK46

User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Post by Rain » Mon Jun 11, 2012 3:14 pm

By the way, I have no problems running your script with 1 SetFocus. I had to modify your script because I don't have excel

Code: Select all

// HotKey Ctrl-Alt-w
   MessageModal>Click OK, select the name you wish to send to Notepad, and press the TAB key.
// The TAB key activates a later WaitKeyDown instruction
   Wait>0.5
   'SetFocus>Microsoft Excel - BookDates.xlsm
   'WaitReady>0
   Wait>1
   'WaitKeyDown>TAB
   Wait>0.3
// Since pressing the TAB sends the cursor one cell to the right, we need a 
// Press Left to move the cursor back to the originally selected cell.
   'Press Left
   Wait>0.3
   'Press Ctrl
   Wait>0.5
   'Send>c
   Wait>1
   Release Ctrl
   Wait>0.5
   GetClipBoard>IssuerNm
   Wait>1
   'Press Esc
    IfWindowOpen>Untitled - Notepad
      Goto>NotepadOpen0
    Else
      Run>Notepad.exe
      Wait>0.2
      WaitWindowOpen>Untitled - Notepad
    Endif
    Label>NotepadOpen0
    // Send>Issuer Name to Notepad
    Wait>0.5
    // Logic does not work unless the focus is set to Notepad twice!!!
    Wait>1
    SetFocus>Untitled - Notepad
    Wait>1
    Send>IssuerNm
    Wait>0.5
    // Deselect Cell:
    'SetFocus>Microsoft Excel - BookDates.xlsm
    Wait>0.5
    Press Esc
    Exit>0

evangelmike
Pro Scripter
Posts: 56
Joined: Sun May 11, 2008 9:39 pm

MDF Response to two SetFocus Required Assistance

Post by evangelmike » Mon Jun 11, 2012 4:39 pm

I forgot to mention that I am running Macro Scheduler version 9 in Windows 7. My version of Excel is version 10. My collection of macros is fairly large. I used a search program to count the number of SetFocus's in my macro collection: 2100! May you all have a most blessed day.

Sincerely,

Michael D Fitzpatrick
May you have a blessed day!

Michael D Fitzpatrick
Reg. US Patent Agent

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts