Solved

Path - find a file

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

896 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

18 Experts available now in Live!

Get 1:1 Help Now