Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Path - find a file

Posted on 2002-04-07
3
Medium Priority
?
160 Views
Last Modified: 2010-05-02
Hi anyone
I would like to click on a button and get it to open a word document. At present if I hard-code the full path as in the procedure (below) it works, but I would like it to be able to find the document and open it, if it is on any other drive, directory. I have been trying "Path" as commented out below - but don't quiet seem to have it.

Any ideas?

Private Sub cmd_Click()
    Dim wrd As Object
    Set wrd = CreateObject("Word.Application")
    wrd.Visible = True
    wrd.Documents.Open "C:\Team\club.doc"
   
    'wrd.Documents.Open Filename:=wrd.Path & "\club.doc"
    Set wrd = Nothing
End Sub

Regards
admoller
0
Comment
Question by:admoller
3 Comments
 
LVL 54

Expert Comment

by:Ryan Chong
ID: 6924443
Hi admoller,

Take a look at http://www.freevbcode.com/ShowCode.Asp?ID=631
All common dialogs plus some extra dialogs

or simply add the M$ Common Dialog Control from the
Component. Use like:

Private Sub cmd_Click()
   Dim wrd As Object
   Set wrd = CreateObject("Word.Application")
   wrd.Visible = True
commondialog1.FileName = ""
commondialog1.ShowOpen
If commondialog1.FileName <> "" Then
   wrd.Documents.Open "C:\Team\club.doc"  
Else
   Exit Sub
End If  
   'wrd.Documents.Open Filename:=wrd.Path & "\club.doc"
   Set wrd = Nothing
End Sub

Cheers
0
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 200 total points
ID: 6924498
This should search a drive for a file then open it.

Private Sub cmd_Click()
Dim wrd As Object
Set wrd = CreateObject("Word.Application")
wrd.Visible = True
wrd.Documents.Open PathTo("club.doc")
End Sub

Function PathTo(strFile As String) As String
Dim x As Integer
Dim strDirs As String
Dim strDir As String
Dim strEntry As String
'Replace 'C:\' with the drive you want to search.
strDirs = "C:\" & vbNullChar
Do While Len(strDirs)
x = InStr(strDirs, vbNullChar)
strDir = Left$(strDirs, x - 1)
strDirs = Mid$(strDirs, x + 1)
If Len(Dir$(strDir & strFile)) Then
PathTo = strDir & Dir$(strDir & strFile)
Exit Function
End If
strEntry = Dir$(strDir & "*.*", vbDirectory)
Do While Len(strEntry)
If (GetAttr(strDir & strEntry) And vbDirectory) Then
If strEntry <> "." And strEntry <> ".." Then
  strDirs = strDirs & strDir & strEntry & "\" & vbNullChar
End If
End If
strEntry = Dir$
Loop
Loop
PathTo = ""
End Function
0
 

Author Comment

by:admoller
ID: 6925117
Thanks vinnyd79 - that worked, although I thought there would have been some inhouse function (I thought it was "path") for this.

Would have been nice if it also checked other drives, maybe that could be for me to find out!

Regards
admoller
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question