Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2008

Tip: Looking for answers? Try searching our database.

Assigning a String to a Variable in Word 2003

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alan Stancliff - 02 Mar 2008 23:05 GMT
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.

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.