+ Reply to Thread
Results 1 to 3 of 3

Open PDF File with vba button

Hybrid View

  1. #1
    Registered User
    Join Date
    02-24-2015
    Location
    bangkok
    MS-Off Ver
    2013
    Posts
    46

    Open PDF File with vba button

    Hello,

    i have a code that opens a word document from a path written in a certain cell.
    currently it opens only word documents
    is there a way to adujst it so it will open pdf files alos?

    here is the code:
    'Reference : Microsoft Word 16.0 Object Library
    '----------------------------------------------
    
    Sub infotour()
        Dim strFName As String
        Dim objWord, objDoc
    
        strFName = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\Excel\" & Sheet11.Range("P21").Value
    
        If FileExists(strFName) And strFName <> "" Then
            If Not IsFileOpen(strFName) Then
                Set objWord = CreateObject("Word.Application")
                Set objDoc = objWord.Documents.Open(strFName)
                objWord.Visible = True
            Else
                MsgBox "The File Is Already Open", vbExclamation
            End If
        Else
            MsgBox "The File Does Not Exist!", vbExclamation
        End If
    End Sub
    
    Function FileExists(strfullname As String) As Boolean
        FileExists = Dir(strfullname) <> ""
    End Function
    
    Function IsFileOpen(fName As String) As Boolean
        Dim i As Integer, errNum As Integer
    
        If Dir(fName, 0) = "" Then
            IsFileOpen = False: Exit Function
        End If
    
        On Error Resume Next
            i = FreeFile
            Open fName For Input Lock Read As #i
            Close i
            errNum = Err
        On Error GoTo 0
    
        IsFileOpen = (errNum <> 0)
    End Function

    Thanks in Advance,

    Avi

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Open PDF File with vba button

    Hi Avi,

    Try the following, which should open any file with the default application for that file:
    'Replace:
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Open(strFName)
    objWord.Visible = True
    
    'With:
    ThisWorkbook.FollowHyperlink strFName, NewWindow:=True
    Lewis

  3. #3
    Registered User
    Join Date
    02-24-2015
    Location
    bangkok
    MS-Off Ver
    2013
    Posts
    46

    Re: Open PDF File with vba button

    Thank you very much,

    works perfectly.


    Avi

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Button to open a word file from path value
    By aviko150 in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 10-01-2016, 01:22 PM
  2. [SOLVED] Have form and button need VBA code to open file
    By dariusd in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-08-2015, 05:15 PM
  3. Open an .exe file using button
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-13-2014, 02:32 AM
  4. Open file with command button... but...
    By jfoerch in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-27-2011, 10:33 AM
  5. Macro Button To open PDF File
    By Denzil in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-18-2008, 10:28 AM
  6. [SOLVED] Button to open excel file
    By Koataus in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-26-2005, 05:19 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1