Solved

Path - find a file

Posted on 2002-04-07
3
151 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 49

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 50 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now