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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting a user form to open with a document.

Status
Not open for further replies.

mych

Programmer
May 20, 2004
248
GB
I have created a Word document with a userform that has several macros associated to cmand buttons on the form.

How can I get the form to appear when the document is opened?

Thanks

Mych
 
hi
would this not work?
Code:
Private Sub Document_open()
UserForm1.Show
End Sub

;-)
If a man says something and there are no women there to hear him, is he still wrong? [ponder]
How do I get the best answers?
 
Thanks...Loomah

Not done VBA in a while bit rusty....

Another Quick Question....

I have recorded a macro to open the FileOpen dialog and open a file. What I want to do is open the dialog from a cmd button move to a specific directory say C:CSVFiles and then allow the user to select a file themselves.

Can this be done....

I tried using the recorded macro as a template but got a bit stuck....

Mych
 
Have you tried setting the InitialFilename property of the FileDialog?


-V
 
The recorded macro gave me this...

Code:
Sub OpenFile()
'
' OpenFile Macro
' Macro recorded 26/07/07 by Mych
'
    Documents.Open FileName:="C:CSVFile\[red]File1.csv[/red]", ConfirmConversions:=False, _
        ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
        PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
        WritePasswordTemplate:="", Format:=wdOpenFormatAuto
End Sub

I removed the bit in red thinking that this would then let the user select the file he needs to open... but no good.

Mych
 
Try playing around with

Code:
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogOpen)
fd.InitialFilename = "yourfilename"
If fd.Show Then
    Documents.Open fd.SelectedItems(1)
Else
    Msgbox "Open cancelled"
    End
End If


-V
 
There a number of ways of going about this. An alternative could be:
Code:
Dim strOldLocation As String
strOldLocation = Options.DefaultFilePath(wdDocumentsPath)
With Application
    .ChangeFileOpenDirectory "c:\test"
    .Dialogs(wdDialogFileOpen).Show
End With
Application.ChangeFileOpenDirectory strOldLocation
This will change the FileOpen directory temporarily, then opens the FileOpen dialog.


The procedure stores the current FileOpen location, resets it, displays the FileOpen dialog, and at the end, returns the FileOpen directory to its original location. It depends on what your requirements are. Using ChangeFileOpenDirectory will keep that change for the current Word session.

In the case of the code above, it is immediately changed back, so the change really is only active for the life (scope) of the procedure.

faq219-2884

Gerry
My paintings and sculpture
 
thanks guys

I went with Fumei as it allowed the user to select the file.

Many thanks
Mych
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top