access vba rgb function causes overflow - vba

Access VBA is giving me a runtime overflow error on the following subroutine invoked from an event procedure.
Private Sub TboThreat_Evaluate()
Dim i, red, orange, yellow As Integer
red = RGB(244, 90, 90)
orange = RGB(248, 192, 90)
yellow = RGB(242, 237, 14)
End Sub
red and orange are assigned correctly and yellow causes an overflow. Why? it makes no sense. I should have 12 bits of leading zeros. If I execute the yellow statement in the immediate window it works fine. In the event procedure the first two execute and yellow fails. If I reorder them yellow still fails.
It is easy enough to work around this but I'd really like to know why VBA has trouble doing simple bit manipulations.

Related

How to recolor Shapes/Lines

I have Shapes/Lines I need to recolor.
The problem I encounter is I can't just highlight everything and change line color, or Text Shapes that have no line color will then also now have lines.
How can I either take 2 reference variables for the select color and the recolor, or have the select color based off a currently selected object.
I want to recolor everything from Green to Black.
This is what I hashed together from other guides on the web. It selects objects without lines still (not a lot but some) and won't target anything that's within a group.
Sub Test()
Dim s As Shape
For Each s In ActivePage.Shapes
If s.Cells("LineColor").ResultStr(0) = "RGB(0, 176, 80)" Then
ActiveWindow.Select s, visSelect
End If
Next
End Sub
ActiveWindow.Select s, visSelect
This line just select shape, not change its color, try use this line
s.Cells("LineColor").FormulaU = "0"
If you need change shapes into group you must use recursive algorithm

Getting Text To Fit A Shape

I am coding using PowerPoint VBA and am trying to place text inside a rectangle shape, but ensure that the text fits (so there is no overflowing). I do not want the shape itself to resize, but the text to resize.
I have seen that I can use
oShp.TextFrame2.AutoSize = msoAutoSizeTextToFitShape
However, the problem with this is that the text will only resize after the user has clicked on the textbox when PowerPoint is in normal mode. I want this functionality when the PowerPoint is running!
I would be grateful to know is there a way to get the text automatically resized or do I need to find an alternative method?
Thank you for any comments!
I thought I would answer my question and close the thread.
After doing much research I found that there was no apparent method to get the text to auto-resize itself when the PowerPoint Show runs. I tried a number of approaches e.g. inserting text, trimming the text and turning word wrap off and on - however, none of these worked. I note (Bhavesh) I was fully aware of how to select the auto-size text settings via PowerPoint's GUI.
In the end my solution was to make a do loop and change the size of the text.
Below I pasted my key lines in the hope that it might help someone else who is trying to do the same. I made a variable overflow which attempts to assess if the height of the shape's textbox is bigger than the size of the rectangle.
Dim overflow As Integer
Dim counter As Integer
counter = 0
With ActivePresentation.Slides(i).Shapes(stringToTest)
overflow = CInt((.TextFrame.TextRange.BoundHeight) - (.Height - .TextFrame.MarginTop - .TextFrame.MarginBottom))
Do While overflow > 16 And counter < 50
'*** I note that the shape is overflowing when its value is >0 but I found 16 to be the most "aesthetically pleasing" number!
'*** Also using a counter prevents the code from potentially getting stuck in an infinite loop
If .TextFrame.TextRange.Font.Size > 20 Then
.TextFrame.TextRange.Font.Size = .TextFrame.TextRange.Font.Size - 1
Else
.TextFrame.TextRange.Font.Size = .TextFrame.TextRange.Font.Size - 0.5
End If
'**** By reducing the font size by 0.5 this provided a better fit for the text _
'**** (even better than using on PowerPoint's auto-size function!)
counter = counter + 1
overflow = CInt((.TextFrame.TextRange.BoundHeight) - (.Height - .TextFrame.MarginTop - .TextFrame.MarginBottom))
Loop
End With
In shape format, under text options, choose the option to shrink text on overflow.
Then, using .Shapes("Title 1").TextFrame.TextRange we input text via VBA.
The text automatically changes its font size.

Changing the ColorIndex of a DataLabel is not behaving as stated by the documentation

I am trying to change the color of a DataLabel, however so far I was only able to change the color to red or black.
I've tried to set the ColorIndex to the "Values" as well as "Names" according to the documentation:
https://learn.microsoft.com/en-us/office/vba/api/word.wdcolorindex
dim objChart as PowerPoint.Chart
objChart.SeriesCollection(1).DataLabels(2).Font.ColorIndex = 0
This code will change my DataLabel to Black, so far so good. Hoewever:
objChart.SeriesCollection(1).DataLabels(2).Font.ColorIndex = 8
will change the color to red, just as most of the other numbers (I have not tried all of them). Even better:
objChart.SeriesCollection(1).DataLabels(2).Font.ColorIndex = wdWhite
will throw an Error that wdWhite is not defined, while it is executed in debugging mode. If printing in the immediate window ? wdWhite it is Empty, which explains the Error as I use Option Explicit. However I am quite confused that it is not defined.
Also if I let
objChart.SeriesCollection(1).DataLabels(2).Font.ColorIndex = 0
and then print it in the immediate window it will return -1.
So I am kind of confused what is going on. So far I have not worked with ColorIndex but only with Color.RGB so I am not sure if or what I am doing wrong.
Do I maybe have to include a specifiy library?
Also maybe as additional information I am coding a macro in Excel that populates charts in a PowerPoint.
Any help is appreciated. Thanks in advance :)
1) I think you want Font.Color instead of ColorIndex.
2) You can use the named color constants, e.g. vbRed, vbBlack, etc.
objChart.SeriesCollection(1).DataLabels(2).Font.Color = vbWhite
Or you can use RGB for other colors as needed:
objChart.SeriesCollection(1).DataLabels(2).Font.Color = RGB(100, 50, 0)

Word VBA shape forecolor wdThemeColorAccent2 shows as ThemeColor 1 in the menu

I wrote some macro code in Word (Office 365) to set the color of a shape outline to one of the theme colors. The code for doing that to a shape looks like this:
shape.line.foreColor.ObjectThemeColor = wdThemeColorAccent2
By assigning a 'wdXX' color to the ObjectThemeColor field, the color of the line around the shape will automatically change when the document ColorTheme is changed.
My problem (or the first weirdness) is that when I assign Accent2 with the code above and then do: select the shape, Menu, Format, Shape Outline, and hover over the color box with a red outline (which marks the active line color), the tooltip says "Turquoise, Accent 1" not "Accent 2."
I would have expected the wdThemeColorAccent2 color to be called Accent 2 in the tooltip, but it is not.
The second problem is that there is apparently no way for me to assign the last color shown in the menu using macro code. Because of the offset (Accent 2 in code = Accent 1 in the menu), I would need to use wdThemeColorAccent7 in code to assign the last color shown in the menu (labeled Accent 6 in the tooltip).
I'm wondering if this is a bug in Word (it sure looks like it to me), or if I am doing something wrong. To reproduce the situation, I created a simple empty rectangle, selected it, and ran the line of code above to change the outline color of the shape. Here's a little subroutine that illustrates the problem (select your shape before running the subroutine).
Sub TestAccent()
Dim shp As Shape
Set shp = selection.ShapeRange(1)
shp.line.foreColor.ObjectThemeColor = wdThemeColorAccent4
shp.line.Weight = 0.5
shp.line.Visible = True
End Sub
I believe the colors in the "theme scale" (see image below) don't correspond to the names of the WdThemeColorIndex, but rather to the underlying numerical value. If you look in the VBA Editor's Object Browser (F2), and search wdThemeColorAccent you'll get the full list. Click on a member in the list and at the bottom you'll see the numerical value.
The value 0 is assigned to MainDark1 and isn't recognized by VBA. Values 1, 2 and 3 are assigned to MainLight1, MainDark2 and MainLight2 which are black, white and the first entry in the image (These colors repeat in the last four enumerations for background and text). Values 4 (wdThemeColorAccent4) through 9 (wdThemeColorAccent6) correspond to the remainder of the colors in the image below. (Note: more discussion after image!)
So, no, I don't think it's a bug, just your expectations don't match what the developers were thinking when they assigned the numerical enumeration to the enumeration names. Or maybe the people who designed the color schemes changed their minds after the VBA code was locked down... And I imagine the names you see in the tooltips are another step removed from the VBA. You might find the information in this article helpful.
If you use the values, rather than the names, things could be less confusing. Or, define your own Enum:
Public Enum ColorSchemeAccents
Accent1 = 3
Accent2 = 4
Accent3 = 5
Accent4 = 6
Accent5 = 7
Accent6 = 8
Accent7 = 9
Accent8 = 10
End Enum
Sub TestAccent()
Dim shp As Shape
Set shp = Selection.ShapeRange(1)
shp.Line.ForeColor.ObjectThemeColor = ColorSchemeAccents.Accent8
shp.Fill.ForeColor = RGB(250, 250, 250)
shp.Line.Weight = 2
shp.Line.Visible = True
End Sub
Although the ColorFormat object's .ObjectThemeColor is defined as a wdThemeColorIndex in fact the value depends on context.
If it is a Word object - such as text, then you should use the wdThemeColorIndex constants, but if it is an Office object - such as shape, then you have to use the msoThemeColorIndex constants. These are weirdly NOT the same - mostly the mso constants are one more than the wd constants, but not for the Background1&2 and Text1&2 cases - Text1&2 are the same in both cases, but for Background1&2 mso is two more than wd.
A side effect of this is that it appears impossible in VBA to set the Background2 colour, as its mso value is 16 and so out-of-range BUT if you use the native GUI to set it, it can be set to 16!
Looks really poor design/implementation that needs cleaning up!

Change the color of all red shapes and lines to black in visio

Hi I am trying to change the following
everything with red Background should be changed to black background
everything with red Line color should be changed to black line color
everything with red Font color should be changed to blue font color
Is this doable with a Macro?
I tried several stuff but that was all nonsense and in the end I as not able to change the color nor get the color of a shape to check it.
The shapes used are all the same standard rectangles with round borders and connectors. But that should not be the point of the question.
That code can helps!
Sub ttt()
Dim shp As Shape
For Each shp In ActivePage.Shapes
If shp.Cells("FillForegnd") = 2 Then shp.Cells("FillForegnd") = 0
If shp.Cells("LineColor") = 2 Then shp.Cells("LineColor") = 0
If shp.Cells("Char.Color") = 2 Then shp.Cells("Char.Color") = 4
Next
End Sub
But in versions 2007 and newer occurs troubles, because shape with red color of line/fill/font can use themes. In this case in shapesheet cells you can find values like THEMEGUARD(RGB(255,0,0))
Also you can read this thread VBA Code for Shape Color Changes on Double Click