# Hyperlink to specific .pdf page from Excel



## laguy83

Hi All,

I am trying to create a hyperlink in Excel that will open up a specific page from a .pdf file stored on a network drive. Unfortunately I am only able to get the pdf file to open to the first page. Any help would be greatly appreciated!!!!

Thanks!


----------



## WendyM

Hi laguy,
You just need to add #page=[page number] at the end of your link. So if your file is http://www.something.com/file.pdf, and you want it to open to page 3, it would be http://www.something.com/file.pdf#page=3. Hope that helps.


----------



## Zack Barresse

Wow! Nice Wendy!! I was trying to figure out how to do it via the Object Model. That's so simple!! Learn something new each day.


----------



## laguy83

Thanks for your help, WendyM. Unfortunately, the problem I have is that the pdf file I am linking to is not online, but rather is stored on a local drive. For some odd reason Acrobat does not allow you to link to individual pages when the pdf file is stored on a local drive - only when it is stored online. Is there another solution, for locally-stored pdf files? Thanks so much.


----------



## computerman29642

Give this code a try



 Code:


Sub OpenPDFpage()
    Dim myLink As String
    Dim TargetPage As Double
    Dim objIE As New InternetExplorer

    [B]myLink = "path/filename.pdf"[/B] *Replace with your PDF path destination
    TargetPage = 7   'Page number to be shown

    With objIE
        .Navigate myLink & "#page=" & TargetPage
        .Visible = True
    End With
End Sub

This code was found at the link below:

http://excel.tips.net/Pages/T003350_Linking_to_a_Specific_Page_in_a_PDF_File.html


----------



## laguy83

Thank you so much for your help computerman. But at the risk of appearing like a total novice....i've never used code before. Can you tell me how exactly I go about using this code, or at least point me in the direction of an FAQ or tutorial or something? I'm good with computers but just have no training in code or macros....


----------



## computerman29642

Do you want to run the code through a button, or by clicking on something in a cell?


----------



## Zack Barresse

CM, you're getting fast!! Was working on something, but ya beat me to it! Here it uses late binding though, no reference needed...



Code:


Sub TestPDFlink()
    Dim IE As Object, strFile As String, iPageNum As Long
    strFile = "C:\Users\Zack\Desktop\OpenPDF\Book1.pdf"
    iPageNum = 2
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Navigate strFile & "#Page=" & iPageNum
    IE.Visible = True
End Sub

Just copy/paste code into a standard module (Alt + F11, Alt + I, M).

HTH


----------



## laguy83

I would like to just click on a link in the cell and have it open up the pdf file...is that what you mean?


----------



## computerman29642

Zack, you are the MAN!!! 

Can you explain what you mean by late binding?


----------



## Zack Barresse

You would need a button. The functionality of an inserted hyperlink doesn't give you what you want, but the VBA code does (hence the button). Or you could write more code to if you select that cell, or double click it, or whatever.


----------



## computerman29642

Zack, I was actually working on the double-click option.


----------



## Zack Barresse

computerman29642 said:


> Zack, you are the MAN!!!
> 
> Can you explain what you mean by late binding?


Yup. Late binding does not use references. Early binding does. VBE | Tools | References. All those listed are object models you can reference to your current project (whatever the active project is). Doing so will give you use of the object library and intellisense. When you see my code, I declared the variable as an Object, plus I used the CreateObject() call, which creates the object (_if installed on the host computer_). The code you had, you would've needed to go to Tools | References, then check the *Microsoft Internet Controls* control. Then you can use something like *Dim IE as InternetExplorer*, because traditionally it's not available to the Excel Object Model.

I prefer late binding because it transports to any machine, no need to set references, sometimes references can come up missing if somebody doesn't have a full install, or just versioning you can come up with reference(s) issues. Early binding can be nice, if it is a solution which only you will run and the solution won't move around. If I'm posting to the board and I don't know that, generally I'll use late binding.

Binding is just referring to how you are bound to an object (i.e. what object library you are referencing). Make sense?


----------



## computerman29642

Thanks Zack. Yes, I believe I get what you are saying. If not, I know who to come and ask..LOL!


----------



## laguy83

Thank you guys all so much. I really appreciate your efforts. 

Zack - I used the code you posted:

Sub TestPDFlink()
Dim IE As Object, strFile As String, iPageNum As Long
strFile = "C:\Users\Zack\Desktop\OpenPDF\Book1.pdf"
iPageNum = 2
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate strFile & "#Page=" & iPageNum
IE.Visible = True
End Sub

However, although it does open up the pdf file, it still only takes me to the first page. Any idea how I can target a specific page?

Thanks again!

Brian


----------



## computerman29642

Laguy83, would double-clicking the cell work for you?


----------



## computerman29642

I have tested the code, and it works for me.

What number do you have after this line of code "iPageNum ="?

What is the range of cells that the code will need to apply? Is it one cell or multiple cells?


----------



## WendyM

Zack Barresse said:


> Wow! Nice Wendy!! I was trying to figure out how to do it via the Object Model. That's so simple!! Learn something new each day.


Yeah, but so much for my bright idea!


----------



## Zack Barresse

Works for me Wendy! Always wanted to know how to do that.. never thought of looking at URLs.


----------



## laguy83

I have entered the code exactly as follows:

Sub TestPDFlink()
Dim IE As Object, strFile As String, iPageNum As Long
strFile = "\\sli\pub\AUI\Brian\SKMBT_C35308122216410.pdf"
iPageNum = 2
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate strFile & "#Page=" & iPageNum
IE.Visible = True
End Sub


The file opens up in Acrobat, not IE. Is this part of the problem?


----------



## laguy83

Oh and computerman to answer your question I want the code to apply to one individual cell. I will have many individual cells, each linking to a different pdf file/page number.


----------



## computerman29642

laguy83 said:


> I have entered the code exactly as follows:
> 
> Sub TestPDFlink()
> Dim IE As Object, strFile As String, iPageNum As Long
> strFile = "\\sli\pub\AUI\Brian\SKMBT_C35308122216410.pdf"
> iPageNum = 2
> Set IE = CreateObject("InternetExplorer.Application")
> IE.Navigate strFile & "#Page=" & iPageNum
> IE.Visible = True
> End Sub
> 
> The file opens up in Acrobat, not IE. Is this part of the problem?


When I test the code, it opens in Acrobat as well. Forgive me for asking such a stupid question, but the PDF does have more than one page....right?


----------



## computerman29642

laguy83 said:


> Oh and computerman to answer your question I want the code to apply to one individual cell. I will have many individual cells, each linking to a different pdf file/page number.


So, the double-clicking of the cells would work for you...correct?


----------



## laguy83

Yes double clicking the cells would be fine. And Wendy to answer your question the pdf I am working with has 4 pages.


----------



## computerman29642

Would it be possible for you to attach sample files?


----------



## computerman29642

Zack, what would be the best way to handle the different cells pointing to different pages with the double-clicking?

I was just going to do something like



Code:


Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    
    Dim IE As Object, strFile As String, iPageNum As Long

    If Range("C3").Value <> "" Then
        strFile = "C:\Test.pdf"
        iPageNum = 2
        Set IE = CreateObject("InternetExplorer.Application")
        IE.Navigate strFile & "#Page=" & iPageNum
        IE.Visible = True
    End If

End Sub

Assuming the cell is suppose to contain some type of value.


----------



## laguy83

yikes that's a tough one...my docs all contain confidential info. I could attach a generic test document if that suits your purposes...


----------



## computerman29642

laguy83 said:


> yikes that's a tough one...my docs all contain confidential info. I could attach a generic test document if that suits your purposes...


I guess not. I am just trying to figure out why the code does not work when you run it.


----------



## computerman29642

Every PDF I have tested with so far, the code has worked properly. Are you sure you are using the code to open the PDF?


----------



## laguy83

Here is a 2-page pdf that should be ok.


----------



## computerman29642

laguy83 said:


> Here is a 2-page pdf that should be ok.


Works fine.


----------



## laguy83

It opens up in Adobe Acrobat 8 Professional.


----------



## laguy83

So when you do it on your machine you are able to open it up to page 2?


----------



## computerman29642

laguy83 said:


> So when you do it on your machine you are able to open it up to page 2?


That would be correct.


----------



## computerman29642

laguy83 said:


> It opens up in Adobe Acrobat 8 Professional.


The version of Adobe Acrobat should not matter, I do not think.


----------



## laguy83

Man I don't know.... Just so I make sure I'm doing it correctly, you run the code by clicking the "play" button at the top of the Visual Basic toolbar?


----------



## computerman29642

laguy83 said:


> Man I don't know.... Just so I make sure I'm doing it correctly, you run the code by clicking the "play" button at the top of the Visual Basic toolbar?


That would be correct. You do have the code in it's own seperate module...correct?

I guess it could be possible that Adobe Acrobat 8 Pro does not like the code for whatever reason.

If anyone else has Adobe Acrobat 8 Professional, could you please test the code?


----------



## laguy83

Yeah I do have the code in its own module. Rats...my computer is not cooperating today it seems.


----------



## Zack Barresse

@CM: You can use the *Target.Value* as the cell's value being double clicked.


----------



## computerman29642

What version of Excel are you running?


----------



## Zack Barresse

What version of Internet Explorer and Adobe are you using?

Edit: And Excel


----------



## laguy83

Excel 2003. 
IE 6. 
Acrobat 8 Professional.
I also have firefox installed, which is what I am currently using.


----------



## computerman29642

Zack Barresse said:


> @CM: You can use the *Target.Value* as the cell's value being double clicked.


How would that be used in order to open different pages by double-clicking different cells?


----------



## Zack Barresse

The Target object is the cell being double clicked. So whatever the value is in that cell, you have it already in your sub by using Target.Value call. So double click a cell with the code *Msgbox Target.Value*, you should see what I mean.


----------



## computerman29642

Zack Barresse said:


> The Target object is the cell being double clicked. So whatever the value is in that cell, you have it already in your sub by using Target.Value call. So double click a cell with the code *Msgbox Target.Value*, you should see what I mean.


So, you are saying do something like this:



Code:


Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    
    Dim IE As Object, strFile As String, iPageNum As Long
    If Target.Value = "Test" Then
    MsgBox Target.Value
        strFile = "C:\Test.pdf"
        iPageNum = 2
        Set IE = CreateObject("InternetExplorer.Application")
        IE.Navigate strFile & "#Page=" & iPageNum
        IE.Visible = True
    End If
End Sub


----------



## Zack Barresse

I'm saying if the file path is in the cell you could use...



Code:


Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    
    Dim IE As Object, strFile As String, iPageNum As Long
    iPageNum = 2
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Navigate Target.Value & "#Page=" & iPageNum
    IE.Visible = True
End Sub

If some other value was in the cell, there'd need to be some way to 'decode' what was represented in the cell vs. what file path it was referring to. I.e. a lookup table (either in hte worksheet or via code).


----------



## computerman29642

I see what you are saying. I had the IF statement so the code would not run if the cell was empty. I guess I was over analyzing again and making things harder than they have to be.


----------



## computerman29642

laguy83 said:


> Excel 2003.
> IE 6.
> Acrobat 8 Professional.
> I also have firefox installed, which is what I am currently using.


I wonder if it is the Firefox that is keeping the code from working properly. Would it matter if Firefox is the default Internet browser?

Could you make IE your default browser, and try the code again?


----------



## laguy83

yeah IE is my default browser...no dice though.


----------



## computerman29642

laguy83 said:


> yeah IE is my default browser...no dice though.


I guess it was worth a shot.


----------



## computerman29642

Why don't you try walking through the code?

1. Click on the Grey area (bar) to the left of the line "strFile = "\\sli\pub\AUI\Brian\SKMBT_C35308122216410.pdf"
2. Click The "Run Macro" button (play button)
3. The line of code should become highlighted yellow. Now, press F8 to walk through the code. The code executes after the line is no longer highlighted.


----------



## laguy83

Computerman...I did exactly what you said. As I walk through the code, the line "strFile = ....pdf" remains highlighted red. Each time I press F8 the next line down turns yellow. When I arrive at the line "IE.Visible = True", Acrobat pops up with my pdf file, again at the first page.

Any thoughts?


----------



## computerman29642

Not really. 

The two things that I know for sure that is different is your version of Adobe Acrobat and I do not have Firefox.

Can you think of anything else?


----------



## laguy83

Operating system? (I have XP professional).

What version of Adobe are you using? Perhaps I could try installing the old version...


----------



## computerman29642

I am running the same operating system.

I believe there is a user here that has Adobe Acrobat 8 Pro. Let me do some testing.


----------



## computerman29642

I just tested the code on a computer with Adobe Acrobat 8 Pro. The code worked just fine.


----------



## slurpee55

Using Acrobat Pro 8, Firefox is my default but it opens up just fine in IE on the 2nd page. Do you have the code set in a module or in "ThisWorkbook"? It runs fine from a module...don't know if that should affect it, though.


----------



## laguy83

Thanks for testing.


----------



## laguy83

I have the code set in a module...is there some way I need to save the module prior to running the code? Right now all I am doing is pasting the code into a module and then hitting the "play" button.


----------



## computerman29642

laguy83 said:


> Thanks for testing.


No problem.


----------



## computerman29642

laguy83 said:


> I have the code set in a module...is there some way I need to save the module prior to running the code? Right now all I am doing is pasting the code into a module and then hitting the "play" button.


That should be fine. I mean you will need to change the PDF path location (if that is not already correct).


----------



## computerman29642

Would it be possible for you to attach your Excel workbook (replace sensitive data with dummy data)? Also, could you run the code, take a screenshot, and attach the screenshot?


----------



## laguy83

There is the excel workbook. Screenshot to follow.


----------



## laguy83

UPDATE: Here is the screenshot. Thanks!


----------



## computerman29642

laguy83 said:


> There is the excel workbook. Screenshot to follow.


Everything looks good here.


----------



## computerman29642

laguy83 said:


> UPDATE: Here is the screenshot. Thanks!


I apologize. I wanted you to take a screenshot of the PDF that opened after you ran the code.


----------



## laguy83

No worries. I attached the updated screenshot.


----------



## computerman29642

I just realized that when I run my code it does open in IE and not Adobe. So, I guess the question is why does yours open in Adobe and not IE.


----------



## laguy83

Your guess is as good as mine...maybe someone reading this will have an idea?

Thanks again for all your help...seriously i really appreciate that you went to this much effort for a total stranger. u rock!


----------



## computerman29642

Do this...

1. Right-click one of your PDF files.
2. Highlight Open With
3. Click *Choose Program...*

Is the line *Always use the selected program to open this kind of file* checked and Adobe Acrobat is the program highlighted?

If so, uncheck the checkbox and click OK.

Follow the steps above to be sure that it did not re-check itself. If it is still unchecked, try to run the code again.


----------



## computerman29642

laguy83 said:


> Your guess is as good as mine...maybe someone reading this will have an idea?
> 
> Thanks again for all your help...seriously i really appreciate that you went to this much effort for a total stranger. u rock!


No problem.  Users on here have done the same for me. :up:


----------



## laguy83

So I did what you said. Acrobat is not the default program. The box is unchecked. However, I went ahead and tried opening it with IE. The result was that the file opened in Acrobat.

For some reason these files refuse to be opened by IE. Perhaps it's built in to the version of Acrobat that I currently have. Beyond that I am out of ideas.


----------



## computerman29642

It is not the version of Acrobat you are using. Recall, I have tested the code on a computer with the same version.


----------



## laguy83

True...perhaps then it's the security settings on my system? I do not have full administrative privileges. Hrmm....tomorrow I'll talk to our IT administrator about it.


----------



## computerman29642

Sounds good. Please be sure to keep us updated. I would be curious to find out the solution.


----------



## Zack Barresse

I was thinking something along those lines. Two things come to mind. We're bound to an IE object, and the code we are passing is acting on such. Thus opening in Adobe will not have the same effect. I looked at the Object Model for Adobe (via Excel) and it sucks, there's literally one object in it. And virtually no documentation (at least I couldn't find any). So try changing to default application to open them, otherwise it will keep opening a PDF in Adobe, so just the file association. You may need administrator privelages in control panel for that.

The other thing I was thinking is that it was on a network path. To test this you can take your code to somebody else's machine on the same network who has IE as their default PDF viewer, or at least the Adobe add-on in IE to open it, and check it there. Maybe you just need the IE add-on. Not sure. I wouldn't think it would make a difference if the file was on a server or not, but I've seen stranger things happen...


----------



## slurpee55

To follow up on what Zack said, perhaps you could try copying the pdf file to your hard drive and setting up a link in Excel to that. I managed to get one to open, but it was on my own PC, not on the network.
If you can do that also, then we know it has to do with the network - probably a security issue (although allowing you to open it to one page and not to another doesn't seem to be much of a safety concern).
It could also be the path name is getting too long - the extra characters that state page#3 or whatever may not be getting understood.


----------



## computerman29642

I noticed that the file path being used looked as though it was on a network. I tested the code here using a PDF file that was on the network, and it worked just fine.

However, like Slurp said it could be a security issue.


----------

