Controlling the Word '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 'Save As' drop-down list

Post by catman » Wed Oct 14, 2015 8:28 pm

Here's a macro that saves the current open .docx file as Filtered .htm.
(It can easily be modified to work with other file types.)

Code: Select all

// NOTE: This macro was developed with Word 2013
//       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

// Testing diagnostic: optionally open up a specified .docx file
// Let>test_docx=%USERDOCUMENTS_DIR%\sample.docx
// Run>"C:\Program Files\Microsoft Office 15\root\office15\WINWORD.EXE" "%test_docx%"

// 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
// Press F12 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
// Press Alt-Down to explode the list display
ObjectSendKeys>dd_handle,ALT_DN,VK40,ALT_UP

// Press 'Home' to go 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
    // 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

// 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 'Save' button
FindObject>saveas_handle,Button,,7,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

// 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

    SRT>IgnoreMSerror
    END>IgnoreMSerror
    
    Let>ONERROR=IgnoreMSerror
    FindObject>word_handle2,Button,,1,button_handle,x1,y1,x2,y2,result_text
    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

User avatar
Meryl
Staff
Posts: 124
Joined: Wed Sep 19, 2012 1:53 pm
Location: Texas
Contact:

Re: Controlling the Word 'Save As' drop-down list

Post by Meryl » Mon Oct 19, 2015 1:24 pm

Thanks for sharing that.

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

Re: Controlling the Word 'Save As' drop-down list

Post by catman » Mon Oct 19, 2015 4:49 pm

You're welcome.

This macro certainly shows that controlling applications can be quite 'ad hoc' due to the unpredictability of the environment.

Like 'kung-fu' fighting, you gotta block the blows as they come, and then try to strike any way you can!

Fortunately, Macro Scheduler has ever-so-many tools for both 'defense' and 'offense.'

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Re: Controlling the Word 'Save As' drop-down list

Post by armsys » Thu Oct 22, 2015 11:23 pm

catman wrote:Here's a macro that saves the current open .docx file as Filtered .htm.
Hi Catman,
Thank you for generously sharing of your amazing code of advanced techniques and coding tricks.
In particular, I appreciate your extensive comments and readability, simplifying future maintenance and enhancement.
Thank you again.

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

Re: Controlling the Word 'Save As' drop-down list

Post by catman » Mon Nov 09, 2015 7:04 pm

Armsys: Glad that you find the macro informative.

In addition to wanting to share the technology, the reality is that I HAVE to document extensively for myself because I tend to FORGET details (including where my wallet and house keys are).

I'm going back into the macro today, a mere month later, to update it for Word 2016 ... and it's going to take me a while to 'relearn' Macro Scheduler!

My lame excuse for this absent-mindedness is 'creativity.'

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