Preserve percent sign values in VBA code?

jazzcar99

New Member
Joined
Aug 14, 2014
Messages
7
Hello All,

I have a code that opens outlook and pulls data from cells to send an email. There are cells that contain a percent sign (%). I want that to be preserved when it transfers to email, but it doesn't. I've noticed it also does that with the ampersand(sp?) sign. I realize these are part of the coding nature so my theory is that has something to do with it.

How can I preserve the values to read exactly with the percent sign (%) remaining? See below examples:


Here is my code:
Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long


Sub SendEMail()
Dim Email As String, Subj As String
Dim Msg As String, URL As String
Email = Range("C2")


Subj = Range("C2") & "_" & _
Range("AO2") & "_" & _
Range("AP2") & "_" & _
"Reactive" & "_" & _
Range("AM2") & "_" & _
Range("AK2")


Msg = "Customer Name: " & Range("C2") & vbCrLf & _
"Requestor Contact Name: " & Range("AL2") & vbCrLf & _
"Requestor Contact Email: " & Range("AN2") & vbCrLf & _
"Theatre: " & Range("AO2") & vbCrLf & _
"Segment: " & Range("AP2") & vbCrLf & _
"Descriptions: " & Range("AR2") & vbCrLf & _
"Cisco Expiring Quarter/Month: " & Range("AM2")



'Replace spaces with %20 (hex)
Subj = Application.WorksheetFunction.Substitute(Subj, " ", "%20")
Msg = Application.WorksheetFunction.Substitute(Msg, " ", "%20")

'Replace carriage returns with %0D%0A (hex)
Msg = Application.WorksheetFunction.Substitute(Msg, vbCrLf, "%0D%0A")

'Create the URL
URL = "mailto:" & Email & "?subject=" & Subj & "&body=" & Msg

'Execute the URL (start the email client)
ShellExecute 0&, vbNullString, URL, vbNullString, vbNullString, vbNormalFocus

'Wait two seconds before sending keystrokes
'Application.Wait (Now + TimeValue("0:00:02"))
'Application.SendKeys "%s"
End Sub


Here is what the data looks like:
Note the "Naming Variations" column is what I need the percent sign preserved in
image.png




Here is the result:
Note the descriptions line does not keep the formatting. Instead it has ? and no %.
image.png


Please note I am newer to VBA. I can figure out the response, but step-by-step answers are much appreciated! Thank you for your time!
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Your question is almost indecipherable. Which line of code isgiving the problem?
 
Upvote 0
If you want the value of a cell as it's formatted on the worksheet use its Text property.
 
Upvote 0
can you force an apostrophe to the front of that string making it text, then maybe to programme won't try and interpret as web address spacing
 
Upvote 0
If you want the value of a cell as it's formatted on the worksheet use its Text property.

Can you elaborate a little on this? I'm not sure how to accomplish this in VBA. Sorry for the ignorance, again I'm new to VBA. Possibly an example of how the code above should be changed.

Also to be more clear, the line of code that applies to that cell is in paragraph 4, line 6. The line below is "descriptions:"

Rich (BB code):
Msg = "Customer Name: " & Range("C2") & vbCrLf & _
"Requestor Contact Name: " & Range("AL2") & vbCrLf & _
"Requestor Contact Email: " & Range("AN2") & vbCrLf & _
"Theatre: " & Range("AO2") & vbCrLf & _
"Segment: " & Range("AP2") & vbCrLf & _
"Descriptions: " & Range("AR2") & vbCrLf & _
"Cisco Expiring Quarter/Month: " & Range("AM2")
 
Upvote 0
maybe

"Descriptions: " & "'" & Range("AR2") & vbCrLf & </pre>
 
Upvote 0
Mole999, can you be more specific? Again, sorry I am very new to VBA. Can you define "string" and also possibly post a code example of what you are referring to?
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top