Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Switching from File to File in Word 1

Status
Not open for further replies.

anastasia

Programmer
Dec 13, 2000
111
GB
Hi,

I am wondering what the VBA code is for switching between files that have been opened in Word.

I have two files that have been opened in Word and you can switch between then from the File menu. I have a command button on one file and when the user clicks this button I want the other File to open, and vice versa instead of the user having to use the File menu way.

I have tried certain code but there always seem's to be something missing.

Thanks.
 
Anastasia,

It sounds like you are referring to the list of "most recently used" (mru) files that appear on the File menu. As you probably know, this list changes whenever new files are opened. If you are trying to have two Word documents (files) open each other, then you probably want to hard-code their filenames into an OnClick event handler for the command buttons. If you really do want to reference the mru files you can use the RecentFiles property of the Application object. Please advise - Posting code you've already tried wouldn't hurt, either. [smile]


Regards,
M. Smith
 
This should work - will need to be in both docs - also assumes that there are only 2 docs open

Sub ChangeWin()

currWin = ActiveWindow.Index
If currWin = 1 Then
Windows(2).Activate
Else
Windows(1).Activate
End If
End Sub

HTH
Geoff
 
xlbo,

Tried your code above but not working, in the File menu it shows 4 files instead of 2.

Mike,

I have just tried opening the file with the Open command etc for Application or Document but not quite sure on the complete syntax.

which would be best referencing the menu or file?.
 
Anastasia,

FYI. You are using the most recently used file listing on the File menu in a rather unconventional way! Normally, you click on one of these files to quickly open a previously opened file, although apparently it also serves to switch between open documents (files). The Windows menu is for switching between documents that are already open. That is the point of Geoff's posting. What I was getting at in my first response is that you don't want to use the mru to open two particular files, since those are always changing. To open a particular file use this syntax:

Code:
Document.Open Filename:="MyDocument.doc"

This can be called from your command button's OnClick event handler. For example, you have two documents Doc1 & Doc2, each with a command button that you want to open the other.

For Doc1 you have the following code:

Code:
Private Sub CommandButton1_Click()
  Document.Open Filename:="Doc2.doc"
End Sub

In Doc2 you would have:

Code:
Private Sub CommandButton2_Click()
  Document.Open Filename:="Doc1.doc"
End Sub

Hope this is what you are looking for.

M. Smith
 
Anastasia,

Because you haven't yet responded, I'm wondering if you encountered the same problem I did. It "might be me", but when I tried Mike's (welcomed) routine, I ran into the error "Object required". This happened whether both files were loaded into memory or not.

In the meantime, here's another option / suggestion...

PREFACE: Most Excel users appreciate being able to use <Alt> <Tab> to &quot;hotkey&quot; from one workbook to the next, or from one worksheet to the next (when there are two worksheet windows open).

WISH LIST: Most Word users &quot;envy&quot; this <Alt> <Tab> method used in Excel, and &quot;wish&quot; MS had created this in Word.

SOLUTION: Use the following steps to create a &quot;similar&quot; hotkey for use in Word. First, the &quot;recommended hotkey&quot; is the use of the key immediately above the <Tab> key - i.e. the key with the <`> in the &quot;lower-case&quot; position on the key.

Thus, you would (with one hand) hold down the <Alt> key and hit the <`> key. This is &quot;easy and quick&quot; - especially for those MS Office users who are used to using <Alt> <Tab> in Excel.

STEPS:

1) From the menu, select: Tools – Customize, and then Click the “Commands” tab.
2) Under “Categories”, click “Window and Help”.
3) On the right side (under “Commands”), click-and-drag “Previous Window” and add it to one of your existing toolbars.
4) Right-click on the “Previous Window” icon you just added.
5) Click in the “Name” box (3rd from the top), and add the ` character to the right of the & .
6) You can also “shorten” or abbreviate the “&`Previous Window” – if you need to “get it to fit” on your toolbar.

OTHER SHORTCUTS ??? I don’t use Word much, but my wife does (at 120 wpm error-free), and she REALLY appreciates the shortcuts I set up for her. In particular, she appreciates:
1) <Alt> - (minus key) ...for decreasing the font size, and
2) <Alt> + (plus key) ...for increasing the font size.

Hope this helps. :) Please advise as to what your thoughts are on the use of the &quot;hotkeys&quot;. I'm a firm believer that &quot;once shown how fast and easy these hotkeys really are&quot;, MOST Word users will &quot;start bowing (-: when you come near&quot;.

Regards, ...Dale Watson dwatson@bsi.gov.mb.ca
 
Dale,

You are correct regarding my code snippet. Document should be Documents.

Thanks for the shortcut ideas!

M. Smith
 
Anastasia,

A correction... (My mind is &quot;more awake now&quot;) %-)

I referred to using the <Alt> <Tab> hotkey in Excel to go from Worksheet to Worksheet, or from Workbook to Workbook. I should have said... <Control> <Tab>.

<Alt> <Tab> as you probably know, takes you from &quot;Window to Window&quot;.

Regards, ...Dale Watson dwatson@bsi.gov.mb.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top