473,385 Members | 1,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to change relative hyperlink to absolute hyperlink in VBA

20
I want to send several attached files from MS Access using CDO.Message.
When I use "objMessage.AddAttachment" I need to indicate full path to the file, but all hyperlinks to files stored in separate column of my query (recordset) but as relative links only. How to change it to absolute hyperlinks?
Full code as below:

Expand|Select|Wrap|Line Numbers
  1. Private Sub btn4Pay_Click()
  2. On Error GoTo btn4Pay_Click_Error
  3.  
  4. Dim rst As ADODB.Recordset
  5. Dim strSQL As String
  6. Set rst = New ADODB.Recordset
  7.  
  8. strSQL = "[qryDocsToSend]" 'source of recordset
  9. rst.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
  10. sFilePath = Trim(rst(8))
  11.  
  12. Set objMessage = CreateObject("CDO.Message")
  13. objMessage.Subject = "DOCUMENTS" 
  14. objMessage.From = """Sender"" <sender@gmail.com>"   
  15. objMessage.To = """Recepient"" <recepient@gmail.com>"
  16.  
  17. Do While Not rst.EOF 
  18. objMessage.TextBody = Trim(rs(0)) & "  " & Trim(rs(1))
  19. objMessage.AddAttachment "sFilePath"
  20.  
  21. rst.MoveNext
  22. Loop
  23. rst.Close
  24. Set rst = Nothing
  25.  
  26. objMessage.BodyPart.Charset = "windows-1251"
  27.  
  28. '===SMTP server configuration extracted ===
  29.  
  30. objMessage.Send
Nov 21 '12 #1

✓ answered by NeoPa

Costa,
When you check the Help section for Split() you will see that it returns a value that is an array. The (1) after the call is how VBA says it wants to use element #1 from an array. The default array indexing starts from zero so element #1 is the second element - IE. The data between the first and second separator charactes ("#" in this case) in the text. Try the following from the Immediate Pane of the VBE window (Use Ctrl-G to get there) :
Expand|Select|Wrap|Line Numbers
  1. ?Split("A@B@C@D@E@F","@")(2)
You will see the value "C" printed.

CostasM:
and what do you mean "I have already extracted that string into a variable called strHyperlink" ?
I didn't ever say that, or anything that had that meaning. What I said was :
"The following example code assumes you have already extracted that string into a variable called strHyperlink :"
This means that the code is designed based on the assumption that the hyperlink string is stored in strHyperlink. It's a variable name so any other variable name will do as well.

The syntax where Call is used is optional in many cases, but I prefer to use it consistently as it highlights that a procedure is being used. VBA syntax (IMHO) is quite ambiguous when it comes to using procedures so I always try to make it as obvious and clear as possible that my code is using one when it is. Your earlier syntax would still work, but I will continue to use the clearer syntax in all my code.

I see no line where sFilePath is declared (See Require Variable Declaration), but as you seem to have that available already the code might look like :
Expand|Select|Wrap|Line Numbers
  1. Call objMessage.AddAttachment("C:\Documents\" & Split(sFilePath, "#")(1))

13 5981
zmbd
5,501 Expert Mod 4TB
Would you please provide an example of the relative link? I am fairly certain what you are doing; however, having that example would confirm.
Nov 21 '12 #2
CostasM
20
When I add the hyperlink in table I see it as "Folder1\testfile.txt",
using VBA code Trim(rst(8)) I get it as
"#Folder1\testfile.txt#",
but actual path is C:\DOCUMENTS\Folder1\testfile.txt

It seems that I get this link with Hyperlink properties.
I need only full path (absolute) without #-signs and any other info.

My base is located in C:\DOCUMENTS
Nov 21 '12 #3
NeoPa
32,556 Expert Mod 16PB
CostasM:
I need only full path (absolute) without #-signs and any other info.
I'm afraid that is just not true. There is no hyperlink without the hash (#) characters. See Error 7980: HyperlinkAddress or HyperlinkSubAddress read-only for Hyperlink for more about how hyperlinks work and how to set them in code when you need to.

For your situation you will need to extract the address that is relative (unless you have that information somewhere already). Fix the address. Unless it is the same every time it's run rou will need to use knowledge only you have. Lastly, redo the hyperlink as explained in the linked thread. Remember, the recipient of any email must be able to access the file from whatever is in the hyperlink, so drive letters are only adequate if they are necessarily the same on their PC. Otherwise you need to consider UNCs (\\Server\Share\folders...\File).
Nov 21 '12 #4
CostasM
20
to NeoPa
Thanks, but I don't need to send a link by e-mail, I need to send attached file, wich has a full path - C:\DOCUMENTS\Folder1\testfile.txt and this path stored in a table. I have table with 2 columns: "DocName" and "DocLink" where DocLink in Hyperlink format.
When I use
Expand|Select|Wrap|Line Numbers
  1. objMessage.AddAttachment "C:\DOCUMENTS\Folder1\testfile.txt" 
it works, but when I get this path from query(recordset) I see it as "#C:\DOCUMENTS\Folder1\testfile.txt#"
Probably I need to convert this path to text format in order to use it in objMessage.AddAttachment, but how ?
Nov 21 '12 #5
ADezii
8,834 Expert 8TB
Try something similar to (Code intentionally omitted):
Expand|Select|Wrap|Line Numbers
  1. Dim sNewFilePath As String
  2.  
  3. 'Assuming sFilePath = rst(8), namely
  4. sFilePath = "#C:\DOCUMENTS\Folder1\testfile.txt#"
  5.  
  6. sNewFilePath = CStr(Replace(sFilePath, "#", ""))
  7.  
  8. objMessage.AddAttachment sNewFilePath
  9.  
Nov 21 '12 #6
NeoPa
32,556 Expert Mod 16PB
CostasM:
When I add the hyperlink in table I see it as "Folder1\testfile.txt",
using VBA code Trim(rst(8)) I get it as
"#Folder1\testfile.txt#",
but actual path is C:\DOCUMENTS\Folder1\testfile.txt
CostasM:
it works, but when I get this path from query(recordset) I see it as "#C:\DOCUMENTS\Folder1\testfile.txt#"
Your original question was about the problem of the file address being stored as a relative link rather than a full one. My earlier response was directed at that as I can only respond to what you ask. It seems you're now saying that isn't a problem after all, but that you need to extract the value from a (fully formatted) string in Hyperlink format.

Ignoring the point of the original thread for the moment then (It has already been answered, after all, in the earlier post.), to get the value from within a Hyperlink formatted string you can use the Split() function. The following example code assumes you have already extracted that string into a variable called strHyperlink :

Expand|Select|Wrap|Line Numbers
  1. Call objMessage.AddAttachment(Split(strHyperlink, "#")(1))
Nov 21 '12 #7
NeoPa
32,556 Expert Mod 16PB
It seems I cross-posted with ADezii. His approach will work for you with the data as you've given as an example, but be aware that it will fail if given a hyperlink value containing either the first or third elements (Display value or Internal (within the file) link). If that's a possibility in your situation then you need another approach (such as using Split() of course).
Nov 21 '12 #8
CostasM
20
Sorry, I've mistaked in my last post, when I run VBA code I get only "#Folder1\testfile.txt#" without C:\DOCUMENTS\. In order to see full path I had to enter it manualy to Hyperlink field, but it is extra work, usually I use traditional window Hyperlink/Edit or F4 and see only the relative link to the file - Folder1\testfile.txt

Using Split() and Replace() functions I will get requested value, but for the case when I typed hyperlink manualy in Hyperlink window and not for the case when I choose file from spicified folder in browser.

How to find first part of the file's path - C:\DOCUMENTS\ ?
Nov 21 '12 #9
NeoPa
32,556 Expert Mod 16PB
CostasM:
How to find first part of the file's path - C:\DOCUMENTS\ ?
Good question. Now tell us how you know where to find it, so that we can consider how to go about programming it to do that same job.

If it's always C:\Documents\ then it's relatively straightforward (excuse the pun and see example code below), but if there is some reasoning behind where it should be found then we need to know that instead.

Expand|Select|Wrap|Line Numbers
  1. Call objMessage.AddAttachment("C:\Documents\" & Split(strHyperlink, "#")(1))
Nov 22 '12 #10
CostasM
20
toNeoPa
Thanks, now I check it in Immediate window and get sFilePath=Folder1\testfile.txt#Folder1\testfile.tx t
Can you clarify again how to use Split() function to get value from Hyperlink formatted string. I can add manualy "C:\Documents\" before and then to bound it with "sFilePath", but how to exclude all data after #-sign. It seems that it is 3rd element of Hyperlink filed.
Also original line was - objMessage.AddAttachment sFilePath,
why now - Call objMessage.AddAttachment(.....(1)) ?
Can you explain the 2nd part of this line after &
Expand|Select|Wrap|Line Numbers
  1. Call objMessage.AddAttachment("C:\Documents\" & Split(strHyperlink, "#")(1))
what does (1) parametr mean?

and what do you mean "I have already extracted that string into a variable called strHyperlink" ?
Nov 22 '12 #11
NeoPa
32,556 Expert Mod 16PB
Costa,
When you check the Help section for Split() you will see that it returns a value that is an array. The (1) after the call is how VBA says it wants to use element #1 from an array. The default array indexing starts from zero so element #1 is the second element - IE. The data between the first and second separator charactes ("#" in this case) in the text. Try the following from the Immediate Pane of the VBE window (Use Ctrl-G to get there) :
Expand|Select|Wrap|Line Numbers
  1. ?Split("A@B@C@D@E@F","@")(2)
You will see the value "C" printed.

CostasM:
and what do you mean "I have already extracted that string into a variable called strHyperlink" ?
I didn't ever say that, or anything that had that meaning. What I said was :
"The following example code assumes you have already extracted that string into a variable called strHyperlink :"
This means that the code is designed based on the assumption that the hyperlink string is stored in strHyperlink. It's a variable name so any other variable name will do as well.

The syntax where Call is used is optional in many cases, but I prefer to use it consistently as it highlights that a procedure is being used. VBA syntax (IMHO) is quite ambiguous when it comes to using procedures so I always try to make it as obvious and clear as possible that my code is using one when it is. Your earlier syntax would still work, but I will continue to use the clearer syntax in all my code.

I see no line where sFilePath is declared (See Require Variable Declaration), but as you seem to have that available already the code might look like :
Expand|Select|Wrap|Line Numbers
  1. Call objMessage.AddAttachment("C:\Documents\" & Split(sFilePath, "#")(1))
Nov 22 '12 #12
CostasM
20
To NeoPa
Great, Now it works ! Correct line wich I need is
Expand|Select|Wrap|Line Numbers
  1. Call objMessage.AddAttachment("C:\Documents\" & Split(sFilePath, "#")(0))
Thanks again. We can consider this thread closed, but 2 other questions have arisen from this :

** Extra questions removed as per site rules. See following post from NeoPa. **
Nov 23 '12 #13
NeoPa
32,556 Expert Mod 16PB
Costa,
You need to post each question in a separate thread. We don't allow multiple questions in the same thread as it would cause all sorts of problems.

May I suggest you take both extra question, add enough information for them to make good sense so they won't just be deleted immediately, then post them as separate threads. You may post links into this thread if you like to draw the attention of any experts already involved, once the threads have been posted. Please pay particular attention to the instructions about fixing the questions before posting though. We are not mind-readers, and technical questions nearly always rely on various detailed information, without which it will be impossible to respond with meaningful answers.

Best wishes.
Nov 23 '12 #14

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

Similar topics

15
by: Nick K. | last post by:
I recently began maintenance work on a production web server that is located in the root directory of a web server. I moved this into a sub web on my local web server in order to do work on it. I...
5
by: Soren Vejrum | last post by:
I am working on a web-based html editor using MSIE's designmode and iframes. Everything works just fine, but MSIE changes all my relative "a href" and "img src" links (i.e. "/index.asp") to...
6
by: Quiet Desperation | last post by:
I started with this: #section { position: relative; width: 100%; } and
4
by: louissan | last post by:
Hi all, I've met a problem with Opera and its ability to render absolutely positioned divs inside relatively positioned parent blocks, themselves contained inside a relatively positioned...
0
by: Flip | last post by:
I am retrieving a web page and then taking an inside table to display as an RSS feed. However, there are some relative URLs in there. Of course they work perfectly fine when using IE and...
19
by: Jerry M. Gartner | last post by:
Greetings: What is the best way to resolve paths within a document regardless of what path it is opened under? For example: I have x.php and it contains <img src="images...">, (amongst other...
1
by: GGG | last post by:
Neither go the way I want them to... Absolute doesn't get it right over multiple browsers. Relative puts it in the right place, but only the portion that it is "relative" the style, #wleMenu, is...
1
by: bernie | last post by:
what is the difference between relative and absolute addressing?
2
by: Garima12 | last post by:
There is htm page. In its body I am calling a class from stylesheet called TaskbarStyle(classname). Now I want to change the color of hyperlink in this page as well as their active link color. I am...
30
by: Bassem | last post by:
Hi, I've two div.s. One contains four images, when click on one, a JS function create an image corresponding to it and append it to the second div. I'm attempting to make each new image is to be...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.