Powerpoint VBA - remove border/line/outline from shape - vba

I thought it would be an easy task: remove the line/border from a selected shape via VBA.
(set shp = ActiveWindow.Selection.ShapeRange(1))
First try: shp.line.weight = 0 --> there is still a very thin line around the shape. Interesting: same behaviour via GUI: if I set the line weight to 0 there a very thin line is still visible.
Second try: shp.line.visible = false --> line disappears but in the GUI none of the three options "no line", "single colored line", "gradient line" is selected. Deselecting and selecting the shape again let's "single colored line" flicker but in the end no option is selected. Weird ...
Is this a (known) bug?
Is there another VBA-option to disable the line?
What I tried else:
I checked line.style and line.dashstyle of a shape where I set "no line" via the GUI. Both values are -2. But it is not possible to set these values via VBA (error no valid value)
(Office 365)

Try to select the shape first. Then write one of your codes above

Try shp.Line.Visible = msoFalse, it worked for me.
You can find other LineFormat properties here https://learn.microsoft.com/en-us/office/vba/api/powerpoint.lineformat

Related

Detect if selected shape / table is in edit mode with VBA?

I'm trying to write a PowerPoint VBA macro that would act differently whether a shape is merely selected or is in edit mode (i.e. the cursor to edit the text is present, see image).
Is there a way to check this using VBA in PowerPoint? I haven't been able to find on this anything so far
My ultimate goal being:
If just the shape is selected, the macro will align the shape left
If the shape is in edit mode, the macro will align the text left (and leave the shape where it is)
Any help would be appreciated
Best
You can do this by using Selection.Type. If it returns 2, the shape is selected. If the insertion point is in the text, it will return 3:
Sub DetectShapeOrText()
MsgBox ActiveWindow.Selection.Type
End Sub
PpSelectionType enumeration (PowerPoint)

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!

How to run VBA macros in all sheets

I am new to VBA, and I have a sheet which has VBA macros assigned to shapes. It works fine on the same sheet, but when I copy and paste the shapes in the next sheet or create a new shape and assign the same macro it does not work. Kindly help me. Below is the code.
ActiveSheet.Shapes("CTSnext").Fill.ForeColor.SchemeColor = Black
Thanks for your response !!
However, Let me explain the issue.
In Sheet 1 I have 4 shapes Named
1) Cancelling the service
2) Next
3) Not interested
4) Reset
And all 3 shapes (2,3,4) will be in forecolor as white so it will be invisible.
When user clicks on "Cancelling the service" "Next" will appear (Meaning the fore color will change to black). And when "Next" is clicked "Not interested" will appear (Meaning the fore color will change to black). And when user clicks on "Reset" both "Next" & "Not interested" will turn into invisible (Meaning the fore color will change to white). Until now everything works fine.
Now i want to have a shape in Sheet 2 which says "Reset". So when user clicks on it the same function (Meaning the VBA of the "Reset" button in Sheet 1) should happen.
Below are the code.
For Cancel
ActiveSheet.Shapes("Next").Fill.ForeColor.SchemeColor = Black
For Next
ActiveSheet.Shapes("Notinterested").Fill.ForeColor.SchemeColor = Black
For Reset
ActiveSheet.Shapes("Next").Fill.ForeColor.SchemeColor = 1
ActiveSheet.Shapes("Notinterested").Fill.ForeColor.SchemeColor = 1
I hope i made myself clear, also i don't know to how to attach the excel file to this page so it will be helpful for you all to understand my need.
Thanks is advance.
Your line of code refers to one specific named shape (CTSnext). You replace that by shape number if you have only one in the sheet Shapes(0).
If you have several shapes on the current sheet and you want the macro to run on all shape, you can use a loop like
for each shp in ActiveSheet.Shapes
shp.Fill.ForeColor.SchemeColor = Black
Next shp
Following on from iDevlop's answer to loop through each Worksheet as well as each shape.
Dim wks as Worksheet
For Each wks in Me.Worksheets
For Each shp in wks.Shapes
shp.Fill.ForeColor.SchemeColor = Black
Next shp
Next wks

VBA in Word 2010: how do I fix the relative position of a TextBox?

Writing VBA in Microsoft Word 2010 (no-one's favourite job). I'm trying to fix the vertical position of a textbox to a location in the document, so that as text is added before, the text box retains its relative position (i.e. moves down if text is inserted before the location it's linked to).
My code is
ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 372, 400, 120, 120).Select
With Selection
.ShapeRange.TextFrame.TextRange.Select
.Collapse
.TypeText Text:="Text box placement test"
With .ShapeRange
.Select
.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
.RelativeVerticalPosition = wdRelativeVerticalPositionPage
...
The key I think is this last property, RelativeVerticalPosition.
I have tried changing it to wdRelativeVerticalPositionLine: this works for the first paragraph (i.e. the text box is fixed to the position in text, so if text is added before it moves correctly): however, for the second and later paragraphs the vertical position is completely wrong.
I have tried changing the LockAnchor property but that makes no change.
The frustration is that this can be done manually (Page layout, Arrange group, Wrap text, more layout options, position, move object with text = checked) but Word won't let me record a macro where I change a textbox's properties, so I can't find the combination of settings to make it work.
Any suggestions? Or is this just one of the consequences of using 20+ year old code?
First, you've got to set RelativeVerticalPosition as well.
Second, be careful with using .ShapeRange. Incorrent usage of .ShapeRange may be the problem in some cases.
That's a small snippet from my code. Just tested it with Word 2010.
Dim oShp As Shape
Set oShp = Selection.ShapeRange(1)
oShp.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
oShp.RelativeVerticalPosition = wdRelativeVerticalPositionLine
So if your "move object with text" is unchecked, it'll become checked after running the code.