Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Path - find a file

Posted on 2002-04-07
3
154 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 51

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…

861 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