PDA

View Full Version : making an open word document active



Blueskies
01-19-2014, 01:40 PM
Hi, This is probably a very simple request but I cannot find an answer anywhere!
I want a user to scroll through the Word documents which are already open and to choose one to look at, ie to make it active. I am using the framework below but cannot get the selected document to be active. Any suggestions very gratefully received.



Dim doc As Document
ireply = 0
For Each adoc In Documents
ireply = MsgBox("Do you want to work with this document?" & adoc, vbYesNo + vbDefaultButton2)
If ireply = vbYes Then

'some code here to make the selected Word document active

End If
Next adoc

Jay Freedman
01-20-2014, 09:46 PM
Dim adoc As Document
Dim ireply As VbMsgBoxResult
For Each adoc In Documents
ireply = MsgBox("Do you want to work with this document?" & _
vbCr & adoc.Name, vbYesNo + vbDefaultButton2)
If ireply = vbYes Then
adoc.Activate
Exit For
End If
Next adoc

Some observations: You declared "doc" but then used "adoc" in the rest of the macro. You didn't declare ireply. Both of these problems (not exactly errors) would be avoided if you put the line


Option Explicit

at the top of the module. There's an option in the VBA editor that will add that line to every new module -- see http://www.word.mvps.org/FAQs/MacrosVBA/DeclareVariables.htm.

There's no reason you need to set ireply = 0 at the start; it doesn't hurt, but it doesn't help, either.

Finally, a question: Are you writing this macro just to learn VBA? If not, then you should know that it's (badly) reinventing the wheel. Every version of Word has a Windows menu or a Switch Windows button (on the View tab) that does the same thing with much less effort for the user.

fumei
01-20-2014, 09:56 PM
I have to agree. Selecting via the Windows menu is better than generating a messagebox to pick yes or no for EACH document. Suppose you want the fifth one. That means you have to iterate through this four times before you get to it. Or if you really want a separate display (rather than using Windows...but why why why), it would be far better to use a userform and show ALL of the documents.

Blueskies
01-21-2014, 02:56 AM
Thanks for all these points, some sloppy coding! Just to explain the background . . . what I want to do is use the code to make it easier for a blind person using a screen reader to move between open files. There doesn't seem to be any Alt key linked to the switch wiondows icon on the ribbon and the Alt+Tab and Shift+Alt+Tab options move you through all the files and programs which are running. I just wanted the user to scroll round the open word docs with the keyboard (with speech from the screen reader) and then select the one which they want to move to.

If the Switch Windows option could be accessed via the keyboard that would be ideal, maybe I can create a new icon to do that which could be accessed with a keyboard shortcut. Thanks for the help anyway, appreciated.

Blueskies
01-21-2014, 08:34 AM
Thanks for all these points, some sloppy coding! Just to explain the background . . . what I want to do is use the code to make it easier for a blind person using a screen reader to move between open files. There doesn't seem to be any Alt key linked to the switch wiondows icon on the ribbon and the Alt+Tab and Shift+Alt+Tab options move you through all the files and programs which are running. I just wanted the user to scroll round the open word docs with the keyboard (with speech from the screen reader) and then select the one which they want to move to.

If the Switch Windows option could be accessed via the keyboard that would be ideal, maybe I can create a new icon to do that which could be accessed with a keyboard shortcut. Thanks for the help anyway, appreciated.

Blueskies
01-21-2014, 08:35 AM
Well actually you can use a keyboard shortcut (Alt+W + W) to access the switch windows tool so will prob resort to that! I might pursue a vba solution too as I was making a userform with a set of tools all in one place. Thanks again for the help.

Jay Freedman
01-21-2014, 10:13 AM
The userform is a better idea. Be sure to test it with JAWS and any other screen reader you can get access to -- most but not all controls are "readable", and there are differences among screen readers.

You can assign a shortcut of your choice to the built-in command WindowList:

In Word 2007, click Office button > Word Options > Customize. In later versions, click File > Options > Customize Ribbon.
Click the Customize button at the bottom of the dialog.
In the Customize Keyboard dialog, make sure the "Save changes in" dropdown is set to the template where the shortcut should be stored.
In the Categories list, click View Tab.
In the Commands list, click WindowList.
Click in the "Press new shortcut key" box, and press the shortcut you want. I suggest Ctrl+Alt+W, which is currently unassigned.
Click the Assign button and click Close.
Save the template.

This command opens a dialog box named Activate rather than the ribbon dropdown. I think that dialog will be screen-reader enabled.