Controlling the Word 2016 'Save As' drop-down list

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
catman
Newbie
Posts: 10
Joined: Wed Oct 07, 2015 5:34 pm

Controlling the Word 2016 'Save As' drop-down list

Post by catman » Tue Nov 10, 2015 11:29 pm

Code: Select all

// NOTE: This macro was developed with Word 2013 (Oct 2015)
//       The two modifications for Word 2016 are commented below (Nov 2015)
//       There are a number of 'Unsolved Mystery' workarounds in the macro

// ==> ALERT: If Debug-stepping, Debug>RefocusWindows MUST be unchecked
// ==> in order for the drop-down list processing to function correctly in the Debugger

Let>ONERROR=KillMS
SRT>KillMS
    // You'll come here if, for example, there is a missing 'Endif' in the macro
    // or if there is command error like MouseMove,100,200 instead of MouseMove>100,200
    MessageModal>Disastrous Macro Scheduler error: %LAST_ERROR%
    Exit
END>KillMS

IfNotWindowOpen>.docx - Word*
    // Testing diagnostic: optionally open up a test .docx file
    Let>test_docx=%USERDOCUMENTS_DIR%\sample.docx
    // Word 2013
    // Run>"C:\Program Files\Microsoft Office 15\root\office15\WINWORD.EXE" "%test_docx%"
    // Word 2015
	Run>"C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE" "%test_docx%"
Endif

// ignore extraneous spaces in commands
Let>IGNORESPACES=1
// give Word plenty of time to do its windows things
Let>WW_TIMEOUT=30
// visible windows only
Let>WF_TYPE=2
// slow down Macro Scheduler a little, to give Word plenty of time to respond
Let>STEP_DELAY=10

// begin, using window titles (but we will later use handles for objects)
Let>WIN_USEHANDLE=0
IfWindowOpen>.htm - Word*
    GoSub>ErrorQuit,an '.htm - Word' window is already open
Endif
IfWindowOpen>Save As
    GoSub>ErrorQuit,a 'Save As' window is already open
Endif
WaitWindowOpen>.docx - Word*
IfNotWindowOpen>.docx - Word*
    GoSub>ErrorQuit,there is no Word .docx for us to save!
Endif

GetWindowHandle>.docx - Word*,docx_handle
Let>WIN_USEHANDLE=1
// Now we press F12 in order to open the 'Save As' window
FindObject>docx_handle,_WwG,,1,child_handle,x1,y1,x2,y2,result_text
ObjectSendKeys>child_handle,VK123

Let>WIN_USEHANDLE=0
WaitWindowOpen>Save As
IfNotWindowOpen>Save As
    GoSub>ErrorQuit,F12 failed to open the 'Save As' window!
Endif

// drop-down list processing
GetWindowHandle>Save As,saveas_handle
FindObject>saveas_handle,ComboBox,,2,dd_handle,x1,y1,x2,y2,result_text
Let>desired_text={"Word Document (*.docx)"}
Position>desired_text,result_text,1,dt_pos
If>dt_pos=0
    MessageModal>Just FYI, the drop-down list opened to "%result_text%" instead of "%desired_text%"
Endif

// ==> ALERT: If Debug-stepping, Debug>RefocusWindows MUST be unchecked
// ==> in order for the drop-down list processing to function correctly in the Debugger

// ***Unsolved Mystery: the 'Save As' window opens with focus on the 'File name:' combobox 
// in order to get keystrokes to the 'Save as type' combobox, we must send a Tab to the 'File Name' combo box
// You would think that this should not be necessary when sending keystrokes directly to the drop-down object
// weirdly, ObjectSendKeys>dd_handle,VK9 works just as well ... What's up with that???
FindObject>saveas_handle,ComboBox,,1,fn_handle,x1,y1,x2,y2,result_text
Position>.docx,result_text,1,docx_pos
If>docx_pos=0
    GoSub>ErrorQuit,'File name:' combo box does not contain '.docx': "%result_text%"
Endif
ObjectSendKeys>fn_handle,VK9

// here come the official Word shortcut keystrokes for a drop-down list
// We will press Alt-Down to explode the list display
ObjectSendKeys>dd_handle,ALT_DN,VK40,ALT_UP

// We will press 'Home' to force the drop-down to the top item in the list
// (ComboBox,2 is the 'Save as type:' object)
ObjectSendKeys>dd_handle,VK36
GetControlText>saveas_handle,ComboBox,2,result_text
Let>WIN_USEHANDLE=1
Let>desired_text={"Web Page, Filtered (*.htm;*.html)"}
Repeat>result_text
    // We will repeatedly press DownArrow in order to march down the list to the desired value
    ObjectSendKeys>dd_handle,VK40
    GetControlText>saveas_handle,ComboBox,2,result_text
    If>result_text={"Works 6-9 Document (*.wps)"}
        GoSub>ErrorQuit,we hit the bottom of the drop-down list and got "%result_text%" ... this should not occur!
    Endif
Until>result_text=desired_text

// We will press Alt-Down to implode the drop-down list
ObjectSendKeys>dd_handle,ALT_DN,VK40,ALT_UP

// just to be thorough, we'll check for the existence of the 'Save' button
// Word 2013
// FindObject>saveas_handle,Button,,7,button_handle,X1,Y1,X2,Y2,result_text
// Word 2015
FindObject>saveas_handle,Button,,8,button_handle,X1,Y1,X2,Y2,result_text
If>result_text<>&Save
    GoSub>ErrorQuit,'Save' button text not found ... got "%result_text%" instead
Endif

// ***Unsolved Mystery: this handle-directed PushButton always fails, whether stepped or executed
Let>WIN_USEHANDLE=1
PushButton>saveas_handle,&Save
// ***but this title-directed PushButton always succeeds!
Let>WIN_USEHANDLE=0
PushButton>Save As,&Save

WaitWindowOpen>Microsoft Word
IfNotWindowOpen>Microsoft Word
    GoSub>ErrorQuit,Save As "Save" button failed to bring up 'Microsoft' Word window ... this should not occur!
Endif
GetWindowHandle>Microsoft Word,word_handle1

// at this point, we are looking at one of two windows,
// both are titled: 'Microsoft Word'
// 1) 'Replace existing file', or if there was no existing file ...
// 2) the Warning window about losing Office-specific tags when saving as 'Filtered' .htm

Let>WIN_USEHANDLE=1
UIAccessibleList>word_handle1,uilist
Position>already exists,uilist,1,ae_pos
If>ae_pos>0
    // close the 'Replace existing file' window if it has appeared
    UIClick>word_handle1,OK
    WaitWindowClosed>word_handle1
    Let>WIN_USEHANDLE=0
    WaitWindowOpen>Microsoft Word
    IfNotWindowOpen>Microsoft Word
        GoSub>ErrorQuit,Pressing 'OK' to the 'Replace existing file' window did not open a 'Microsoft Word' window!
    Endif
Endif

Let>WIN_USEHANDLE=0
GetWindowHandle>Microsoft Word,word_handle2
// Testing diagnostic:
// If>word_handle1=word_handle2
//     MessageModal>Diagnostic: Just FYI, there was no 'Replace existing file' window
// Endif

// ** now we'll close the Warning window
Let>WIN_USEHANDLE=1
GetWindowText>word_handle2,window_text
Position>{"Office-specific tags"},window_text,1,ost_pos
If>ost_pos=0
    GoSub>ErrorQuit,'Office-specific tags' not present in window text: "%window_text%
Endif

// We will press the 'Yes' button
If>word_handle2<>word_handle1
    // ***Unsolved Mystery: this UIC only works if there was a Replace window handled above
    UIClick>word_handle2,Yes
Else
    // ***Unsolved Mystery: when Debug-stepping, if there was no Replace window, then the following UIClick has no effect
    // however ... if executing rather than Debugging, it works! (you can verify this by enabling the following breakpoint)
	// **breakpoint**
    UIClick>word_handle2,Yes

    // If the above UIClick failed, then there is still a 'Yes' button, and we will mouse-click it
    // If the above UIClick succeeded, then the FindObject>...Button below will throw an MS error ... 
	// which we will ignore by temporarily activating the following temporry null SRT for ONERROR

    SRT>IgnoreMSerror
    END>IgnoreMSerror
    
    Let>ONERROR=IgnoreMSerror
    FindObject>word_handle2,Button,,1,button_handle,x1,y1,x2,y2,result_text
	// Reactivate the original ONERROR SRT
    Let>ONERROR=KillMS
    
    // if there was no MS error in the FindObject above, then we found a button
    If>LAST_ERROR<>Object not located in FindObject
        // just to be sure, we'll verify that we found the 'Yes' button
        If>result_text<>&Yes
            GoSub>ErrorQuit,FindObject found a button, but it was not the 'Yes' button: "%result_text%"
        Endif
        // CAUTION: if you are debug-stepping, use the F8 key now
        // and be sure that the Warning window 'Yes' key is visible on the screen
        // otherwise, the following MouseMove + LClick will fail
        MouseMove>x1,y1
        LClick
        // OK, you can resume clicking the 'Debug Step' icon
    Endif
Endif

// at this point, the 'Microsoft Word' Warning window should have been closed by the 'Yes' button
// which was clicked by UIClick if executing, or by LClick if debugging
WaitWindowClosed>word_handle2
IfWindowOpen>word_handle2
    GoSub>ErrorQuit,clicking the 'Yes' button did not close the Warning window
Endif

Let>WIN_USEHANDLE=0
WaitWindowOpen>.htm - Word*
IfNotWindowOpen>.htm - Word*
    GoSub>ErrorQuit,the .htm window did not open
Endif
// give the '.htm - Word' window time to run its file save progress bar
SetFocus>.htm - Word*
WaitReady>0
CloseWindow>.htm - Word*
WaitWindowClosed>.htm - Word*
IfWindowOpen>.htm - Word*
    GoSub>ErrorQuit,the .htm window did not close!
Endif

Exit

SRT>ErrorQuit
    MessageModal>ErrorQuit: %ErrorQuit_var_1%
    Exit
END>ErrorQuit

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