+ Reply to Thread
Results 1 to 20 of 20

Looking for a macro to open a pdf file based on a cell value please

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-03-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    246

    Looking for a macro to open a pdf file based on a cell value please

    In my worksheet I have a macro that saves the data to a pdf file.
    I need a macro that will open one of the saved pdf files based on the value in a cell.
    I have tried searching for code but I can't find one that does exactly what I want.
    Sample file attached. Many thanks in advance.
    Attached Files Attached Files

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,343

    Re: Looking for a macro to open a pdf file based on a cell value please

    For starters.
    Sub Call_PDF()
    On Error Resume Next
    With Sheets("Sheet1")
        fName = "PO#" & " " & .Range("F6") & " " & .Range("G6") & " " & .Range("H6") & ".pdf"
    End With
    ThisWorkbook.FollowHyperlink "C:\Users\Al\Dropbox\Backup\PO\" & fName
    End Sub
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Forum Contributor
    Join Date
    11-03-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    246

    Re: Looking for a macro to open a pdf file based on a cell value please

    Hi bakerman2, Thanks for your reply but sadly nothing at all happens!!!

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,343

    Re: Looking for a macro to open a pdf file based on a cell value please

    If fName doesn't reflect the correct filename then nothing happens indeed.

    Sub Call_PDF()
    On Error Resume Next
    With Sheets("Sheet1")
        fName = "PO#" & " " & .Range("F6") & " " & .Range("G6") & " " & .Range("H6") & ".pdf"
        msgbox fName
    End With
    ThisWorkbook.FollowHyperlink "C:\Users\Al\Dropbox\Backup\PO\" & fName
    End Sub
    Does the msgbox reflect correct filename ?

  5. #5
    Forum Contributor
    Join Date
    11-03-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    246

    Re: Looking for a macro to open a pdf file based on a cell value please

    Hi bakerman2, Yes, message box displays the correct file name.

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,343

    Re: Looking for a macro to open a pdf file based on a cell value please

    Another one that works for me.
    Sub tst()
        fPath = "C:\Users\Al\Dropbox\Backup\PO"
        With Sheets("Sheet1")
            fName = "PO#" & " " & .Range("F6") & " " & .Range("G6") & " " & .Range("H6") & ".pdf"
        End With
        ShowPDF = Shell("C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" & " " & fPath & "\" & fName, 4)
    End Sub

  7. #7
    Forum Contributor
    Join Date
    11-03-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    246

    Re: Looking for a macro to open a pdf file based on a cell value please

    Hi bakerman2, Sorry. This time it will start acrobat reader dc but with the error message "There was an error opening this document. This file cannot be found.
    I have tried all your suggestions in two workbooks, each macro gives the same result for each workbook.

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,343

    Re: Looking for a macro to open a pdf file based on a cell value please

    I'm 200% certain that both codes work so either your filename isn't correct or the path you provided in your example file is wrong.

  9. #9
    Forum Contributor
    Join Date
    11-03-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    246

    Re: Looking for a macro to open a pdf file based on a cell value please

    Ok, I'll do some investigation. Thank you for your help today, much appreciated.

  10. #10
    Forum Contributor
    Join Date
    11-03-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    246

    Re: Looking for a macro to open a pdf file based on a cell value please

    Hi bakerman2, I had a good look at this last night and did find one or two issues with the file names, trailing spaces and the routine I use to save my files to pdf actually adds - in between the ranges. However I have tried using the insert hyperlink method only to find that they will not work either, error message saying "Cannot open the specified file", I also tried using hyperlinks to numerous pdf files not created by the Excel macro I use to save to pdf and they all open fine. Do you think that the macro I use to save Excel to pdf can be rectified to make them work?

  11. #11
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Looking for a macro to open a pdf file based on a cell value please

    Does this work?

    Option Explicit
    
    Private Const mstrcPATH_SEPARATOR As String = "\"
    
    Public Sub Test()
        Dim strErrMsg As String
        Dim strPathPDFs As String
        Dim strFileNamePDF As String
        Dim strFullNameReader As String
    
        ' set values
        With Worksheets("Sheet1")
            strPathPDFs = .Range("E12").Value2
            strFullNameReader = .Range("E10").Value2
            strFileNamePDF = "PO#" & " " & .Range("F6").Value & " " & .Range("G6").Value & " " & .Range("H6").Value & ".pdf"
        End With
    
        ' verify exist
        strErrMsg = "PDF Reader program not found at " & strFullNameReader
        If Not fnblnExistsFileFolder(strFullNameReader) Then
            GoTo ExitProcedure
        End If
        strErrMsg = "PDF output path not found at " & strPathPDFs
        If Not fnblnExistsFileFolder(strPathPDFs) Then
            GoTo ExitProcedure
        End If
    
        ' ensure path separatored and fix for spaces when used in Shell
        strPathPDFs = fnstrGetSeparatoredPath(strPathPDFs)
        strFullNameReader = """" & strFullNameReader & """"
    
        ' ensure pdf exists
        strErrMsg = "PDF file was not found at " & strPathPDFs & strFileNamePDF
        If Not fnblnExistsFileFolder(strPathPDFs & strFileNamePDF) Then
            GoTo ExitProcedure
        End If
        strErrMsg = vbNullString
    
        ' open Pdf
        Shell strFullNameReader & " " & """" & strPathPDFs & strFileNamePDF & """", vbNormalFocus
    
    ExitProcedure:
        If LenB(strErrMsg) Then
            MsgBox strErrMsg, vbExclamation
        End If
    End Sub
    
    Public Function fnblnExistsFileFolder(ByRef strFullName As String) As Boolean
    'adapted from function written by Ken Puls (www.excelguru.ca)
        If LenB(strFullName) Then
            On Error Resume Next
            fnblnExistsFileFolder = LenB(Dir(strFullName, 31))
            On Error GoTo 0
        End If
    End Function
    
    Public Function fnstrGetSeparatoredPath(ByVal strDirPath As String, Optional ByVal blnInvert As Boolean) As String
    '/ ensures folder path ends in path separator (aka trailing backslash)
    '/ doesn't detect garbage input, requires a Path arg
        Dim blnChange As Boolean
    
        strDirPath = Trim$(strDirPath)
        If LenB(strDirPath) Then
            blnChange = ((Right$(strDirPath, 1) = mstrcPATH_SEPARATOR) = blnInvert)
            If Not blnChange Then
                fnstrGetSeparatoredPath = strDirPath
            ElseIf Not blnInvert Then
                'add trailing separator
                fnstrGetSeparatoredPath = strDirPath & mstrcPATH_SEPARATOR
            Else
                'remove last character
                fnstrGetSeparatoredPath = Left$(strDirPath, Len(strDirPath) - 1)
            End If
        End If
    End Function
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  12. #12
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,343

    Re: Looking for a macro to open a pdf file based on a cell value please

    Post the code U use to save to pdf.

  13. #13
    Forum Contributor
    Join Date
    11-03-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    246

    Re: Looking for a macro to open a pdf file based on a cell value please

    Hi bakerman2, I have attached a workbook with the code in a module as I don't seem to be able to post the code for some reason. The cell refs will all be different to the sample file I sent you in the first instance.
    Attached Files Attached Files

  14. #14
    Forum Contributor
    Join Date
    11-03-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    246

    Re: Looking for a macro to open a pdf file based on a cell value please

    mc84excel, Thanks for your code, but no luck, I will open other files but not ones created by the macro I have used to create pdf from my worksheet. Many thanks anyway.

  15. #15
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,343

    Re: Looking for a macro to open a pdf file based on a cell value please

    1. Where does 'currentmonth' come from ??

    2. Where is 'AlwaysOverwritePDF' set ??

  16. #16
    Forum Contributor
    Join Date
    11-03-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    246

    Re: Looking for a macro to open a pdf file based on a cell value please

    Hi bakerman2, I am so sorry I posted the wrong code in the workbook this morning, please see new Revised Save2PDF file.
    I'm afraid I don't know what any of the code really mean, I can change the cell refs and file paths, that's about it.
    Attached Files Attached Files

  17. #17
    Forum Contributor
    Join Date
    11-03-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    246

    Re: Looking for a macro to open a pdf file based on a cell value please

    bakerman2, mc84excel, I have actually got mc84excel's code to work this evening, have renamed all my files without the hyphens and removed the hyphens from the macro that saves to pdf from worksheet. I will do further checks before I mark thread as solved. Fingers x

  18. #18
    Forum Contributor
    Join Date
    11-03-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    246

    Re: Looking for a macro to open a pdf file based on a cell value please

    mc84excel, Many thanks for your code, I now have it working. Much appreciated.

  19. #19
    Forum Contributor
    Join Date
    11-03-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    246

    Re: Looking for a macro to open a pdf file based on a cell value please

    bakerman2, Thank you for your help I have it sorted now with mc84excel's code. Thank you for your time.

  20. #20
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,343

    Re: Looking for a macro to open a pdf file based on a cell value please

    You're welcome and thanks for rep+.

+ 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. Use VBA to open a PDF based on the file name typed into a cell
    By stormhayward in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 08-11-2017, 09:49 AM
  2. [SOLVED] Macro to open pdf file based on cell value
    By laxminarayana in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-25-2016, 10:30 AM
  3. Macro to open a file and folder that has date in the path based on a cell value
    By mm671750 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-07-2015, 06:31 PM
  4. How to get macro to open file name based on today's date?
    By ytc1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-05-2013, 05:37 AM
  5. Macro to open pdf file based on a cell value.
    By martin61 in forum Excel General
    Replies: 0
    Last Post: 12-15-2011, 11:48 PM
  6. Macro to open a file, input value into a cell, and run a macro from file
    By wkhor in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-11-2011, 03:00 AM
  7. Macro to open a file and search based on condition
    By jhc8255 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-15-2010, 01:33 AM

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