PDA

View Full Version : Word 2003 macro



james28
10-05-2010, 08:02 AM
Im trying to create a macro which opens a dialog box with a specific path that I specify. The path has templates (.dot) in it which a user will open and use.

For example:

Sub FileOpen()

ChangeFileOpenDirectory "C:\Templates"
Dialogs(wdDialogFileOpen).Show

End Sub

However, if a user opens a template using the above macro, it 'opens' the template itself and not a new instance of it. I would like the dialog box to be a 'File - new' dialog box.

I have also tried:

Sub FileOpen()

ChangeFileOpenDirectory "C:\Templates"
Dialogs(wdDialogFileNew).Show

End Sub

which would open a new instance of the template but I am not able to specify a path on the network using this method. In Word 2003, when you do a File - New, in the right hand task pane you get an option "New from exisiting document". I would like to be able to use this button but then be able to specify a specific path for it to open. Does any one know if this is possible?

Any help will be much appreciated.

Tinbendr
10-05-2010, 09:10 AM
maybe
Sub Test()
ChangeFileOpenDirectory "C:\Templates"
With Dialogs(wdDialogFileOpen)
If .Display = -1 Then
'User clicked OK
Documents.Add Template:=.Name
End If
End With
End Sub

james28
10-06-2010, 03:05 AM
Hi Tinbendr,

Many thanks for the code. I have been testing this since yesterday within a macro button on a Word toolbar.

The code does work, but can be unpredictable. For example, I would use the button to open a document in c:\Templates, which works fine. I would then press the button again to open another document, but instead of opening the directory that I set (c:\Templates), it opens up the My Documents folder.

If I press the button again, it opens the correct folder, but then it seems to go back to the My Documents folder when I press it again. The behaviour seems unpredictable.

Not quite sute why this could be even though the path has been set.

Tinbendr
10-06-2010, 08:42 AM
Try adding
ChDir "C:\Template"

james28
10-07-2010, 04:08 AM
I've tried:

Sub Test()
ChangeFileOpenDirectory "C:\Templates"
With Dialogs(wdDialogFileOpen)
ChDir "C:\Templates"
If .Display = -1 Then
'User clicked OK
Documents.Add Template:=.Name
End If
End With
End Sub

but still the same behaviour. It specifically opens the My Documents folder which is quite strange.

Tinbendr
10-07-2010, 05:21 AM
That's why I said ADD.

I believe that if the recent directory is blank, Word uses the OpenFileDirectory. Try adding the CHDir.

james28
10-07-2010, 05:35 AM
I've tried:

Sub Test()
ChangeFileOpenDirectory "C:\Templates"
With Dialogs(wdDialogFileOpen)
ChDir "C:\Templates"
If .Display = -1 Then
'User clicked OK
Documents.Add Template:=.Name
End If
End With
End Sub

but still the same behaviour. It specifically opens the My Documents folder which is quite strange.

james28
10-07-2010, 06:06 AM
Hi Tinbendr,

Sorry there was a problem with the double posting.

Sub Test()
ChangeFileOpenDirectory "C:\Templates"
With Dialogs(wdDialogFileOpen)
ChDir "C:\Templates"
If .Display = -1 Then
'User clicked OK
Documents.Add Template:=.Name
End If
End With
End Sub

I've added the above line in bold, as you mention. Am I adding this in the correct place?

james28
10-07-2010, 08:51 AM
OK, I think I finally cracked it:

Sub Test()

Dim location As String
location = "C:\Templates"
ChangeFileOpenDirectory (location)
With Dialogs(wdDialogFileOpen)
If .Display = -1 Then
Documents.Add Template:=.Name
End If
End With
End Sub

I havent had any problems yet but it may require more testing.

fumei
10-07-2010, 12:45 PM
Dim location As String
location = "C:\Templates"
ChangeFileOpenDirectory (location)

and..

ChangeFileOpenDirectory "C:\Templates"
are, instructionally identical.

james28
10-08-2010, 03:10 AM
Dim location As String
location = "C:\Templates"
ChangeFileOpenDirectory (location)

and..

ChangeFileOpenDirectory "C:\Templates"
are, instructionally identical.

This may be correct, but the behaviour of the two scripts (when used as a macro in a toolbar) are not the same.

fumei
10-08-2010, 09:29 AM
Oh really? In what way?

james28
10-08-2010, 09:39 AM
In the way that has been mentioned in the posts above and what Tinbendr mentions!

fumei
10-08-2010, 01:20 PM
Sorry, but no they do not. The results of using a string versus using a string variable having the same value are identical. And I can prove it.

In the attached document there are two macro icons on the top toolbar.

WithNoVariable

It executes:Sub TestWithNoVariable()
ChangeFileOpenDirectory "C:\Documents and Settings"
With Dialogs(wdDialogFileOpen)
If .Display = -1 Then
Documents.Add Template:=.Name
End If
End With
' revert back to C:
ChangeFileOpenDirectory "c:\"
' show that it really IS just C:
Dialogs(wdDialogFileOpen).Display
End SubI used Doc and Setting as I know you should probably have that folder.

ChangeFileOpenDirectory "C:\Documents and Settings"

instead of "C:\Templates"

Then the macro icon WithStringVariable. It executes:Sub TestWithStringVariable()

Dim location As String
location = "C:\Documents and Settings"
ChangeFileOpenDirectory (location)
With Dialogs(wdDialogFileOpen)
If .Display = -1 Then
Documents.Add Template:=.Name
End If
End With
' revert back to C:
ChangeFileOpenDirectory "c:\"
' show that it really IS just C:
Dialogs(wdDialogFileOpen).Display
End Sub
The ONLY difference between the two is:

ChangeFileOpenDirectory "C:\Documents and Settings"

versus

Dim location As String
location = "C:\Documents and Settings"
ChangeFileOpenDirectory (location)

The result is EXACTLY the same.

"but the behaviour of the two scripts (when used as a macro in a toolbar) are not the same."

This is incorrect...they ARE the same. Now you may be having issues if you execute some code twice. This is because ChangeFileOpenDirectory premanently changes the value until you change it again. Which is why I avoid it unless forced to.

james28
06-03-2011, 08:48 AM
Hi Fumei,

Apologies for bringing this old post back up but I still haven't resolved this problem & after doing a bit of searching it seems that this is quite a common problem. The code further down seemed to work perfectly in Word 2000 but in Word 2003 the location always defaults to my documents. Fumei is correct that the two sets of code are identical and do produce the same output but as you can see from some of these posts:

http://www.officekb.com/Uwe/Forum.aspx/word-vba/10977/ChangeFileOpenDirectory-doesn-t-always-work

http://windowssecrets.com/forums/showthread.php/63071-ChangeFileOpenDirectory-intermittent-(Word-2000-SP3)

http://www.eggheadcafe.com/software/aspnet/31177815/changefileopendirectory.aspx

it seems that it is a ChangeFileOpenDirectory problem. Fumei does mention that he avoids using it but does anybody know how else I could write this script:

Sub Openfolder()

Dim location As String
location = "C:\Documents and Settings"

ChangeFileOpenDirectory (location)
With Dialogs(wdDialogFileOpen)
If .Display = -1 Then
'User clicked OK
Documents.Add Template:=.Name
End If
End With
End Sub

without using ChangeFileOpenDirectory?

james28
06-06-2011, 06:22 AM
Anyone have any ideas??

Frosty
06-07-2011, 09:28 AM
Can you restate your actual problem? You want to specify a directory to open using the File > Open dialog, *without* defaulting all further File > Opens to the same directory?

Does this link help?
http://en.allexperts.com/q/Microsoft-Word-1058/Using-wdDialogFileFind-return-file.htm

There are so many many ways to do what you're doing, I don't know where to begin, and a couple of google searches will help-- but it seemed the discussion above went a little sideways and I really can't tell for sure what you want done.

You solved your first issue by switching from .Show to .Display.

You solved your next issue by adding a document based on the template, rather than opening the template.

So what's the problem you're trying to solve now?

james28
06-07-2011, 09:48 AM
Hi Frosty,

Thanks for the response.

My problem is this. I would like to create a macro which will open a dialog box to path that I specify. Like you say - there are many ways to do this. However, my problem is that the folder that I open has templates in them (.dot) files. If I do the normal File > Open method, the action will open the template not create a new instance of it. However with Tinbendr's help, I was able to overcome this problem using the ChangeFileOpenDirectory. This worked perfectly in Word 2000.

However, since upgrading to Word 2003, the ChangeFileOpenDirectory becomes unreliable and flaky so some of the time the correct directory would open but most of the time, the My Documents directory opens. As I posted above, many people seem to have the same problem after moving to Office 2003 from an earlier version.

In the end I actually did solve it literally 30 minutes ago. The code is below:


Sub Openfolder()

With Dialogs(wdDialogFileOpen)
.Name = "C:\Templates\"
If .Display = -1 Then
'User clicked OK
Documents.Add Template:=.Name
End If
End With
End Sub

This works perfectly in Word 2003. So what it does is open a directory that you specify - which in my case is a templates folder. When a user clicks the template file, it opens a new instance of the template rather than doing a file open. Seems pretty straightforward - but it did take quite a long time for a novice like me to solve.

Frosty
06-07-2011, 10:05 AM
The problem with builtin dialogs is that they are pretty undocumented. The .Name property might be available on one, .Title or .Caption available on another.

There is also a wdDialogFileFind that you can use, which is better or worse depending on need. The problem in your existing methodology is that you look like you're providing "functionality" you're not actually providing (check out the options under the open button).

And, I'd argue, it's not great to click a button which says "Open" in a dialog which says "File Open" only to create a "new" document on it.

There are ways to change a lot about existing dialogs, but my preferred approach on this is the FileDialog object.

Sub Test()
Dim x As FileDialog

Set x = Application.FileDialog(msoFileDialogFilePicker)

x.InitialFileName = "C:\"
x.ButtonName = "A new button name"
x.Title = "My very own title"
'only want to open one file?
x.AllowMultiSelect = False
x.Show

If x.SelectedItems.Count > 0 Then
MsgBox x.SelectedItems(1)
Else
MsgBox "you hit cancel"
End If
End Sub

There are more options than I'm displaying (allowing multi-select, the other types of FileDialogs, etc), but this gives you an idea.