Results 1 to 9 of 9

Thread: Opening a file

  1. #1
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926

    Unanswered: Opening a file

    Hey guys/gals,

    I have a problem where I didn't know of a better place to turn to other than my faithful dbforums board.

    Here's the situation:

    I have the full location and filename stored. I can extract just the filename (i.e. MyTest.Doc) as well as the filetype (i.e. right(xxx,3).

    I have code which determines whether it's xls, doc, ofm, bmp, jpg, visio file, etc..) to shell with the appropriate Program to open the file with.

    The problem I'm having is trying to use the shell command and open the file if the file is stored in: \Documents and Settings\My Documents\MyFileName.doc - the program bombs.

    I also have it so that if the filetype is not recognized, the user can enter the Program Name to execute the file. But for some reason, I can't overcome all the spaces (and possible other user defined drives) in the entire file location. I've always used the shell command but I think there might be other ways or tricks to this.

    Can anyone help please or guide me in the right direction?
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  2. #2
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    Had this site stashed in my tip folder. Don't know if it'll help (I've never used it) but you might look at it:

    http://www.devx.com/tips/Tip/5756
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  3. #3
    Join Date
    Apr 2004
    Location
    Derbyshire, UK
    Posts
    789
    Provided Answers: 1
    Hi

    Not sure what your problem is but this works for me

    Code:
    Sub testWord()
        OpenWordDoc "H:\URLsl.doc"
    End Sub
    
    Sub OpenWordDoc(ByVal DocPathName As String)
        Dim x As Variant
        Const cWordPath As String = "C:\Program Files\Microsoft Office\OFFICE11\winword.exe"
        
        x = Shell(cWordPath & " " & DocPathName, vbMaximizedFocus)
        
    End Sub

    ??


    MTB

  4. #4
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    Thanks for the link Misslinglinq (I looked at it and tried it but it didn't seem to work - I was hopeful though). MTB - I need to be able to open any file, not just worddocuments. Are you saying that your code could be utilized to open say bmp, jpg, xls, etc. files?

    Any other suggestions please?
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  5. #5
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    Couple of thoughts. First, have you tried FollowHyperlink, which should open the referenced file in whatever program the extension is associated with? Second, you can get around the problem of having spaces in the path by surrounding the path with Chr(34).
    Paul

  6. #6
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    Paul - thanks - no I didn't think about the FollowHyperlink option (although I should have) - thanks again. If that fails, I'll try surrounding the path with the chr(34) (something I didn't know about and have to experiment on how to do it as I've never done it - you wouldn't happen to have an example by chance would you?).

    Great suggestions!!!

    Thanks much!
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  7. #7
    Join Date
    Jan 2004
    Posts
    4

  8. #8
    Join Date
    Apr 2004
    Location
    Derbyshire, UK
    Posts
    789
    Provided Answers: 1
    Hi

    Being as I had done most of the work before the last two posts here is what I have

    Code:
    Sub TestOpenDoc()
        'OpenDocument "H:\CES101765 Ord SRM.doc"
        'OpenDocument "H:\London - Sickness Record 2005-06.xls"
        OpenDocument "C:\WINDOWS\Gone Fishing.bmp"
        'OpenDocument "C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water lilies.jpg"
    End Sub
    
    Sub OpenDocument(ByVal FilePathName As String)
        Dim x As Variant
        Dim Extension As String
        
        If Dir(FilePathName) = "" Then
            MsgBox "File not found"
            Exit Sub
        End If
        
        Extension = UCase(Right(FilePathName, 3))
    
        Select Case Extension
            Case Is = "XLS"
                x = Shell("C:\Program Files\Microsoft Office\OFFICE11\excel.exe " & Chr(34) & FilePathName & Chr(34), vbMaximizedFocus)
            Case Is = "DOC"
                x = Shell("C:\Program Files\Microsoft Office\OFFICE11\WINWORD.exe " & Chr(34) & FilePathName & Chr(34), vbMaximizedFocus)
            Case Is = "BMP", "JPG"
                x = Shell("C:\WINDOWS\system32\mspaint.exe " & Chr(34) & FilePathName & Chr(34), vbMaximizedFocus)
            Case Else
                MsgBox "File type '" & Extension & "' not recognised!"
        End Select
    End Sub
    This open the 4 file types (using mspaint for bmp & jpg).
    I did have trouble with spaces in the names until pkstorm tip (I like to think I would have got there on my own, but not garentees!).

    I also tried Application.FollowHyperlink FilePathName, , True but this producted direwarning from microst about opening jpg & bmp files which I could not find a way to stop. Any suggestion anybody?

    HTH


    MTB

  9. #9
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    Again, Thanks much!!
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •