In Word 2003 VBA, if I wanted to assign a string to a variable, I could
do it with the following code:
Sub eraseme()
Dim MyText As String
MyText = "Here is my wonderful string, assigned to its very own variable"
MsgBox MyText
End Sub
But supposing the string I wanted to assign to a variable was this?
Here is my "wonderful" string, assigned to (its very own) variable. 'I
hope.
And it had all those things that mean something special in VBA, such as
quotation marks, parentheses, apostrophes? What would be the correct
approach to that?
Regards,
Alan Stancliff
Greg Maxey - 02 Mar 2008 23:44 GMT
Alan,
Sub Test()
Dim pStr As String
pStr = "Here is my " & Chr(34) & "wonderful" & Chr(34) & " string, assigned"
_
& " to (its very own) variable."
MsgBox pStr
'or
pStr = "Here is my ""wonderful"" string, assigned" _
& " to (its very own) variable."
MsgBox pStr
End Sub

Signature
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Greg Maxey - Word MVP
My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> In Word 2003 VBA, if I wanted to assign a string to a variable, I could do
> it with the following code:
[quoted text clipped - 15 lines]
>
> Alan Stancliff
Jay Freedman - 02 Mar 2008 23:55 GMT
>In Word 2003 VBA, if I wanted to assign a string to a variable, I could
>do it with the following code:
[quoted text clipped - 15 lines]
>
>Alan Stancliff
Hi Alan,
The only character you need to be concerned with is the double quote, because
that's the marker for the beginning and end of a string literal. All the other
characters can be plunked into the literal just as they are.
There are a couple of ways to deal with double quotes:
- You can type the double quote twice where it should be embedded in the string:
MyText = "Here is my ""wonderful"" string with (parentheses)."
- You can declare a constant whose value is a double quote character, and
concatenate that into the string where needed:
Const qt = """"
MyText = "Here is my " & qt & "wonderful" & qt & " string."
- You can use the Chr(34) function, where 34 is the ASCII value of the double
quote character. Personally, I think this is too distracting when reading code.
MyText = "Here is my " & Chr(34) & "wonderful" & _
Chr(34) & " string with (parentheses)."
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
Alan Stancliff - 04 Mar 2008 06:19 GMT
Hi Jay and Greg,
Thanks for the information and the help.
Regards,
Alan Stancliff
>> In Word 2003 VBA, if I wanted to assign a string to a variable, I could
>> do it with the following code:
[quoted text clipped - 45 lines]
> Microsoft Word MVP FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.