Excel VBA: Positioning and Alignment of Shapes - vba

I am trying to make a printable report from excel sheet with some shapes such as charts and pictures. I am able to put the charts on the sheet using the below code:
oSheetReport.Range("A51").Select()
Dim oChart1 As Excel.Shape
oChart1 = oSheetReport.Shapes.AddChart()
oChart1.Chart.ChartType = Excel.XlChartType.xlLine
oChart1.Chart.SetSourceData(Source:=oSheet.UsedRange)
oSheetReport.Range("A70").Select()
oChart1 = oSheetReport.Shapes.AddChart()
oChart1.Chart.ChartType = Excel.XlChartType.xlColumnStacked
oChart1.Chart.SetSourceData(Source:=oSheet.UsedRange)
oSheetReport.Range("A100").Select()
oChart1 = oSheetReport.Shapes.AddChart()
oChart1.Chart.ChartType = Excel.XlChartType.xlColumnStacked100
oChart1.Chart.SetSourceData(Source:=oSheet.UsedRange)
But positioning and alignment is failing which is making my reports look ugly. Any better way of achieving this?

To add, position and align in one step you can use the following:
Set oChart1 = ActiveSheet.ChartObjects.Add _
(Left:=250, Width:=375, Top:=75, Height:=225)
Left is the left alignment and Top is the top alignment. Width and Height - well, you can figure that out!
More info at http://peltiertech.com/Excel/ChartsHowTo/QuickChartVBA.html

Related

Align a text frame when creating a ppt from access vba

I'm trying to align a text frame when creating a ppt from access. Alignment control is available from excel vba but I'm struggling to find this in access.
Here is an example of the creation of a textbox in powerpoint from access vba. I have control of the font size and such but I cannot locate the alignment for the textbox.
' Program data
Dim tb_ProgramData As Shape
Set tb_ProgramData = pptCurrentSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=68.399, Top:=51.12, Width:=187.2, Height:=18.72)
tb_ProgramData.TextFrame.TextRange.Text = rs.Fields(27) + " Program: " + rs.Fields(29)
tb_ProgramData.TextFrame.TextRange.Font.Size = 9
tb_ProgramData.TextFrame.TextRange.Font.Bold = msoTrue
The only thing close is a paragraph alignment but it has no effect...
tb_Title.TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignCenter
Any help would be appreciated... regards,
Good grief.. I found it under the text effects.
tb_SystemClass.TextEffect.Alignment = msoTextEffectAlignmentRight
and if you wish to select the textbox from the list of shapes then...
pptCurrentSlide.Shapes.Range("tb_SystemClass").TextEffect.Alignment = msoTextEffectAlignmentRight

How to maintain the size of fonts in anchored MSGraph Chart object

I have a anchored MSGraph Chart object on my form to stretch left and bottom. It stretches well but the fonts also stretch. How do I maintain the same font size for all the Data labels but still be able to view the graph in full size on stretched or zoomed mode? My aim is to keep font sizes for all Data labels and Axis to 11pt. The original font size for the Data Labels and Axis is 8pt
Edit:
I have notice that when I select the Zoom or Stretch features in the Chart's properties sheet, the chart stretches and the so does the fonts. This does not happen if I select Clip
Is it possible to make the chart stretch whilst maintaining the font sizes to 11pt? I am asking this because when I looked at the chart on a 4:3 screen today, some fonts were barely visible. Please see the image of my current graph and together with its properties settings below:
I have inserted a Stacked Layout with two cells onto the the form, then inserted the chart object in the first cell and left the second cell empty.
A good thing is that an empty cell can be anchored to Strech when the form resizes to suit the screen's aspect ratio.
I then adjusted the height of the blank cell and anchored both the object and the blank cell to Sctrech Down and Across.
On the chart properties, the chart Size Mode must be set to Strecth
Then used the below code on the form open event:
Note: This code requires the use of Microsoft Graph Object Library which should be added using the Tools -> References feature on the code editor.
I am using Microsoft Graph 16.0 Object Library.
I shall be exploring how to use latebinding inorder to avoid this step or perhalps somone can comment.
Dim myChart As Graph.Chart
Dim myChartSeries As Graph.Series
Dim mySeriesDataLabel As Graph.DataLabel
Set myChart = Me.myGraph.Object
For Each myChartSeries In myChart.SeriesCollection
For Each mySeriesDataLabel In myChartSeries.DataLabels
mySeriesDataLabel.Font.Name = "Times New Roman"
mySeriesDataLabel.Font.FontStyle = "Normal"
mySeriesDataLabel.Font.Size = 8
Next mySeriesDataLabel
Next myChartSeries
With Me.myGraph.Axes(1).TickLabels.Font
.Name = "Times New Romans"
.FontStyle = "Normal"
.Size = 8
End With
With Me.myGraph.Axes(2).TickLabels.Font
.Name = "Times New Romans"
.FontStyle = "Normal"
.Size = 8
End With
Without Anchoring:
After Anchoring:

How to make label text to grow on left while making the right side fixed in vb.net?

Label text to grow on left side while making the right side fixed in vb.net.Image is attached for better understanding
Label text to grow on left side:-
What you need is to set your text label to be right-justified. Showing the part of your code related to creating and populating the label (the one displaying the total amount) will help.
You could set Autosize = False and choose TextAllign = *right. The actual label wouldn't grow, so you'd need to set a 'sufficient' width.
Alternative is to do it via code:
'find the current right alignment position
Dim rightAlign As Integer = Label1.Left + Label1.Width
'set the text (assumes AutoSize is set to True)
Label1.Text = value
'adjust position so the right hand point is in the same position as before
Label1.Left = rightAlign - Label1.Width
Depending on how your form is laid out, you can dock the label to the right and set:
Label.Dock = DockStyle.Right
Label.AutoSize = True
This will make your label expand to the left. Docking to the right will put your label at the top of the form unless you have an appropriate layout.

Word VSTO - Change the absolute positions' type?

I use the following VB.NET (VSTO) code to add a shape in MS-Word,
Dim app As Word.Application = Globals.ThisAddIn.Application
Dim doc As Word.Document = app.ActiveDocument
Dim left As Single = CSng(Convert.ToDouble(app.Selection.Information(Word.WdInformation.wdHorizontalPositionRelativeToPage)))
Dim top As Single = CSng(Convert.ToDouble(app.Selection.Information(Word.WdInformation.wdVerticalPositionRelativeToPage)))
Dim shape As Word.Shape = doc.Shapes.AddShape(1, left, top, 225.1F, 224.5F)
shape.Fill.BackColor.RGB = ColorTranslator.ToOle(Color.Transparent)
shape.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
shape.Fill.Transparency = 0.0F
shape.Line.Transparency = 0.0F
shape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
What this code does is, it adds a rectangle shape at cursor point and makes it transparent (both background and line).
Now I like to change the absolute positions' type. To explain further, when you select the rectangle shape, then if you select the Ribbon tab Format > Position > More Layout Options... as shown in the image below,
It will open the following dialog,
In the above dialog I like to change Column and Paragraph marked by the red rectangles into the type Margin. How to do this by code?
Word provides a Macro recorder. You may use it to get the code generated for you in the background. Thus, you will find what properties and methods exactly should be used to get the job done. See Record or run a macro for more information.
The solution to this was solved in the link below,
https://social.msdn.microsoft.com/Forums/vstudio/en-US/e69584d7-24fe-4396-9a82-26b7dae02584/word-vsto-change-the-absolute-positions-type?forum=vsto

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