Excel Hyperlink function to open a specific page - excel

Could anyone from this group can offer some advice on the problem I'm trying to solve? I was attempting to open a PDF document to a specific page using the Excel Hyperlink function. I tried to solve this mystery but to no avail. Hope someone can provide me their thoughts and ideas.
I tried Google and Youtube but only few were related to my issue and the solution discussed was not working on my situation... or maybe i missed something. This will be a great help for me once the issue is solved. Thank you in advance.

Using a macro enabled workbook you can hi-jack the hyperlink open event but it isn't as dynamic as we would hope it to be. Unfortunately, we cannot cancel any followed hyperlink using Worksheet_FollowHyperlink so we need to find a way around following a direct hyperlink.
We can create a hyperlink which goes nowhere by referencing itself then set the text to display as "Follow Link". Because the cell reference is then in the hyperlink, we can use an offset from that to get the desired address and sub address from the column before it; which then allows us to open the PDF at the desired place.
Unfortunately, the hyperlink if copied down will still reference the original cell, so each link would need to be edited separately. To date I haven't found a way to use the HYPERLINK function successfully.
The hyperlink format in the cell to the left would need to be the full path appended with "#page=" and the relevant page; e.g. C:\links\blah.pdf#page=6
In the worksheet module
Const AcrobatReader = "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe"
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
If Target.TextToDisplay = "Follow Link" Then
Dim Ref As String: Ref = Range(Target.SubAddress).Offset(0, -1).Text
Dim URL() As String: URL = Split(Ref, "#page=")
If UBound(URL) > 0 Then Call OpenPDFtoPage(URL(0), CLng(URL(1)))
End If
End Sub
Function OpenPDFtoPage(FilepathPDF As String, page As Long)
Dim Path As String: Path = AcrobatReader & " /A ""page=" & page & """ " & FilepathPDF
Shell Path, vbNormalFocus
End Function
The AcroRd32.exe path may need updating for specific installations
Maybe having just one "Follow Link" hyperlink where the URL in the cell next to it is more dynamic may allow this to be a bit more useable.

Related

How to copy text contained in a cell to clipboard

I'm very new to this, so please bear with me.
Background info: I'm trying to create my own quickparts / textblock tool so to speak.
In order to achieve this I have created multiple "buttons" that i want to assign to macros.
The desired result is, that you only have to click the button (named "holidays" for instance) and the corresponding explanatory text is copied into your clipboard.
The problem is that the corresponding text passages are often long and I don't want them included in the code as is the case at the moment where I use this:
Sub Holidays()
Dim obj As New DataObject
Dim txt As String
txt = "Holidays must be requested minimum 2 weeks prior"
obj.SetText txt
obj.PutInClipboard
Beep
End Sub
Instead of having the explanatory text in the code I would like to have it stored in a cell in a different sheet and it to be accessed and copied into the clipboard.
What I have tried so far is putting the text in a cell and named it [Holidays].
I can also set it as a message box, but can't seem to figure out how to get it into the clipboard.
Can anyone help me out with this?
Thanks a lot!

Searching for DATA (VBA+Web)

I'm trying to create a macro that fetch for specific DATA in URL from Excel sheet and when the DATA is found , it copy the values and past it into the Excel File andmove on to the next DATA
I'm a little bit new with SCRAPPING WEB via EXCEL VBA so Anyone can light me and helps me to carry on ?
I made a lot of searches here in stack over flow but I didn't understand too much
I will explain below with Image what I want to Do and I will show My code which is nothing :(
First URL to use to access to MP :http://XXXX-XXXXX.eu.airbus.XXXX:XXXXX/XXXX/consultation/preSearchMP.do?clearBackList=true&CMH_NO_STORING_fromMenu=true
then Take a value from EXCEL FILE and put it in that Path to make search and tape enter
The last Picture IT will loop for MOD and Copy Value and Paste it into Excel File
Can Anyone guide me please with something
this is my first Code
Dim str As String: str = ThisWorkbook.Sheets("Feuil1").Range("A1").Value
Dim myURL as String
' The & symbol concatenates strings. The _ symbol is for line continuation.
myURL =
"http://Confidential.eu.airbus.Confidential:Confidential/Confidential/" _
& "consultation/preViewMP.do?" & str
If you have your URL then the next stage is to send it. To send it you need something to act as a connector, like Internet Explorer, or a background connector without the browser like XMLHttpServer objects - THIS is a good guide to hopefully get you started.

Using # in a path name for a hyperlink in excel vba

I am creating a vba For loop to cycle through cells in a range and creating a hyperlink to file folders based on the text within the cell. See below:
Set rng = Worksheets("Sheet1").Range("A1:A100")
For Each cell In rng
address1 = "C:\Users\Desktop\Tests\Comm Review\Item #" &
cell.Text
If Not IsEmpty(cell) Then
Worksheets("Sheet1").Hyperlinks.Add Anchor:=cell, Address:=address1, TextToDisplay:=cell.Text
End If
Next cell
The cell value will be something like 1001.T0502 and the actual folder name that I am linking to will be Item #1001.T0502. So in my address1 variable i create the path to the folder.
However, when I do this it creates the path with everything but #1001.T0502 and ends up stopping at "\Item". If I were to drop the number sign(#) though it includes the number and ends up being Item 1001.T0502. For some reason the number sign stops it from making the correct path. What am I missing here? There are already 200 folders with the number sign in the folder name so going back now and taking it out would be too much work.
Any help would be appreciated. Thanks!
You cannot use a pound character in a file name for a hyperlink in an Office program. See official Microsoft documentation here:
https://support.microsoft.com/en-us/help/202261/you-cannot-use-a-pound-character-in-a-file-name-for-a-hyperlink-in-an
Seems totally wacko if you ask me, but alas, I think you're trying to solve an unsolvable problem.
But, fear not, I did think of a potential work around. Instead of making the cells actual hyperlinks, you could just recolor the cell to blue with an underline and then use this little trick to capture when the cell is selected and open Windows Explorer to the corresponding file path.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("A1:A100")) Is Nothing Then
Shell "explorer ""C:\Users\Desktop\Tests\Comm Review\Item #" & Target.Value & """", vbNormalFocus
End If
End If
End Sub
The only downside I can see here is that selecting the cell with the arrow keys also opens the corresponding folder. There may be a work around to that, but I don't have time at the moment to research it.
I hope this helps!

How to create hyperlinks with VBA to the same open workbook

The setup
I have an Excel workbook stored on my hard drive.
The structure is such that on the first sheet I have a list of the names of the other sheets in the same workbook (...which can be created or deleted).
All the names on the list, on the first sheet, are supposed to be hyperlinks to the corresponding sheet in the workbook. So, by clicking the name on the first sheet you jump to the corresponding sheet.
When a new sheet is created a macro creates also the new name on the list on the first sheet and makes a hypelink of it. This works.
...BUT...
The links point to the stored version of the file, not to the open workbook! Clicking the links opens the stored file and not the one which is under work.
QUESTION: How to create a hyperlink that always points to the same open workbook and not to the stored copy of it?
Try this:
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
"Sheet3!", TextToDisplay:="Link to sheet #3"
Address is the URL and SubAddress is a localtion of the page (or a sheet or a range in excel workbooks).
You may try creating the hyperlink as a formula (via VBA, as you need).
I am using the parameters you posted, this may need a little adjustment.
Dim rngsrc as Range, rngtrgs as String
Set rngsrc = Worksheets("Summary").Cells(Cell.Row, 5)
Set rngtrg = "'" & sSheetName & "'!B5"
rngsrc.FormulaR1C1 = "=HYPERLINK(" & rngtrgs & "," & sSheetName & ")"
See
Official documentation
Example 1
Example 2
This (hopefully) answers your question. As a separate note, it still remains to be clarified why you see the behavior you see.
It seems that the problem comes from the following fact: My file is in a SharePoint folder. If I open it just for reading, the hyperlinks work fine. If I open the file for editing, a copy of the SharePoint file is placed on my hard disk, on a specified location. So, the path to the file is not the same as it would be if I open it read-only. Should I use hyperlink.follow to solve this?
So, this all comes down to the question: In VBA/Excel, can I create a hyperlink which always points to a location in the same opened file so that the hyperlink ignores the storage path of the corresponding file? Using empty string (or BLANK) doesn't help as the address parameter in hypelinks.add as Excel seems to automatically fill in the whole storage path.
I upgraded to Office 2013 and "PADAM": The problem vanished! It seems that there was a bug in Excel/VBA in this in the 2007 version.

Is it possible to automatically input VBA code when a new sheet is generated?

Is it possible to have a new Excel sheet created and then VB code automatically put in that sheet?
Here is what I have done so far. A sheet called Template has the input for all of the information that users need to input. I have various checks to make sure that all fields are filled out and are filled out correctly before anything else will execute. When they click on a certain cell to execute the script it will open a Word document and import all required information in it. Then a new sheet in Excel is created. A name is given to the new sheet, based on what was selected in the ComboBox (cboSites) from the Template sheet. I also have a check in place to make sure there already isn't a sheet with the same name. I have all of this working without any issues.
From here what I would like to do and can't think of how to do it, is when the new sheet is created I want VBA code automatically dumped in this new sheet. I know the code that I want to use, but I just have no idea how to get it so it will automatically put that code with that sheet.
Is this possible to do or can only a new sheet be created and formatted, without being able to import any code into it?
Here is a sample code which will insert
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
MsgBox "Hello World"
End Sub
in a new sheet in a new workbook.
Sub Sample()
Dim wb As Workbook, ws As Worksheet
Dim VBP As Object, VBC As Object, CM As Object
Dim strProcName As String
Set wb = Workbooks.Add
Set ws = wb.Sheets(1)
Set VBP = wb.VBProject
Set VBC = VBP.VBComponents(ws.Name)
Set CM = VBC.CodeModule
strProcName = "Worksheet_SelectionChange"
With wb.VBProject.VBComponents( _
wb.Worksheets(ws.Name).CodeName).CodeModule
.InsertLines Line:=.CreateEventProc("SelectionChange", "Worksheet") + 1, _
String:=vbCrLf & _
" Msgbox ""Hello World"""
End With
End Sub
Please amend it to suit your needs.
You need to ensure that Trust access to Visual Basic Project is selected. To select it, follow the steps mentioned in THIS LINK
I am not aware of an easy way to put code in an Excel file.
Someone might think about changing the XML structure directly (xlsx files are basically a zipped directory of xml and code files).
But did you consider using XLAM (Excel addin) files, that can contain code and be imported for all users, who ever need to use it. And would open up with Excel, when the users start it. Depending on your setup, this could help you?
Look into the VBProject property of the workbook you are generating. You should be able to manipulate it, adding new VBComponents items containing the code you want.
http://msdn.microsoft.com/en-us/library/office/ff194737.aspx
This will require the appropriate security settings to do.
Here's a thread on another site related to this topic:
http://www.mrexcel.com/forum/excel-questions/663848-access-vbulletin-project-not-trusted.html
I haven't tried it myself, so I can't give much more detail.

Resources