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

Re: Gremlin space in string data by Karl

Karl
Wed May 24 14:54:36 CDT 2006

Greg Maxey wrote:
> 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.
--
Working without a .NET?
http://classicvb.org/



Re: Gremlin space in string data by Jonathan

Jonathan
Wed May 24 14:58:14 CDT 2006

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

"Greg Maxey" <gmaxey@mvps.org> wrote in message
news:1148498789.843671.194210@g10g2000cwb.googlegroups.com...
> 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
>


Re: Gremlin space in string data by Helmut

Helmut
Wed May 24 15:11:56 CDT 2006

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"



Re: Gremlin space in string data by Greg

Greg
Wed May 24 15:17:43 CDT 2006

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


Re: Gremlin space in string data by Karl

Karl
Wed May 24 15:49:39 CDT 2006

Jonathan West wrote:
> 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.
--
Working without a .NET?
http://classicvb.org/