John500
asked on
Accessing Colors Other Than VB's Eight (i.e. vbBlack, vbWhite....)
I'm currently developing reports using VB and a report tool (Crystal Reports) which is an Add-In to VB. Crystal Reports offers background colors such as Navy and Silver.
I decided it would be best to change the background color to white (from
silver) during the print job to save toner. However, getting the colors
back to their original state has been a trick for one reason. That is, not
all colors offered by Crystal Reports are available in VB while programming.
I need the report header background to be Navy and the header text to be
white. All other bacground should be silver. I have the code that turns
the colors to white and black within the PrintClicked command, and I'm almost
done with the code which turns it all back within the RefreshClicked
command:
For the Print Job:
Private Sub CRViewer1_PrintButtonClick ed(UseDefa ult As Boolean)
Dim crSections As CRAXDRT.Sections
Dim crSection As CRAXDRT.Section
Dim crTextObject As Object
Dim crReportObject As Object
Report.SelectPrinter Printer.DriverName, Printer.DeviceName, Printer.Port
Set crSections = Report.Sections
For Each crSection In crSections
crSection.BackColor = vbWhite
Report.Text8.TextColor = vbBlack
Next crSection
End Sub
Turning The Colors Back:
Private Sub CRViewer1_RefreshButtonCli cked(UseDe fault As Boolean)
Dim crSections As CRAXDRT.Sections
Dim crSection As CRAXDRT.Section
Dim crTextObject As Object
Dim crReportObject As Object
Font.Color
Report.SelectPrinter Printer.DriverName, Printer.DeviceName, Printer.Port
Set crSections = Report.Sections
For Each crSection In crSections
crSection.BackColor = vbsilver
Report.Section1.BackColor = vbNavy
Report.Text8.TextColor = vbWhite
Next crSection
End Sub
The problem here is that there is no such thing as 'vbSilver' or 'vbNavy'.
How can I get those colors? I was thinking I could create 'TextColor'
variables within the PrintClicked command and set them to the Navy and Silver objects of the header background and report background. This would be done previous to turning the backgrounds white. If that sounds good, can you tell me how the syntax is done and whether I can call these variables within the RefreshButtonClicked command when resetting the colors?
I'll take any suggestions for a different approach but I think this might be simplest. Can you edit the code above if my idea sounds good, thanks.
Here are the only VB colors available:
vbBlack
vbRed
vbGreen
vbYellow
vbBlue
vbMagenta
vbCyan
vbWhite
I decided it would be best to change the background color to white (from
silver) during the print job to save toner. However, getting the colors
back to their original state has been a trick for one reason. That is, not
all colors offered by Crystal Reports are available in VB while programming.
I need the report header background to be Navy and the header text to be
white. All other bacground should be silver. I have the code that turns
the colors to white and black within the PrintClicked command, and I'm almost
done with the code which turns it all back within the RefreshClicked
command:
For the Print Job:
Private Sub CRViewer1_PrintButtonClick
Dim crSections As CRAXDRT.Sections
Dim crSection As CRAXDRT.Section
Dim crTextObject As Object
Dim crReportObject As Object
Report.SelectPrinter Printer.DriverName, Printer.DeviceName, Printer.Port
Set crSections = Report.Sections
For Each crSection In crSections
crSection.BackColor = vbWhite
Report.Text8.TextColor = vbBlack
Next crSection
End Sub
Turning The Colors Back:
Private Sub CRViewer1_RefreshButtonCli
Dim crSections As CRAXDRT.Sections
Dim crSection As CRAXDRT.Section
Dim crTextObject As Object
Dim crReportObject As Object
Font.Color
Report.SelectPrinter Printer.DriverName, Printer.DeviceName, Printer.Port
Set crSections = Report.Sections
For Each crSection In crSections
crSection.BackColor = vbsilver
Report.Section1.BackColor = vbNavy
Report.Text8.TextColor = vbWhite
Next crSection
End Sub
The problem here is that there is no such thing as 'vbSilver' or 'vbNavy'.
How can I get those colors? I was thinking I could create 'TextColor'
variables within the PrintClicked command and set them to the Navy and Silver objects of the header background and report background. This would be done previous to turning the backgrounds white. If that sounds good, can you tell me how the syntax is done and whether I can call these variables within the RefreshButtonClicked command when resetting the colors?
I'll take any suggestions for a different approach but I think this might be simplest. Can you edit the code above if my idea sounds good, thanks.
Here are the only VB colors available:
vbBlack
vbRed
vbGreen
vbYellow
vbBlue
vbMagenta
vbCyan
vbWhite
Erick37 has listed the correct concept. Now, how to know the hex code for the color you wish?
Here is what I do. I use MS Access to get the color required. Since there are more colors listed by default there. In Access, create a form, and add a plain box (rectangle). In the BackColor properties, find the color you are looking for. Using the Build button, you can choose from as many colors as your video driver can display (ie 16,000+). Once you have selected the proper color, you are given the Long Integer format for that color. You can use that number in your VB code or you can convert it to hex and use that.
Good luck
Here is what I do. I use MS Access to get the color required. Since there are more colors listed by default there. In Access, create a form, and add a plain box (rectangle). In the BackColor properties, find the color you are looking for. Using the Build button, you can choose from as many colors as your video driver can display (ie 16,000+). Once you have selected the proper color, you are given the Long Integer format for that color. You can use that number in your VB code or you can convert it to hex and use that.
Good luck
Alternatively, just save the default colors before you change them. That way you do not need to know the actual value.
Ex.
Private PrevTextColor As OLE_COLOR
'Before you change the color:
PrevTextColor = Report.Text8.TextColor
'...
'Restore the color:
Report.Text8.TextColor = PrevTextColor
Ex.
Private PrevTextColor As OLE_COLOR
'Before you change the color:
PrevTextColor = Report.Text8.TextColor
'...
'Restore the color:
Report.Text8.TextColor = PrevTextColor
ASKER
Erick37,
I tried what you said but I'm doing something wrong. What needs to be corrected below so that 'SilverTextColor' & 'NavyTextColor' can be used in the CRViewer1_RefreshButtonCli cked?
I tried declaring the variables as 'Private' but I was getting the following error for the word 'Private':
Compile error: Invalid attribute in Sub or Function
Private Sub CRViewer1_PrintButtonClick ed(UseDefa ult As Boolean)
Dim crSections As CRAXDRT.Sections
Dim crSection As CRAXDRT.Section
Dim crTextObject As Object
Dim crReportObject As Object
Dim SilverTextColor As OLE_COLOR
Dim NavyTextColor As OLE_COLOR
Report.SelectPrinter Printer.DriverName, Printer.DeviceName, Printer.Port
Set crSections = Report.Sections
For Each crSection In crSections
SilverTextColor = crSection.BackColor
NavyTextColor = Report.Section1.BackColor
crSection.BackColor = vbWhite
Report.Text8.TextColor = vbBlack
Next crSection
End Sub
Private Sub CRViewer1_RefreshButtonCli cked(UseDe fault As Boolean)
Dim crSections As CRAXDRT.Sections
Dim crSection As CRAXDRT.Section
Dim crTextObject As Object
Dim crReportObject As Object
Report.SelectPrinter Printer.DriverName, Printer.DeviceName, Printer.Port
Set crSections = Report.Sections
For Each crSection In crSections
crSection.BackColor = SilverTextColor
Report.Section1.BackColor = NavyTextColor
Report.Text8.TextColor = vbWhite
Next crSection
End Sub
I tried what you said but I'm doing something wrong. What needs to be corrected below so that 'SilverTextColor' & 'NavyTextColor' can be used in the CRViewer1_RefreshButtonCli
I tried declaring the variables as 'Private' but I was getting the following error for the word 'Private':
Compile error: Invalid attribute in Sub or Function
Private Sub CRViewer1_PrintButtonClick
Dim crSections As CRAXDRT.Sections
Dim crSection As CRAXDRT.Section
Dim crTextObject As Object
Dim crReportObject As Object
Dim SilverTextColor As OLE_COLOR
Dim NavyTextColor As OLE_COLOR
Report.SelectPrinter Printer.DriverName, Printer.DeviceName, Printer.Port
Set crSections = Report.Sections
For Each crSection In crSections
SilverTextColor = crSection.BackColor
NavyTextColor = Report.Section1.BackColor
crSection.BackColor = vbWhite
Report.Text8.TextColor = vbBlack
Next crSection
End Sub
Private Sub CRViewer1_RefreshButtonCli
Dim crSections As CRAXDRT.Sections
Dim crSection As CRAXDRT.Section
Dim crTextObject As Object
Dim crReportObject As Object
Report.SelectPrinter Printer.DriverName, Printer.DeviceName, Printer.Port
Set crSections = Report.Sections
For Each crSection In crSections
crSection.BackColor = SilverTextColor
Report.Section1.BackColor = NavyTextColor
Report.Text8.TextColor = vbWhite
Next crSection
End Sub
You need to declare SilverTextColor and NavyTextColor outside of the functions so that both functions can use the same variable.
eg
'~~~~Form Code~~~~
Option Explicit
Private SilverTextColor As OLE_COLOR
Private NavyTextColor As OLE_COLOR
Private Sub CRViewer1_PrintButtonClick ed(UseDefa ult As Boolean)
'... your sub
End Sub
Private Sub CRViewer1_RefreshButtonCli cked(UseDe fault As Boolean)
'...the other sub
End Sub
eg
'~~~~Form Code~~~~
Option Explicit
Private SilverTextColor As OLE_COLOR
Private NavyTextColor As OLE_COLOR
Private Sub CRViewer1_PrintButtonClick
'... your sub
End Sub
Private Sub CRViewer1_RefreshButtonCli
'...the other sub
End Sub
ASKER
Erick37,
I did as you said and moved the two lines outside the functions and it compiled. However, the background colors of silver and navy aren't getting changed. Note that the text letters are getting changed back to white.
Do you have any suggestions on how I could debug this or why the colors aren't getting changed? Thanks.
I did as you said and moved the two lines outside the functions and it compiled. However, the background colors of silver and navy aren't getting changed. Note that the text letters are getting changed back to white.
Do you have any suggestions on how I could debug this or why the colors aren't getting changed? Thanks.
ASKER
Erick37,
I forgot to add in the last comment, is it possible these variables should be declared as report objects like:
Private SilverBackColor As crSection.BackColor
Private NavyBackColor As crSection.BackColor
I don't know if this makes sense, just a guess.
I forgot to add in the last comment, is it possible these variables should be declared as report objects like:
Private SilverBackColor As crSection.BackColor
Private NavyBackColor As crSection.BackColor
I don't know if this makes sense, just a guess.
Look at the values that you have assigned in each sub or add a breakpoint in the For loop and examine the variables.
For Each crSection In crSections
SilverTextColor = crSection.BackColor
NavyTextColor = Report.Section1.BackColor
'Confirm that a color is being assigned
Debug.Print Hex(SilverTextColor); Hex(NavyTextColor)
crSection.BackColor = vbWhite
Report.Text8.TextColor = vbBlack
Next crSection
'...
For Each crSection In crSections
Debug.Print Hex(SilverTextColor); Hex(NavyTextColor)
crSection.BackColor = SilverTextColor
Report.Section1.BackColor = NavyTextColor
Report.Text8.TextColor = vbWhite
Next crSection
For Each crSection In crSections
SilverTextColor = crSection.BackColor
NavyTextColor = Report.Section1.BackColor
'Confirm that a color is being assigned
Debug.Print Hex(SilverTextColor); Hex(NavyTextColor)
crSection.BackColor = vbWhite
Report.Text8.TextColor = vbBlack
Next crSection
'...
For Each crSection In crSections
Debug.Print Hex(SilverTextColor); Hex(NavyTextColor)
crSection.BackColor = SilverTextColor
Report.Section1.BackColor = NavyTextColor
Report.Text8.TextColor = vbWhite
Next crSection
One bug: You change the Backcolor in the Loop, so on the second loop, SilverTextColor is set to vbWhite.
Assign SilverTextColor to one of the Sections color BEFORE you enter the loop.
For Each crSection In crSections
'On second entry, backcolor has been set to white
'So silver is white
SilverTextColor = crSection.BackColor
NavyTextColor = Report.Section1.BackColor
'Change to white
crSection.BackColor = vbWhite
Report.Text8.TextColor = vbBlack
Next crSection
Assign SilverTextColor to one of the Sections color BEFORE you enter the loop.
For Each crSection In crSections
'On second entry, backcolor has been set to white
'So silver is white
SilverTextColor = crSection.BackColor
NavyTextColor = Report.Section1.BackColor
'Change to white
crSection.BackColor = vbWhite
Report.Text8.TextColor = vbBlack
Next crSection
ASKER
Erick37,
Still having some trouble. I'm new to VB so I'm still trying to understand the different 'types' and so on... I grabed much of this code from an example program. You get the idea...
I understand exactly when you say "On second entry, backcolor has been set to white, so silver is white" However, I'm not sure where to make the assignment when you say "Assign SilverTextColor to one of the Sections color BEFORE you enter the loop"
I assigned the variables as:
Private Sub CRViewer1_PrintButtonClick ed(UseDefa ult As Boolean)
Dim crSections As CRAXDRT.Sections
Dim crSection As CRAXDRT.Section
Dim crTextObject As Object
Dim crReportObject As Object
Report.SelectPrinter Printer.DriverName, Printer.DeviceName, Printer.Port
Set crSections = Report.Sections
SilverBackColor = crSection.BackColor
NavyBackColor = Report.Section1.BackColor
For Each crSection In crSections
crSection.BackColor = vbWhite
Report.Text8.TextColor = vbBlack
Next crSection
End Sub
But I got the following error:
Run-time error '91': Object variable or With block variable not set
Can you edit the code the way you think it should work? Thanks
Still having some trouble. I'm new to VB so I'm still trying to understand the different 'types' and so on... I grabed much of this code from an example program. You get the idea...
I understand exactly when you say "On second entry, backcolor has been set to white, so silver is white" However, I'm not sure where to make the assignment when you say "Assign SilverTextColor to one of the Sections color BEFORE you enter the loop"
I assigned the variables as:
Private Sub CRViewer1_PrintButtonClick
Dim crSections As CRAXDRT.Sections
Dim crSection As CRAXDRT.Section
Dim crTextObject As Object
Dim crReportObject As Object
Report.SelectPrinter Printer.DriverName, Printer.DeviceName, Printer.Port
Set crSections = Report.Sections
SilverBackColor = crSection.BackColor
NavyBackColor = Report.Section1.BackColor
For Each crSection In crSections
crSection.BackColor = vbWhite
Report.Text8.TextColor = vbBlack
Next crSection
End Sub
But I got the following error:
Run-time error '91': Object variable or With block variable not set
Can you edit the code the way you think it should work? Thanks
My mistake.
Your first code should work, since each time through the loop, crSection refers to a different section.
What values did you get from the Debug.print statements?
Your first code should work, since each time through the loop, crSection refers to a different section.
What values did you get from the Debug.print statements?
ASKER
Erick37,
I put the lines back where they were and put in the debug code that you mentioned. Tell me where the output appears for the debug statements. I am very familar with debugging in VC++ but I can't tell how to view the variables using VB while the program is running.
I used the 'Run To Cursor' method and placed the cursor one line past the Debug statement. I also let the entire program run, but there was still nothing like a MessageBox to reveal the values. What am I misssing? Thanks.
I put the lines back where they were and put in the debug code that you mentioned. Tell me where the output appears for the debug statements. I am very familar with debugging in VC++ but I can't tell how to view the variables using VB while the program is running.
I used the 'Run To Cursor' method and placed the cursor one line past the Debug statement. I also let the entire program run, but there was still nothing like a MessageBox to reveal the values. What am I misssing? Thanks.
Debug.Print statements appear in the Immediate window.
On the menu go to View->Immediate Window
I like to keep it docked on the bottom.
On the menu go to View->Immediate Window
I like to keep it docked on the bottom.
ASKER
Erick37,
Got any ideas why the 'Debug.Print statements don't return any info in the 'Immediate Window'? I ran the program without any breaks but nothing shows up.
I tried 'Running to cursor' and then typing 'Print crSection.BackColor' in the Immediate window, but it's as if the window is locked up and doesn't respond
I also tried setting a couple 'Watches'. I'm able to set the watches but when the program runs, the watches disappear from the window??
Got any ideas why the 'Debug.Print statements don't return any info in the 'Immediate Window'? I ran the program without any breaks but nothing shows up.
I tried 'Running to cursor' and then typing 'Print crSection.BackColor' in the Immediate window, but it's as if the window is locked up and doesn't respond
I also tried setting a couple 'Watches'. I'm able to set the watches but when the program runs, the watches disappear from the window??
ASKER
Erick37,
I see now that the Watch window displays info after the program terminates. Here is what I got:
Watch : : SilverBackColor : <Out of context> : Empty : Form1.CRViewer1_PrintButto nClicked
Watch : : crSection.BackColor : <Out of context> : Empty : Form1.CRViewer1_PrintButto nClicked
I see now that the Watch window displays info after the program terminates. Here is what I got:
Watch : : SilverBackColor : <Out of context> : Empty : Form1.CRViewer1_PrintButto
Watch : : crSection.BackColor : <Out of context> : Empty : Form1.CRViewer1_PrintButto
Start a new test project with a Command Button on the form.
Code:
Private Sub Command1_Click()
Static i As Long
i = i + 1
Debug.Print "Click "; i
End Sub
Also, in your original code, add a breakpoint to the line where you assign the colors. By placing the mouse over a variable name in the code, the value will be displayed. Press F8 to step through the code and watch the variables.
What varsion of VB are you using?
Code:
Private Sub Command1_Click()
Static i As Long
i = i + 1
Debug.Print "Click "; i
End Sub
Also, in your original code, add a breakpoint to the line where you assign the colors. By placing the mouse over a variable name in the code, the value will be displayed. Press F8 to step through the code and watch the variables.
What varsion of VB are you using?
ASKER
Erick,
I'm using the MS Visual Studio, Enterprise Edition which has VB 6.0.
I added the Command button as you said, and it works. I figured the next step was to add modify the command as follows:
Private Sub Command1_Click()
Static i As Long
i = i + 1
Debug.Print "Click "; i
Debug.Print Hex(SilverBackColor); Hex(NavyBackColor)
End Sub
When I clicked this button I would get:
Click 1
00
Click 2
00
Click 3
00
Does this mean the values for SilverBackColor & NavyBackColor are 0 and 0
After adding the breakpoint I still wasn't able to view the value of variables. I know what you are talking about when you say "By placing the mouse over a variable name in the code, the value will be displayed". I do this in VC++ daily. At this point, I don't know why VB isn't working this way. I'm wondering if there is some option I need to select so that the values display?
I'm using the MS Visual Studio, Enterprise Edition which has VB 6.0.
I added the Command button as you said, and it works. I figured the next step was to add modify the command as follows:
Private Sub Command1_Click()
Static i As Long
i = i + 1
Debug.Print "Click "; i
Debug.Print Hex(SilverBackColor); Hex(NavyBackColor)
End Sub
When I clicked this button I would get:
Click 1
00
Click 2
00
Click 3
00
Does this mean the values for SilverBackColor & NavyBackColor are 0 and 0
After adding the breakpoint I still wasn't able to view the value of variables. I know what you are talking about when you say "By placing the mouse over a variable name in the code, the value will be displayed". I do this in VC++ daily. At this point, I don't know why VB isn't working this way. I'm wondering if there is some option I need to select so that the values display?
Tools->Options->Auto Data Tips
to turn on the data tips.
If debug.Print says 0, then it is 0.
I don't quite know why it is zero.
to turn on the data tips.
If debug.Print says 0, then it is 0.
I don't quite know why it is zero.
ASKER
Eric,
Maybe some progress here. I moved the code you gave me into the PrintButtonClicked event rather than putting it in separate 'Command' button. Therefore, I'm guessing that with each iteration of FOR the loop I got the values of something. I'd like to say the values pertain to the Silver and Navy variables but it looks like there is only one value per loop, yes/no?
Click 1
800000800000
Click 2
C0C0C0FFFFFF
Click 3
C0C0C0FFFFFF
Click 4
C0C0C0FFFFFF
Click 5
C0C0C0FFFFFF
Click 6
C0C0C0FFFFFF
Click 7
FFFFFFFFFFFFFF
Here is the code I used again:
For Each crSection In crSections
SilverBackColor = crSection.BackColor
NavyBackColor = Report.Section1.BackColor
Static i As Long
i = i + 1
Debug.Print "Click "; i
Debug.Print Hex(SilverBackColor); Hex(NavyBackColor)
crSection.BackColor = vbWhite
Report.Text8.TextColor = vbBlack
Next crSection
Maybe some progress here. I moved the code you gave me into the PrintButtonClicked event rather than putting it in separate 'Command' button. Therefore, I'm guessing that with each iteration of FOR the loop I got the values of something. I'd like to say the values pertain to the Silver and Navy variables but it looks like there is only one value per loop, yes/no?
Click 1
800000800000
Click 2
C0C0C0FFFFFF
Click 3
C0C0C0FFFFFF
Click 4
C0C0C0FFFFFF
Click 5
C0C0C0FFFFFF
Click 6
C0C0C0FFFFFF
Click 7
FFFFFFFFFFFFFF
Here is the code I used again:
For Each crSection In crSections
SilverBackColor = crSection.BackColor
NavyBackColor = Report.Section1.BackColor
Static i As Long
i = i + 1
Debug.Print "Click "; i
Debug.Print Hex(SilverBackColor); Hex(NavyBackColor)
crSection.BackColor = vbWhite
Report.Text8.TextColor = vbBlack
Next crSection
ASKER
Erick,
I added an additional 'Print' rather than using just one. Here was the ouput:
Click 1
800000
800000
Click 2
C0C0C0
FFFFFF
Click 3
C0C0C0
FFFFFF
Click 4
C0C0C0
FFFFFF
Click 5
C0C0C0
FFFFFF
Click 6
C0C0C0
FFFFFF
Click 7
FFFFFFFF
FFFFFF
How do you interpret this? It seems both the Navy and Silver have the same value of 800000. I decided to use these values in the RefreshButtonClicked event and the color did change from an all white background to all dark green?! Here is the code:
For Each crSection In crSections
Debug.Print Hex(SilverBackColor); Hex(NavyBackColor)
crSection.BackColor = 800000
Report.Section1.BackColor = 800000
Report.Text8.TextColor = vbWhite
Next crSection
What would you suggest I do to obtain the correct colors?
I added an additional 'Print' rather than using just one. Here was the ouput:
Click 1
800000
800000
Click 2
C0C0C0
FFFFFF
Click 3
C0C0C0
FFFFFF
Click 4
C0C0C0
FFFFFF
Click 5
C0C0C0
FFFFFF
Click 6
C0C0C0
FFFFFF
Click 7
FFFFFFFF
FFFFFF
How do you interpret this? It seems both the Navy and Silver have the same value of 800000. I decided to use these values in the RefreshButtonClicked event and the color did change from an all white background to all dark green?! Here is the code:
For Each crSection In crSections
Debug.Print Hex(SilverBackColor); Hex(NavyBackColor)
crSection.BackColor = 800000
Report.Section1.BackColor = 800000
Report.Text8.TextColor = vbWhite
Next crSection
What would you suggest I do to obtain the correct colors?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Const VBSILVER = &H808080
Colors are in the form BBGGRR (hex)