473,498 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Access to Excel Hyperlinks, New question checked old posts

6 New Member
Sorry guys, this is killing me.

I have been fighting this issue for several weeks, to no avail. I am exporting the results of a query to an excel sheet, with one of the columns being from a field of hyperlinks that link to files on the shared drive.

I CANNOT get the hyperlinks to be assigned. I can change the field name, but not the hyperlink address. Can't figure it out.

1) I have a header row
2) Column 16 is where the hyperlinks are

Here it is:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Hiring_submit_cmd_Click()
  2. ' Excel Handling
  3. Dim oExcel As Excel.Application
  4. Dim oWB As Excel.Workbook
  5. Dim oWS As Excel.Worksheet
  6. Dim SaveString As String
  7. Dim sa
  8. Dim HyperCount As Long
  9. Dim RanCell As Excel.Range
  10. Dim StrTxt As String
  11. Dim HyperlinkTxt As String
  12.  
  13. For HyperCount = 2 To oWB.Sheets(1).UsedRange.Rows.Count
  14.      Set RanCell = oWB.ActiveSheet.Cells(HyperCount, 16)
  15.  
  16.      StrTxt = StrConv(RanCell, vbProperCase)
  17.      If Len(StrTxt) > 1 Then _
  18.           call oWB.ActiveSheet.Hyperlinks.Add(anchor:=RanCell, _
  19.                Address:=StrTxt, TextToDisplay:=StrTxt)
  20.      Next HyperCount
Thanks
Aug 25 '08 #1
12 5568
NeoPa
32,557 Recognized Expert Moderator MVP
I know you've already checked and found How to export a hyperlink field from Access to Excel - VBA (I had to remove the post you put in there before you realised you needed to post in your own thread).

This can help you I'm sure, but first you need to think about the basics of your code. It appears that there is nothing in your code which establishes any link into Excel. Check out Application Automation first.

Your code seems to be missing so much it's hard to know where you're at. Let's get the basics sorted out first then we can move on from there to fixing the hyperlink situation.
Aug 25 '08 #2
Frustratee
6 New Member
Roger, thanks NeoPa.

Let me post all of it. I was thinking that I would just cut to the chase of it, but here we go:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Hiring_submit_cmd_Click()
  2. Dim VarItem As Variant
  3. ' Query Handling
  4. Dim dbs As Database
  5. Dim qryname As String
  6. Dim QryDef As QueryDef
  7. ' Excel Handling
  8. Dim oExcel As Excel.Application
  9. Dim oWB As Excel.Workbook
  10. Dim oWS As Excel.Worksheet
  11. Dim SaveString As String
  12. Dim sa
  13. Dim HyperCount As Long
  14. Dim RanCell As Excel.Range
  15. Dim StrTxt As String
  16. Dim HyperlinkTxt As String
  17.  
  18.  
  19. '
  20. ' Development of SQLStr SQL statement, works fine...
  21. '
  22.  
  23. ' Perform Query--------------------
  24. Set dbs = CurrentDb
  25. qryname = "HiringQuery"
  26. 'Delete old query
  27. For Each QryDef In dbs.QueryDefs
  28.     If (QryDef.Name = qryname) Then dbs.QueryDefs.Delete qryname
  29. Next QryDef
  30. Set QryDef = dbs.CreateQueryDef(qryname, SQLStr)
  31. ' Execute query
  32. DoCmd.SetWarnings False
  33. DoCmd.OpenQuery "HiringQuery"
  34. DoCmd.Close acQuery, qryname
  35. DoCmd.SetWarnings True
  36.  
  37. ' Output to Excel-----------------
  38. SaveString = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\" & _
  39.     "Candidate Hiring Query " & Format(Now(), "ddmmmyyyy") & " " & Format(Now(), "hhnn") & ".xls"
  40. On Error GoTo Errorhandler
  41.  
  42. DoCmd.Echo False
  43. DoCmd.SelectObject acTable, , True
  44. DoCmd.OutputTo acOutputQuery, "HiringQuery", acFormatXLS, SaveString
  45. DoCmd.SelectObject acTable, , True
  46. DoCmd.RunCommand acCmdWindowHide
  47. DoCmd.Echo True
  48.  
  49. Set oExcel = New Excel.Application
  50. Set oWB = oExcel.Workbooks.Open(SaveString)
  51. 'Format Headers
  52. oWB.ActiveSheet.Range("A1").Value = "Last Name"
  53. oWB.ActiveSheet.Range("B1").Value = "First Name"
  54. oWB.ActiveSheet.Range("C1").Value = "M.I."
  55. oWB.ActiveSheet.Range("G1").Value = "Possible Reqs"
  56. oWB.ActiveSheet.Range("H1").Value = "Significant Comments"
  57. oWB.ActiveSheet.Range("I1").Value = "Yrs Experience"
  58. oWB.ActiveSheet.Range("J1").Value = "Service"
  59. oWB.ActiveSheet.Range("L1").Value = "Degree Details"
  60. oWB.ActiveSheet.Range("M1").Value = "Number of Schools"
  61. oWB.ActiveSheet.Range("N1").Value = "Deployments"
  62. oWB.ActiveSheet.Range("O1").Value = "Received"
  63. oWB.ActiveSheet.Range("P1").Value = "Resume Path"
  64. **************************************************************************
  65. ' Add hyperlinks
  66. For HyperCount = 2 To oWB.Sheets(1).UsedRange.Rows.Count
  67.     Set RanCell = oWB.ActiveSheet.Cells(HyperCount, 16)
  68.  
  69.     StrTxt = StrConv(RanCell, vbProperCase)
  70.     If Len(StrTxt) > 1 Then _
  71.         Call oWB.ActiveSheet.Hyperlinks.Add(anchor:=RanCell, _
  72.                                 Address:=RanCell, TextToDisplay:=RanCell)
  73.  
  74. Next HyperCount
  75. *************************************************************************
  76. oWB.ActiveSheet.Range("Q1").Value = "LOI Path"
  77. oWB.ActiveSheet.Rows("1:1").EntireRow.AutoFit
  78. oWB.ActiveSheet.Columns("A:n").EntireColumn.AutoFit
  79.  
  80. oWB.Save
  81. Set oWS = Nothing
  82. If Not oWB Is Nothing Then oWB.Close
  83. Set oWB = Nothing
  84. oExcel.Quit
  85. Set oExcel = Nothing
  86. sa = Shell("C:\Program Files\Microsoft Office\OFFICE11\excel.exe " & _
  87.     Chr$(34) & SaveString & Chr$(34), vbNormalFocus)
  88. Exit Sub
  89.  
  90. Errorhandler:
  91.  
  92.     If Err.Number = 2302 Then
  93.         MsgBox ("Close Excel file and try again.")
  94. '    Else
  95. '        MsgBox ("Undefined BPMP error.")
  96.     End If
  97. End Sub
Aug 26 '08 #3
NeoPa
32,557 Recognized Expert Moderator MVP
Generally you can do (and it's a good idea to where possible), but you must include the relevant data or your code will not be recognisable.

Had you simply included a comment saying briefly what the code had done and what the object variables were pointing to (much as you did for the code formulating the SQL string) that could also have worked.

Anyway, no harm, no foul. I'll see if I can find some time to go through this in some detail soon. Just coming to end of Lunch-hour at the moment so will have to squeeze in as and when.
Aug 26 '08 #4
NeoPa
32,557 Recognized Expert Moderator MVP
You mentioned earlier that the links you're using are local files rather than web references. One major difference (that I think is actually your problem) is that in my code I knew all entries that needed converting were already fully qualified HTTP references (http://...). If you want this to work correctly for local file references you need to ensure that they follow the web addressing convention for local files. IE. "FILE://...".

In short, I expect that if you're using addresses of the form "C:\Temp\MySpreadsheet.xls" you will have problems. However, if you were to use (or doctor in the code to use) addresses of the form "FILE://C:\Temp\MySpreadsheet.xls" you would have no such problems.

Try this out and let us know how you get on.
Aug 26 '08 #5
Frustratee
6 New Member

In short, I expect that if you're using addresses of the form "C:\Temp\MySpreadsheet.xls" you will have problems. However, if you were to use (or doctor in the code to use) addresses of the form "FILE://C:\Temp\MySpreadsheet.xls" you would have no such problems.

Try this out and let us know how you get on.
You were correct that this is what I was doing. However, after inserting the following (where AddressTxt is defined as a String), the field shows up looking like a hyperlink (as it did before...blue and underlined) but with no address linked:
Expand|Select|Wrap|Line Numbers
  1. For HyperCount = 2 To oWB.Sheets(1).UsedRange.Rows.Count
  2.     Set RanCell = oWB.ActiveSheet.Cells(HyperCount, 16)
  3.     AddressTxt = "FILE://" & StrConv(RanCell, vbProperCase)
  4.     StrTxt = StrConv(RanCell, vbProperCase)
  5.     If Len(StrTxt) > 1 Then _
  6.         oWB.ActiveSheet.Hyperlinks.Add anchor:=RanCell, _
  7.                                 Address:=AddressTxt, TextToDisplay:=StrTxt
  8.  
  9. Next HyperCount
Setting Address and TextToDisplay directly to "FILE://" & Rancell and Rancell, respectively, yield a runtime error '5' invalid procedure call or argument. Same error occurs when I CALL the hyperlinks.add and use parentheses.

:( Was really hoping your suggestion would work.
Aug 26 '08 #6
NeoPa
32,557 Recognized Expert Moderator MVP
Try something like this :
Expand|Select|Wrap|Line Numbers
  1. With oWB.ActiveSheet
  2.   For HyperCount = 2 To .UsedRange.Rows.Count
  3.     Set RanCell = .Cells(HyperCount, 16)
  4.     StrTxt = StrConv(RanCell, vbProperCase)
  5.     If Len(StrTxt) > 1 Then
  6.       AddressTxt = "FILE://" & StrText
  7.       Call .Hyperlinks.Add(Anchor:=RanCell, _
  8.                            Address:=AddressTxt, _
  9.                            TextToDisplay:=StrTxt)
  10.     End If
  11.   Next HyperCount
  12. End With
I've tried the effect of this code on some basic dummy data (in Excel 2003) and it worked fine for me (it linked to the spreadsheet I had in my original data).

In truth, I understand why you an error 5 (using a Range reference for TextToDisplay which requires a string variable), but not why your other attempts failed. Try with this and see what you get. Does it link when clicked on?
Aug 26 '08 #7
Frustratee
6 New Member
... negative.

It couldn't be a reference library thing, could it? Here are the ones I have:

VBA
Access 11.0
OLE Automation
DAO 3.6
ActiveX Data Objects 2.1
Excel 11.0
Office 11.0
Outlook 11.0

That's the only other thing I can think of. All data gets from the correct Access records, is formatted properly, even the hyperlink fields are blue and underlined, but just no data in the hyperlink address field.

Another thing I have noticed, an anomoly. You know how each line is autocorrected for punctuation after that line has been left? Well, no matter what way I type in 'anchor,' it stays that way: AnChOr.

Regardless, thanks for the time in this.
Aug 26 '08 #8
NeoPa
32,557 Recognized Expert Moderator MVP
Some interesting points. I can only say that I don't know of any link to References that might explain this.

What I will do is first explain the AnChOr:= thing, then suggest a test for you to carry out.

The VBA editor will generally correct all items that it has knowledge of. This includes reserved words as well as any name which has already been defined anywhere in your code. It will handle named parameters of procedures if they are defined in your code, but unless there is a specific declaration of the procedure and all its parameters (there is none for .Hyperlinks.Add()), then it will not have access to the correct capitalisation of the word. In short, for inbuilt procedures don't expect that to work (although it will for ones you've written yourself).

Now, what I want you to try out is the following :
Open a new workbook (Ctrl-N) in Excel and enter the address of one of your Excel files into cell A1 in normal Windows Explorer format (C:\Home\YourName\Excel\Test.xls for instance - but the file should exist for the test to make sense).

When you navigate away from the cell there should be no hyperlink set up.

Now, hit Alt-F11 (open and switch to the VBA Editor) and Ctrl-G (Switch to the Immediate Pane).

Enter the following and hit enter (use Copy / Paste from here) :
Expand|Select|Wrap|Line Numbers
  1. Call ActiveSheet.Hyperlinks.Add(Anchor:=Range("A1"), Address:="FILE://" & Range("A1").Text, TextToDisplay:=Range("A1").Text)
Do you find this link works when you click on it (It does for me)?
Aug 26 '08 #9
Frustratee
6 New Member
Yes, doing that in excel does work...
Aug 27 '08 #10
NeoPa
32,557 Recognized Expert Moderator MVP
That tells me that the code should work assuming the data I expect.

Try replacing the last bit with this (I've added some lines to show some extra information) and see what it shows in the Immediate Pane (Ctrl-G). I'd prefer the results for a single cell only if possible, but if not then dump the lot in a post and we'll see what we can determine.
Expand|Select|Wrap|Line Numbers
  1. With oWB.ActiveSheet
  2.   For HyperCount = 2 To .UsedRange.Rows.Count
  3.     Set RanCell = .Cells(HyperCount, 16)
  4.     StrTxt = StrConv(RanCell, vbProperCase)
  5.     If Len(StrTxt) > 1 Then
  6.       AddressTxt = "FILE://" & StrText
  7. Debug.Print RanCell.Address, RanCell.Text
  8. Debug.Print AddressTxt
  9. Debug.Print StrTxt
  10.       Call .Hyperlinks.Add(Anchor:=RanCell, _
  11.                            Address:=AddressTxt, _
  12.                            TextToDisplay:=StrTxt)
  13.     End If
  14.   Next HyperCount
  15. End With
Aug 28 '08 #11
Frustratee
6 New Member
Using your code during the Access export:

Expand|Select|Wrap|Line Numbers
  1.     with owb.activesheet
  2. For HyperCount = 2 To .UsedRange.Rows.Count
  3.         Set RanCell = .Cells(HyperCount, 16)
  4.         StrTxt = StrConv(RanCell, vbProperCase)
  5.         If Len(StrTxt) > 1 Then
  6.             AddressTxt = "FILE://" & StrTxt
  7.             Debug.Print RanCell.Address, RanCell.Text
  8.             Debug.Print AddressTxt
  9.             Debug.Print StrTxt
  10.             .Hyperlinks.Add AnChoR:=RanCell, _
  11.                                 Address:=AddressTxt, _
  12.                                 TextToDisplay:=StrTxt
  13.         End If
  14.     Next HyperCount
  15. end with
I got the following:

Expand|Select|Wrap|Line Numbers
  1. $P$2          G:\path\file.pdf
  2. FILE://G:\path\file.pdf
  3. G:\path\file.pdf
The hyperlinks did not link.

I used the following code in the exported excel sheet:

Expand|Select|Wrap|Line Numbers
  1. Dim hypercount As Long
  2. Dim rancell As Excel.Range
  3. Dim StrTxt As String
  4. Dim AddressTxt As String
  5.  
  6. With ActiveSheet
  7.  
  8.   For hypercount = 2 To .UsedRange.Rows.Count
  9.     Set rancell = .Cells(hypercount, 16)
  10.     StrTxt = StrConv(rancell, vbProperCase)
  11.     If Len(StrTxt) > 1 Then
  12.       AddressTxt = "FILE://" & StrText
  13. Debug.Print rancell.Address, rancell.Text
  14. Debug.Print AddressTxt
  15. Debug.Print StrTxt
  16.       Call .Hyperlinks.Add(Anchor:=rancell, _
  17.                            Address:=AddressTxt, _
  18.                            TextToDisplay:=StrTxt)
  19.     End If
  20.   Next hypercount
  21. End With
And, interestingly, there was no path after "FILE://":

Expand|Select|Wrap|Line Numbers
  1. $P$2          G:\path\file.pdf
  2. FILE://
  3. G:\path\file.pdf
However, the hyperlink DID work. In the working links, after right-clicking on any one of them and selecting 'Edit Hyperlink', even though the link works, the address line is blank. What the hell...
Aug 28 '08 #12
NeoPa
32,557 Recognized Expert Moderator MVP
That's because in the first one you typed in my code and in the second you copy / pasted it in.

There is a bug in my code at line #6 where it uses the variable name StrText instead of StrTxt. It's always VERY important to use Copy / Paste. In this case it was important to copy the error in order that the whole makes sense. Strange I know.

Possibly try it again with all the same code (in this case clearly fix this bug first).
Aug 28 '08 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

3
7286
by: zino | last post by:
I have an excel sheet that have one of its cells as a hyperlink I need to read this cell through Vb.net and get the value of the hyperlink (the whole path ex: \\c:\document\..... ..... ... .. ,...
7
1669
by: Julia Baresch | last post by:
Hi everyone, My company recently upgraded from Office 97 to Office XP. As those who've read my other posts know I have a database with 3 nested subforms Main form-->First Subform-->Second...
0
466
by: Tim | last post by:
Access 97: I have a table with a hyperlink field that I display on a form. I can click on the form field and the hyperlink activates correctly. However, if I try to activate the hyperlink using VB...
7
1908
by: pw | last post by:
Hi, We need a Gantt chart type calendar for Access 97 (and soon, Access 2003). We need it to list the days of the month (for a date range) going horizontally across the top, the names of the...
6
3647
by: Terry Bell | last post by:
We've had a very large A97 app running fine for the last seven years. I've just converted to SQL Server backend, which is being tested, but meanwhile the JET based version, running under terminal...
1
2357
by: Robin | last post by:
Hello! I'm having trouble with links and hyperlinks in MS Access 2003 - any help would be great! Question 1! The "insert hyperlink" icon opens a browser window, allows the user to browse...
4
7111
by: Janelle.Dunlap | last post by:
When I import an Excel file containing hyperlinks into Access, the hyperlinks turn into text and no longer hold their links. I am using the TransferSpreadsheet function in Access to import my...
7
7043
by: ddecoste | last post by:
I have a need to add a visual representation to some data in Access. I need to draw a matix of squares inside another square. I have all the data that I need in a record in Access. The data...
1
4029
by: mei2523 | last post by:
I've got a query that contains a field called "URL", that field is a hyperlink field. I've got these super long hyperlinks in this field for different records, and for some reason, when I export...
0
7004
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7167
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7208
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4593
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3095
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1423
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
292
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.