Microsoft.Office.Interop.Excel Line Chart Color And Bar Chart Color Alpha - vb.net

I am using the following code to format properties of a chart created by Microsoft.Office.Interop.Excel.
xlChart.SeriesCollection(1).ChartType = XlChartType.xlColumnClustered
xlChart.SeriesCollection(2).ChartType = XlChartType.xlLine
xlChart.SeriesCollection(1).Interior.Color = Color.FromArgb(255, Color.DeepSkyBlue)
xlChart.SeriesCollection(2).Interior.Color = Color.FromArgb(255, Color.DarkOrange)
I have two problems. For the xlColumnClustered chart, the color is fine but the Alpha value does not have any affect (whether being 0 or 126 or 255). For the xlLine even the color doesn't get set. I know probably for xlLine I shouldn't use .Interior.Color but I don't know what I am supposed to use. Any help will be appreciated.

I don't think you can use a transparent color directly. What you can do is set the transparency parameter of the series like this:
Chart.SeriesCollection(1).Format.Fill.Transparency = 0.5
For line you should use:
Chart.SeriesCollection(1).Format.Line.Forecolor.RGB = RGB(255, 0, 0)
I wrote a blog post about how to find out which objects and properties should you use to achieve your result. It seems it might help you find with other difficulties in locating the right objects to use-

Related

Possible to apply a Shadow to an entire Group with VBA (but not to GroupItems)?

I'm working on a PowerPoint VBA script to apply some default styles to selected objects, and I'm running into some problems with grouped objects. Essentially, if I apply ShadowFormat adjustments to a Shape group, the shadow is applied to the items within the group rather than to the group itself.
In the example below, the white box, blue box, and blue circle are all grouped. Using the GUI to set a shadow on the group results in just the bounding shape to receive the shadow (i.e., the blue objects don't get a shadow). When applying the shadow via a VB script, however, the shadow is applied to all three objects individually, which is not the desired behavior. Inspecting the group with the Format Palette verifies that no shadow is applied to the group directly.
Image: Shadows on groups problem (Sorry for the link, I don't have enough rep to post images yet...)
Here's a code snippet. In this example, the Group in question is the first item on the slide:
Set myDocument = ActivePresentation.Slides(1)
With myDocument.Shapes(1).Shadow
.Visible = True
.ForeColor.RGB = RGB(0, 0, 0)
.Blur = 12
.Transparency = 0.4
.OffsetX = 0
.OffsetY = 3
.Obscured = msoTrue
End With
I expect the shadow to only be applied to the group, as it is when you use the GUI to apply a shadow to a group. Instead, all of the group's sub-shapes have their shadows set, and the group itself does not receive a shadow.
Shadow is a property of a shape. A group is not a shape. So shadows can only be applied to the members of the group, not the group itself.
To get this effect, copy all shapes, make an array out of them, merge them using the MergeShapes method, send the merged shape to the back and apply a shadow to it. Here's how to use MergeShapes, thanks to Shyam Pillai for this:
Dim shp1 As Shape
Dim shp2 As Shape
Set shp1 = ActivePresentation.Slides(1).Shapes.AddShape(msoShapeOval, 100, 100, 50, 50)
Set shp2 = ActivePresentation.Slides(1).Shapes.AddShape(msoShapePie, 100, 100, 50, 50)
Call ActiveWindow.Selection.SlideRange(1).Shapes.Range(Array(shp1.ZOrderPosition, shp2.ZOrderPosition)).MergeShapes(msoMergeCombine)

Editing Word Styles in VB.Net

I am looking to edit Word Styles on a document using VB.Net Code.
This is so that any document created by my program anywhere will have the correct styles.
I have found code that allows me to edit font, colour, size, etc. but I am stuck on two parts.
Some of the styles have a border and when look at the border in Word it gives the following breakdown
Border:
Bottom: (Single solid line, Accent 1, 0.5 pt Line width, From text: 4 pt Border spacing:)
I've got everything apart from the distance from text.
https://msdn.microsoft.com/en-us/vba/word-vba/articles/borders-distancefrombottom-property-word says that there should be a property for that but when I attempt to use it in my code I get ".DistanceFromBottom is not a member of Border"
Some of the styles also have indents and I cannot work out how to apply these. Word gives the breakdown as such :
Indent:
Left: 1.27 cm
Right: 1.65 cm, Space
Before: 10 pt
After: 14 pt
I cannot find any library or coding that allows me to put in an indent as part of a style but you can when making the style in Word. It seems odd that Microsoft would allow you to manipulate almost everything else about a style except indents.
So far the code I have for one on the styles is :
With doc.Styles(Word.WdBuiltinStyle.wdStyleIntenseQuote)
.Font.Bold = True
.Font.Italic = True
.Font.Size = 12
.Font.Name = "Cambria"
.Font.Color = RGB(79, 129, 189)
With .Borders(WdBorderType.wdBorderBottom)
.LineStyle = WdLineStyle.wdLineStyleSingle
.Color = RGB(79, 129, 189)
.LineWidth = WdLineWidth.wdLineWidth050pt
End With
End With
If anyone needs anything clarifying then please ask away.
As to the borders: if you look carefully at the link you provide you'll see it's specific to Page borders, not borders around text. This kind of border cannot be part of a style.
If you're creating a style to format entire paragraphs (which is the case when you specify Indent) you probably shouldn't be using a Linked style. Better a Paragraph or ParagarphOnly style. Linked style can be extremely confusing.
Indents are part of the Paragraph formatting. Before and After do not apply to indents; they're the vertical spacing between paragraphs.
There are three kinds of indent: LeftIndent, RightIndent and FirstLineIndent. Left affects all lines from the left margin; right all lines from the right margin; FirstLine sets how the first line differs from the standard setting (LeftIndent) relative to the left margin.
For an indent such as one sees in many books (the first line is further to the right):
Dim doc As Word.Document = ActiveDocument
Dim styl as Word.Style = doc.styles.Add("IndentStyle", wdStyleTypeParagraphOnly)
styl.ParagraphFormat.FirstLineIndent = 7
For a hanging indent (all lines except the first are indented - used most often with bullets or numbering):
Dim doc As Word.Document = ActiveDocument
Dim styl as Word.Style = doc.styles.Add("IndentStyle", wdStyleTypeParagraphOnly)
styl.ParagraphFormat.LeftIndent = 7
styl.ParagraphFormat.FirstLineIndent = -7 'Puts it at 0 relative to margin
Tip for figuring out what objects, properties and methods you need: record a macro in Word while doing something (creating or modifying a style) then look at the result.
Easier approach is to limit the users of your program to using particular templates only. You can then define (and limit) the styles in use in those templates.
Much easier to define a style in a template than in VBA.
Where an external document is used, you can apply your own template to it. If you need further cleaning up/checking, you can use the advice Cindy Meister provided.

Add a gradient to a shape/line

I am attempting to add a gradient to a line shape in Excel using VBA. This feature is available in the Line Color section under the Format Shape option. Despite this feature existing under the Format Shape option, I am unable to reproduce the functionality in VBA. My code is:
With ActiveSheet.Shapes("Straight Connector 4")
.Line.ForeColor.RGB = RGB(193, 193, 193)
.Line.Transparency = 0.25
.Line.Visible = msoTrue
.Line.ForeColor.SchemeColor = 24
.Line.BackColor.SchemeColor = 34
.Line.GradientStops.Insert RGB(255, 0, 0), 0.25 ' Creates error
.Line.Gradient.ColorStops.Add (1) ' Creates error
End With
I know you can easily add a gradient to the shape fill but all search results are returning nothing when wanting to add a gradient to a shape line. Any ideas are more than welcome.
As far as I know, it is not possible. You can set a gradient for a shape's fill through VBA, but you can't do it for a line.
You can either create a thin shape with gradient fill and no border, or you will have to use something outside VBA. (Eg. VB.NET + OpenXLM SDK.)

DevExpress SpreadSheetControl Chart set line color /style

DevExpress website is unable to tell me. How can I influence the line color, thickness, etc. of created graphs?
Dim chart As Chart = myworb.Worksheets(1).Charts.Add(Charts.ChartType.ScatterLine)
chart.TopLeftCell = myworb.Worksheets(1).Cells("A83")
chart.BottomRightCell = myworb.Worksheets(1).Cells("F112")
chart.PlotArea.Fill.SetNoFill()
chart.Series.Add(myworb.Worksheets(1)("A1:A71"), myworb.Worksheets(1)("B1:B71"))
chart.Series.Add(myworb.Worksheets(1)("D1:D81"), myworb.Worksheets(1)("E1:E81"))
chart.Series.Add(myworb.Worksheets(1)("G1:G81"), myworb.Worksheets(1)("H1:H81"))
chart.Series.Add(myworb.Worksheets(1)("J1:J71"), myworb.Worksheets(1)("K1:K71"))
chart.Series.Add(myworb.Worksheets(1)("M1:M66"), myworb.Worksheets(1)("N1:N66"))
chart.Title.Visible = False
Dim axisX = chart.PrimaryAxes(0)
axisX.MajorTickMarks = AxisTickMarks.None
axisX.Scaling.AutoMax = False
axisX.Scaling.AutoMin = False
axisX.Scaling.Max = maxX1
axisX.Scaling.Min = minX1
Dim axisY = chart.PrimaryAxes(1)
axisY.MajorTickMarks = AxisTickMarks.None
axisY.Scaling.AutoMax = False
axisY.Scaling.AutoMin = False
axisY.Scaling.Max = maxY1
axisY.Scaling.Min = minY1
Normally I would just create an Excelfile as a template, load it into the control an just fill the values the chart is using as a series. But as I have to scale it manually because Excel autoscale is NOT WORKING, I have to create the whole chart from scratch. I am unable to find information about how I can set colors etc in the chart programatically. Any help is greatly apreciated.
You can set the color and thickness by using ShapeFormat.Outline property of your series objects. Call to ShapeOutlineFill.SetSolidFill method to set the color of your line and use the ShapeOutline.Width property to set the line width.
Here is example:
chart.Series(5).Outline.SetSolidFill(Color.Magenta)
chart.Series(5).Outline.Width = 20
The "lines" are the chart series and can be accessed in the Series property. From there you can change various things about the series including the color. Alternatively you can custom draw series yourself using the CustomDrawSeries event.

Vba - Set transparent color or direct hex value of label backcolor?

I want to insert a label into a PowerPoint presentation. But I don't want any background on there, or, have the background color be the same as the what is underneath.
I've found that 082F68 is the hex code I want. The RGB code is: 8, 47, 104
This color is supposed to be bluish, but when I insert it, it just gets brown.
I really don't want that. I also tried setting label.backcolor to Color.Transparent. But that isn't recognized. Neither is System.Drawing.Color.Transparent either. It just says it needs an object reference.
But really, isn't it possible to use direct hex values for label backgrounds?
(super late response, but in case others have this issue)
This will create a label on slide 1 in the upper-left hand corner. On my system, I get the bluish background color you are talking about.
ActivePresentation.Slides(1).Shapes.AddLabel(Orientation:=msoTextOrientationHorizontal, _
Left:=0, Top:=0, Width:=50, Height:=50).name = "TestLabel"
Dim sh As Shape
Set sh = ActivePresentation.Slides(1).Shapes("TestLabel")
sh.TextFrame.TextRange.Text = "Hello"
sh.Fill.BackColor.RGB = RGB(8, 47, 104)
You can also set the fill transparency to 100% (fill will be see-through):
sh.Fill.Transparency = 1#
I'm not sure what you're using as a "placeholder", but any Shape object will have an ID:
MsgBox "Label ID = " + CStr(sh.Id)
but it is probably easier to refer to it by name as I do above. The ID is a numerical value and isn't the same as the shape index, making it harder to reference the shape by its ID.
Above I programmatically assign the name, but you can also name the shape yourself: Home -> Arrange -> Selection Pane. In the selection pane you can click on the names of all the shapes on the slide to edit them. You can now refer to these shape names in code.
Try *.BackgroundColor = -1 'Transparent