Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Path - find a file

Posted on 2002-04-07
3
Medium Priority
?
159 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
[X]
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
3 Comments
 
LVL 53

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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

721 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