Gremlin space in string data

G

Greg Maxey

With the extensive help of Jay Freedman and Jonathan West, I am
beginning to understand color in Word. I have put together a macro for
extracting the Long and RBG value of a color selected in the
Format>Borders and Shading>Shading dialog. I have one problem. For
some odd reason there is an extra space in the second message box
dispaly:

oColorRBG = Str$(rVal) & ", " & Str$(gVal) & ", " & Str$(bVal)
MsgBox "Color long value: " & oColorLng
MsgBox "Color RBG value: (" & oColorRBG & ")"

For "Red" is should dispay: Color RBG value: (255, 0, 0) but it is
coming out
Color RBG value: ( 255, 0, 0)

I can't figure out how to close up the space between the ( and 255.

I realize that this is a minor nit, but would like to figure it out
just the same. Thanks.

Here is the complete code:

Sub QuickAndEasyColorData()
Dim oDoc As Word.Document
Dim oRng As Word.Range
Set oDoc = Documents.Add
Set oRng = oDoc.Range
Dim oColorLng As Long
Dim rVal As Long
Dim gVal As Long
Dim bVal As Long
Dim oColorRBG As String
oRng.InsertAfter "Sample Text"
oRng.MoveEnd wdCharacter, -1
Application.ScreenRefresh
oRng.Select
With Dialogs(wdDialogFormatBordersAndShading)
If .Show = -1 Then
.Execute
Application.ScreenRefresh
oColorLng = oRng.Shading.BackgroundPatternColor
rVal = oColorLng Mod 256
bVal = Int(oColorLng / 65536)
gVal = Int((oColorLng - (bVal * 65536) - rVal) / 256)
oColorRBG = Str$(rVal) & ", " & Str$(gVal) & ", " & Str$(bVal)
MsgBox "Color long value: " & oColorLng
MsgBox "Color RBG value: (" & oColorRBG & ")"
End If
End With
oDoc.Close wdDoNotSaveChanges
End Sub
 
K

Karl E. Peterson

Greg said:
With the extensive help of Jay Freedman and Jonathan West, I am
beginning to understand color in Word. I have put together a macro
for extracting the Long and RBG value of a color selected in the
Format>Borders and Shading>Shading dialog. I have one problem. For
some odd reason there is an extra space in the second message box
dispaly:

oColorRBG = Str$(rVal) & ", " & Str$(gVal) & ", " & Str$(bVal)
MsgBox "Color long value: " & oColorLng
MsgBox "Color RBG value: (" & oColorRBG & ")"

For "Red" is should dispay: Color RBG value: (255, 0, 0) but it is
coming out
Color RBG value: ( 255, 0, 0)

I can't figure out how to close up the space between the ( and 255.

I realize that this is a minor nit, but would like to figure it out
just the same. Thanks.

This is a "legacy" MSBASIC issue. Str() always returns a leading space to
allow for the implied sign, unless of course the number is negative. Use
CStr() instead.
 
J

Jonathan West

Hi Greg

The Str$() function converts numbers to strings in a very particular way. If
the number is negative, it has a leading "-", and if it is positive, it has
a leading space. The Str$() is a very old part of BASIC, and dates back to
the days of teletype machines, where you would want the same number of
characters to be used for printing a number irrespective of whether it was
positive or negative.

Use CStr() instead of Str$(). CStr() doesn't include a leading space in the
string it produces.

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
H

Helmut Weber

Hi Submariner,

by default, when converting a number to a string,
Str(number) returns a space as first character.

Use CStr() instead.

Why this is so, is another question, which I can't answer.
Maybe, once there was a good reason for it.

Or someone at MS poured a cup of coffee into the keyboard
at the wrong time, or ...

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
G

Greg Maxey

As you all came in with the answer about the same time I will just
thank you all here. Thanks.
 
K

Karl E. Peterson

Jonathan said:
The Str$() is a very old part of
BASIC, and dates back to the days of teletype machines, where you
would want the same number of characters to be used for printing a
number irrespective of whether it was positive or negative.

Yep! Now that you mention it, I do recall that being exactly the case in
the early-70s.

Long before MSBASIC appeared.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top