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

Inserting a File into a Word Document with a Dialog Box 2

Status
Not open for further replies.

pfrancis

Programmer
Mar 18, 2004
19
US
Ok. I know I can use selection.insertfile with the file path to insert a file, but the file that needs to be inserted has a different name each time. Is there a way to open the dialog box to select the file. The box would be the same as if the user had selected the "Insert" menu and then clicked "File..." Thank you for your help.
 
Take a look at Application.Dialogs(wdDialogInsertFile)

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Thank you, this does help. Another question though. If I want to specify a directory to start in and a file type, how do I do that? Thanks again for your help.
 
Hi Francis, [2thumbsup]

This will do what you want:
Code:
Sub INSERTFILE()
sDir = "C:\Documents and Settings\Joost\Bureaublad\Kopie II\*.Doc"

    With Application.Dialogs(wdDialogInsertFile)
        .Name = sDir
        .Show
    End With
End Sub

Enjoy [peace]
Joost Verdaasdonk
 
JVerdaasdonk, Thank you for your help. I tried using what you posted, but I am still not getting what I want. I'm trying to get .txt files. Using the sDir (with *.txt at the end) as the .Name, sometimes I get the right directory and sometimes I don't, but I always get the .txt files. Do you know why this would happen? or if there is another way to set these properties? Thank you again for your help.
 
Depending of the version of word you may consider this:
Application.ChangeFileOpenDirectory "\path\to\otherdir\"

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Hi Francis & PHV, [2thumbsup]

PHV is right this depends on the version. I had no problems with the code above.

But here's another snippet whit a little more effort:
Code:
Sub INSERTFILETXT()
Dim strData As String
Application.ChangeFileOpenDirectory "C:\Documents and Settings\Joost\Bureaublad\Kopie II\"

Dim dlg As Dialog
Set dlg = Dialogs(wdDialogInsertFile)
    With dlg
       .Name = "*.txt"
        If dlg.Show = -1 Then
          strData = .Name
           If InStr(strData, "\") = 0 Then
            strData = CurDir() + "\" + strData
           End If
        End If
    End With
End Sub

Lets hope whe'l strike oil this time!

Enjoy, [peace]
Joost Verdaasdonk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top