Renaming chart in VBA - vba

I added a chart using this piece of code:
ActiveSheet.Shapes.AddChart2(419, xlFunnel).Select
Now, I wish I could select this graph, so that I can rename it.
PS: What does it mean AddChart2? Is this standard code or not?

Here you go!
Dim MyChart As Shape
Set MyChart = ActiveSheet.Shapes.AddChart2(419, xlFunnel)
MyChart.Chart.SetSourceData Source:=Range("Sheet1!$F$2:$F$4")
MyChart.Name = "My new chart"
Regarding the AddChart vs AddChart2 question - read more here What does the number in the AddChart2 VBA macro represents?

Related

How to get a container header id to a variable and passed to vba to write a new header?

I have created this script to generate a container that I want to use it to generate containers It works fine but I also need to be able to give the containers a custom header. As you can see I tried to capture the shape id in a variable so I could use the variable to get the shape Id for the container. Nevertheless, I cannot get the shape id or assign one statically I also found out that the container has more than one shape ID. How do I Identify the ID for the header portion. I also need to be able to drop shapes in the container. I followed Microsoft instructions and tried using
vsoContainerShape.ContainerProperties.AddMember vsoShape,
visMemberAddExpandContainer
However that doesn’t work.
Sub Add_Container()
Dim DiagramServices As Integer
DiagramServices = ActiveDocument.DiagramServicesEnabled
ActiveDocument.DiagramServicesEnabled = visServiceVersion140 +
visServiceVersion150
Dim visapp As Visio.Application
Dim vlan30 As Visio.Document
Dim node As Visio.Shape
Dim vlan30id As Integer
Application.Documents.OpenEx(Application.GetBuiltInStencilFile(visBuiltInStencilContainers, visMSUS), visOpenHidden)
Application.Windows.ItemEx("container.vsdm").Activate 'need to activate
Application.ActiveWindow.Page.DropContainer vlan30.Masters.ItemU("Classic"), Nothing
vlan30id = vlan30.ID
Debug.Print vlan30id
Dim v30chars As Visio.Characters
Set v30chars = Application.ActiveWindow.Page.Shapes.ItemFromID(vlan30id).Characters
v30chars.Begin = 0
v30chars.End = 7
v30chars.Text = "Vlan_30"
vlan30.Close
ActiveWindow.DeselectAll
'Restore diagram services
ActiveDocument.DiagramServicesEnabled = DiagramServices
End Sub
I need to be able to get the shape id for the heading of the containers and stored in a variable so I can use the variable for passing the argument in the ItemFromID. Thanks
First things first: your exact question was already answered her: http://visguy.com/vgforum/index.php?topic=6787.0
I packaged the whole code into a function, calling the function will drop a classic container on the page you passed as argument and fill the caption with the passed caption argument. The return value is the dropped Container, bottom function shows how to use the function and add shapes to the container.
'#Folder("ExampleDropContainer")
Option Explicit
Public Function DropContainerWithCaption(pg As Visio.Page, caption As String) As Visio.Shape
Dim vsPg As Visio.Page
Set vsPg = pg
Dim vsStencilName As String
vsStencilName = Application.GetBuiltInStencilFile(visBuiltInStencilContainers, visMSUS)
Dim vsStencil As Visio.Document
Set vsStencil = Application.Documents.OpenEx(vsStencilName, visOpenHidden)
Dim vsMas As Visio.Master
Set vsMas = vsStencil.Masters.ItemU("Classic")
'If you already had the shapes you want to have inside the continer you can replace "Nothing" with them.
Dim droppedContainer As Visio.Shape
'Set droppedContainer = vsPg.DropContainer(vsMas, Nothing)
'Using page.Drop circumvents some issues when a shape already occupies the space where the shape is to be dropped.
Set droppedContainer = vsPg.Drop(vsMas, 0, 0)
droppedContainer.Text = caption
Set DropContainerWithCaption = droppedContainer
End Function
Sub TestExample()
Dim newContainer As Visio.Shape
Set newContainer = DropContainerWithCaption(ActivePage, "Bananas")
'Example on how to add a Shape to the container, someShape is a visio.shape object
'newContainer.ContainerProperties.AddMember someShape
End Sub
You seem to have posted a lot of questions concerning the same or similar problems lately, most of which are quite basic once you get to know VBA. You should read up a bit, especially on the concept of return values of functions. Also be aware that most questions regarding programming in Visio are exactly the same in VBA for Excel, only the interaction with the document/workbook is sometime different. Many answers can be found when searching properly
Some good links are:
How to avoid Select Probably the most read article concerning VBA in Stackoverflow
ExcelMacroMastery Good Resource, a lot on the basics of programming in VBA
Chip Pearson's Website Lots of great and deep information
RubberduckVBA Blog Fantastic resource with many examples on how to bring VBA-Code up to modern standards. He's also active here on SO
The first two links are a must-read IMHO, the other two are great if you want to dive deeper into VBA.

Modify Treemap-Chart in PowerPoint

I have a chart that I modify with VBA by changing the values from an excel-workbook. Everything works fine, but I want to use the treemap-chart where suddenly the code stops working.
The code works perfectly fine with the other charts in PowerPoint but when I change it to TreeMap the Code stops executing and the chart doesn't change it values.
Option Explicit
Sub Chart_TT()
With ActivePresentation.Slides(2).Shapes("Chart 1").Chart.ChartData
.Activate
.Workbook.Sheets(2).Range("C2").Value = .Workbook.Sheets(2).Range("I9").Value
.Workbook.Sheets(2).Range("D2").Value = .Workbook.Sheets(2).Range("J9").Value
.Workbook.Sheets(2).Range("E2").Value = .Workbook.Sheets(2).Range("K9").Value
.Workbook.Sheets(2).Range("F2").Value = .Workbook.Sheets(2).Range("L9").Value
.Workbook.Close SaveChanges:=True
End With
End Sub
The error code which is shown is:
"Error -2147467259 (80004005): The method "workbook" for the object "ChartData" failed.
What do I have to change that the code also works with the TreeMap chart?
Thanks!
Sorry, the treemap chart style is fairly new and has not been included in the VBA object model.

VBA replace logo in Excel file

I have a little strange question. I used to have few reports worked upon daily.
All these are in Excel and had a logo of the company in all the sheets of each file.
However, now the company name is changed and hence a new logo needs to be replaced in place of the existing. Wanted to check if this replacement can be done with VBA.
I tried with the application.shapes method. But, was confused to proceed further.
Try this....
Sub ChangePicture(strNewPath As String)
Dim oOld As Picture
Dim oNew As Picture
Set oOld = ActiveSheet.Pictures(1)
Set oNew = ActiveSheet.Pictures.Insert(strNewPath)
oNew.Left = oOld.Left
oNew.Top = oOld.Top
oNew.Width = oOld.Width
oNew.Height = oOld.Height
oOld.Delete
End Sub

Why activate chart first?

Don't know if it is the best title for my question... :)
I have a chart in excel. I want to use SetElement.
If i use this code it gets an error (Object doesn't support this property or method):
Dim Graphics As Worksheet
Set Graphics = Worksheets("Graph")
Graphics.ChartObjects("Chart 1").SetElement (msoElementChartTitleNone)
If i use this code it works well:
Dim Graphics As Worksheet
Set Graphics = Worksheets("Graph")
Graphics.ChartObjects("Chart 1").Activate
ActiveChart.SetElement (msoElementChartTitleNone)
Can someone explain why the first option isn't working?
It is still the same command on the same object?
It's the same command (method) on a different object (ChartObject vs Chart).
Try:
Graphics.ChartObjects("Chart 1").Chart.SetElement ...

How do I center a paragraph in Word using vb.net?

I am using Visual Studio 2015 and coding in vb.net and importing Microsoft.Office.Interop.Word . I am using the following code to create a one page Word document with only two lines. How can I center, both vertically and horizontally these two lines? Also, is there a way to put both lines, with a line break in between, in one paragraph rather than using two? I am very new to this type of programming so please be specific. Thanks.
Private Sub CreateTitlePage2()
Dim wdApp As Microsoft.Office.Interop.Word.Application = New Microsoft.Office.Interop.Word.Application
Dim wdDoc As Microsoft.Office.Interop.Word.Document = New Microsoft.Office.Interop.Word.Document
Dim wdPara1 As Microsoft.Office.Interop.Word.Paragraph
Dim wdPara2 As Microsoft.Office.Interop.Word.Paragraph
wdDoc.Application.Visible = False
wdPara1 = wdDoc.Content.Paragraphs.Add
wdPara1.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter
wdPara1.Range.Font.Bold = True
wdPara1.Range.Text = "BINDER DOCUMENT"
wdPara1.Range.InsertParagraphAfter()
wdPara2 = wdDoc.Content.Paragraphs.Add
wdPara2.Format.SpaceBefore = WdVerticalAlignment.wdAlignVerticalCenter
wdPara2.Range.Font.Bold = True
wdPara2.Range.Text = "Created: " + formattedDate2
wdPara2.Range.InsertParagraphAfter()
wdDoc.SaveAs(binderNameDoc)
wdDoc.Close()
wdApp.Quit()
End Sub
#Ross: It would help if you'd describe HOW it's "not working". However...
WdVerticalAlignment is not valid applied to a paragraph object, I'm surprised you're not getting a compiler error. See https://msdn.microsoft.com/en-us/library/aa224305(v=office.11).aspx.
If you want to center something vertically on the page then it must be done via the PageSetup object and then it will apply to the entire SECTION. See https://msdn.microsoft.com/en-us/library/office/ff838676.aspx?f=255&MSPPError=-2147217396
If you document is really only the one page, as you say, then you don't need to worry about the SECTION part as the document will have only the one.
RE Line break: Insert ANSI 11 character (vbVerticalTab) for a line break (what you get when pressing Shift+Enter in the Word application).