' Draft Scripting class 27 (9/11/2011) ' This scripting class covers the use of the MS Office app (being renamed to Office VBA and VBS Editor) ' This app allows you to create your VBScript apps in the MS Word VBA editor. ' (you must own a copy of MS Word)
' This VBA environment offers you the following features: ' * variable/function types ' * intelli-sense (automatic prompting for object properties and methods when you type the dot operator in a command) ' * auto quick-info (automatic speaking of syntax for a function or method) ' * object browser (allows you to see all properties and methods of an object, along with their syntax) ' * command line syntax checking ' * entire app syntax checking (using Debug menu's "Compile command) ' * Find and Find/Replace dialog with direction option and scope option ' * help for VBA/VBScript functions and commands ' * immediate window (with a real-time link to the Window-Eyes object model) ' example 1: ' This example shows you the text of a small app which does a few things for Win 7 and Vista, such as making a Windows Explorer window always have it's list ' of files have the initial focus. ' What's different here is that this is the VBA source for the app, which shows all the variables and functions being assigned their proper types. ' (the "save as" hotkey for the app will strip all these out as the code is saved as VBScript) Option Explicit ' written by Chip Orange ( [email protected] ), ' Windows Explorer (initially generated by WEScript Framework) ' The following variables set up specific script information, such as ' the locations to the script INI and XML files. Dim sBase As String Dim myINIFile As String Dim myXMLFile As String Dim myStrings As Scripting.Dictionary Dim checkedForUpdates As Boolean Dim myScriptUpdateURL As String Dim errorReportingEnabled As Boolean Dim SO_StandardHelpDialog As Object Dim SO_CheckForUpdate As Object Dim SO_ErrorReporting As Object Dim conn1 As Integer Dim conn2 As Integer Dim conn3 As Integer Sub main() ' ' The myStrings variable places text strings in the XML file into a ' dictionary object for easy retrieval. External strings allow for ' easy localization into other languages. sBase = clientInformation.scriptPath & "\" & Mid(clientInformation.ScriptFileName, 1, InStrRev(clientInformation.ScriptFileName, ".")) myXMLFile = sBase & "xml" Set myStrings = strings(myXMLFile) ' The ClientInformation object allows us to store commonly referred to ' specific script information, such as the script name, version, and ' description. clientInformation.scriptName = myStrings("SCRIPT_NAME") clientInformation.ScriptVersion = myStrings("SCRIPT_VERSION") clientInformation.ScriptDescription = myStrings("SCRIPT_DESCRIPTION") myINIFile = sBase & "ini" ' The ScriptHelp property of ClientInformation is the action that will ' take place when the Help and Options button is selected in the ' Window-Eyes Script Manager dialog. The string we provide below by ' default indicates that the GW Toolkit needs to be loaded. This ' property will change to the name of our script help routine (defined ' later on in the script) when the required object gets created. clientInformation.ScriptHelp = "GW ToolKit is required." ' checkedForUpdates is a global variable that indicates the automatic ' update check has already taken place (assuming the automatic updates ' have been enabled). checkedForUpdates = False ' The myScriptUpdateURL variable stores the web address for the XML ' file that contains version information. This URL will be used ' when checking for updates. It's stored in a variable because it's ' used in two different GW Toolkit objects. Storing it in one location ' means only having to edit it once if the URL changes. myScriptUpdateURL = "http://www.gwmicro.com/scripts/" & Replace(clientInformation.scriptName, " ", "_") & "/xml" ' errorReportingEnabled is a global variable that indicates whether or not ' custom error reporting (via the GW Toolkit) is enabled. errorReportingEnabled = False ' Global Shared Object objects. These will all be initialized as Nothing, and ' then set to the correct object when OnStateChange fires. Set SO_StandardHelpDialog = Nothing Set SO_CheckForUpdate = Nothing Set SO_ErrorReporting = Nothing ' In this section of the script, we're going to monitor any chages to the ' SharedObjects object so that we can tell when objects come and go. Because ' we're interested in events that occur with SharedObjects, that's the object ' we need to connect to. We're also concerned with the state change of these ' objects, so that is the event we will connect to. Finally, the OnStateChange ' function will be called to handle the information the event provides when ' it fires (i.e. when a state change of a shared object occurs). ConnectEvent SharedObjects, "OnStateChange", "OnStateChange" ConnectEvent clientInformation, "onShutdown", "onShutdown" conn1 = 0 ' watch for newly activated windows (looking for Windows Explorer) conn1 = ConnectEvent(desktopWindow, "onChildActivate", "onChildActivate") conn2 = 0 conn3 = 0 ' --------------- ' OnStateChange is the routine that gets called with the OnStateChange event ' fires from the SharedObjects object. End Sub ' Sub OnStateChange(objName, objState) If objName = "com.GWMicro.GWToolkit.StandardHelpDialog" Then If objState Then Set SO_StandardHelpDialog = SharedObjects(objName, 0).NewDialog SO_StandardHelpDialog.INIFileName = myINIFile SO_StandardHelpDialog.HelpTitle = clientInformation.scriptName & " " & clientInformation.ScriptVersion SO_StandardHelpDialog.HelpText = myStrings("SCRIPT_HELP") SO_StandardHelpDialog.scriptName = clientInformation.scriptName SO_StandardHelpDialog.ScriptVersion = clientInformation.ScriptVersion SO_StandardHelpDialog.FocusCloseButton = True SO_StandardHelpDialog.UpdateUrl = myScriptUpdateURL SO_StandardHelpDialog.UseAboutBox = True SO_StandardHelpDialog.AboutAuthor = "Chip Orange" Dim fsObj: Set fsObj = CreateObject("Scripting.FileSystemObject") SO_StandardHelpDialog.AboutReleaseDate = fsObj.GetFile(clientInformation.scriptPath & "\" & clientInformation.ScriptFileName).DateLastModified Set fsObj = Nothing SO_StandardHelpDialog.AboutCopyright = "" SO_StandardHelpDialog.AboutWebsite = "" ' Change ClientInformation.ScriptHelp to our real routine clientInformation.ScriptHelp = "ScriptHelp" Else ' Change ClientInformation.ScriptHelp to our default message Set SO_StandardHelpDialog = Nothing clientInformation.ScriptHelp = myStrings("GWToolkit_Required") End If ElseIf objName = "com.GWMicro.GWToolkit.CheckForUpdate" Then If objState Then Set SO_CheckForUpdate = SharedObjects(objName, 0).NewCheck If Not checkedForUpdates Then queue "CheckForUpdates" checkedForUpdates = True End If Else Set SO_CheckForUpdate = Nothing End If ElseIf objName = "com.GWMicro.GWToolkit.ErrorReporting2" And objState Then ' The ErrorReporting object was just loaded, so now we can execute ' the code it provides for enhanced error reporting, assuming we haven't ' already done so (which the errorReportingEnabled variable will tell us). ' ErrorReporting is not stored as a global variable, because we only care ' about executing it once, and never again. If Not errorReportingEnabled Then ExecuteGlobal SharedObjects(objName, 0)(clientInformation.ScriptVersion, "[email protected]", True) errorReportingEnabled = True End If End If End Sub Sub CheckForUpdates() ' Before instantiating the object, we need to see if automatic updates ' have been enabled. We do that by checking the Automatic_Updates ' section of our INI file for a key called OnScriptStart. If it exists, ' and the value is 1, then we'll attempt to check for a new version. If INIFile(myINIFile).Number("Automatic_Updates", "OnScriptStart") = 1 Then ' OnScriptStart was on, so we need to make sure the CheckForUpdate ' GW toolkit object is available for use (which it should be since ' this subroutine only gets called when the SharedObject is ' loaded). If Not SharedObjects("com.GWMicro.GWToolkit.CheckForUpdate", 10) Is Nothing Then ' The CheckForUpdate object is available, so we'll create our ' own copy of the object. Dim updateCheck: Set updateCheck = SharedObjects("com.GWMicro.GWToolkit.CheckForUpdate", 10).NewCheck ' Now that we have our copy, we'll provide the required ' information: our script version (which we stored in the ' ClientInformation object in the global variables section, and ' the web address to our XML file, which we also stored in ' the global variables section. updateCheck.ScriptVersion = clientInformation.ScriptVersion updateCheck.UpdateUrl = myScriptUpdateURL ' Now that we've provided all the required inforamtion, we ' have the GW Toolkit check for an update. updateCheck.Check ' Finally, we'll set the global flag that indicates whether or ' not we've already checked for updates to True checkedForUpdates = True Else ' Since we weren't able to get the toolkit object at this point, ' we need to make sure our global flag that indicates whether ' or not we've already checked for updates is set to False. checkedForUpdates = False End If End If End Sub Sub ScriptHelp() SO_StandardHelpDialog.Show End Sub Sub OnChildActivate(win As WindowEyes.window) ' event handler for the desktop window's onChildActivate If win Is Nothing Then Exit Sub On Error Resume Next If Not win.IsValid Then Exit Sub If CheckIfScreenResTrackBar(win) Then ' this is the control panel display properties resolution window ' this part of the script corrects for the control panel display resolution dialog not reading properly, and was originally written by GW Micro for Vista If conn3 = 0 Then ' haven't yet setup to supply field names for this window conn3 = ConnectEvent(win, "OnChildFieldName", "MyOnChildFieldName") conn2 = ConnectEvent(win, "onClose", "myOnClose") queue "speakText", "initializing display settings" ' temporary End If ' conn3 = 0 Exit Sub End If ' CheckIfScreenResTrackBar(Win) ' now see if this window is a Windows Explorer one If StrComp(UCase(win.moduleName), "BROWSEUI") <> 0 Then Exit Sub If StrComp(UCase(win.className), "CABINETWCLASS") <> 0 Then Exit Sub ' this is a Windows explorer Window, find the list view control queue "handleWindowsExplorerWindow", win End Sub Sub myOnClose(theHandle As Long) ' event handler for the display settings window onClose disconnect conn2 disconnect conn3 conn2 = 0 conn3 = 0 queue "speakText", "closing display settings" ' temporary End Sub Sub HandleWindowsExplorerWindow(window As WindowEyes.window) ' search the children of this window, looking for the list of files, which we want to make the focused control when we return to this type of window Dim oChild As WindowEyes.window Dim oChildren As WindowEyes.windows 'On Error Resume Next Set oChildren = window.children For Each oChild In oChildren If Not window.IsValid Then Exit Sub If oChild.Type = wtListView Then If Not oChild.IsValid Then Exit Sub If Not oChild.Accessible Is Nothing Then If oChild.visible And oChild.enabled Then ' this is the list of files in a windows explorer window If Not oChild.Accessible.State.Focused Then oChild.Focus End If Exit Sub End If End If End If Next End Sub Sub OnShutdown() If conn1 <> 0 Then disconnect conn1 conn1 = 0 If conn3 <> 0 Then disconnect conn3 conn3 = 0 End Sub ' now the fix for control panel display resolution (written by GW) for Vista, may also work in Win 7 Function MyOnChildFieldName(win As WindowEyes.window) As String If CheckIfScreenResTrackBar(win) Then ' this is the control panel display properties resolution window On Error Resume Next If Not win.parent.control(1854) Is Nothing Then MyOnChildFieldName = win.parent.control(1854).text & " " & win.parent.control(1814).text Else MyOnChildFieldName = win.parent.control(1818).text & " " & win.parent.control(1814).text End If Else MyOnChildFieldName = vbNull End If End Function Function CheckIfScreenResTrackBar(win As WindowEyes.window) As Boolean ' returns true if this is the control panel display properties resolution window CheckIfScreenResTrackBar = False If win Is Nothing Then Exit Function On Error Resume Next If win.control Is Nothing Then Exit Function ' see if the Id property is present in the control; I've found sometimes it is not (unknown as to why) Dim id: id = -1 id = win.control.id If id = -1 Then Exit Function If id = 1808 Then If win.overlap.Title = "Display Settings" Then CheckIfScreenResTrackBar = True End If ' win.overlap.Title = "Display Settings" End If ' id = 1808 End Function Sub speakText(ByVal msg As String) Speak msg End Sub ' end example 1 ' archives of these classes can be found at: ' https://www.gwmicro.com/App_Central/Developers/Interactive_Classes/
