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 ...
Related
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.
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.
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?
This may sound dumb but can someone explain the reason why I need to include this extra code in vb.net that I don't need in VBA when changing the color of a cell.
I wrote a program that creates and saves an excel file using the microsoft.office.interop reference. Here is my code:
Public Class Export_Excel_Class
Public excelapp As Microsoft.Office.Interop.Excel.Application
Public excelbook As Microsoft.Office.Interop.Excel.Workbook
Public excelsheet As Microsoft.Office.Interop.Excel.Worksheet
Public Sub newExcel()
excelapp = New Microsoft.Office.Interop.Excel.Application
excelbook = excelapp.Workbooks.Add()
excelsheet = excelbook.Sheets("sheet1")
excelsheet.Range("A1").Select()
excelsheet.Range("a1").AddComment("hello")
excelsheet.Range("a1:B10").Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow)
excelbook.SaveAs("C:\Users\DatOneBull\Desktop\Excel_Test")
excelapp.Quit()
End Sub
On the line that sets the color I had originally had tried excelsheet.Range("a1:B10").Interior.Color =
and tried to set a color but the color proptery is read only. I found the solution online but there was no explanation. Can someone tell me what this section of code means and why it is able to set the color value of th range?
System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow)
VB.NET stores colors differently to VBA. The ToOle function converts the VB.NET color to the equivalent VBA version. This is an example of marshalling.
(If you are interested, the "OLE" in "ToOle" stands for "Object Linking and Embedding" which is an older way for windows programs to communicate with each other)
In VBA, you would set the color something like this:
Range("A1").Interior.Color = Excel.XlRgbColor.rgbYellow
' Or
Range("A2").Interior.Color = VBA.RGB(255, 255, 0)
Note that I purposely fully qualified the VBA code. This is valid VBA code, but in a style you rarely see.
In VB.Net you can do it like:
ws.Range("A1").Interior.Color = Excel.XlRgbColor.rgbYellow
ws.Range("A2").Interior.Color = Microsoft.VisualBasic.Information.RGB(255, 255, 0)
essentially the same code. Or you can do it like you are currently doing.
The net effect is the same; an integer value that represents color is being assigned to the Interior.Color property. Either you use a function to compute that value, or you use a constant. The various .Net functions are equivalents to the VBA.RGB function. VBA knows nothing about colors defined as a System.Drawing.Color, hence a conversion function is necessary.
I am hoping to set all of the togglebuttons in a powerpoint to "false" (unpressed) upon starting the program. Any ideas why this sort of code is working?
Sub Start()
ActivePresentation.Slides(4).ToggleButton1.Value = 0
ActivePresentation.Slides(4).ToggleButton4.Value = 0
ActivePresentation.SlideShowWindow.View.Next
End Sub
Thank you!
The syntax
ActivePresentation.Slides(4).ToggleButton1.Value = 0
is correct; I've just tested it and it works.
Are you actually calling the Sub at any point though? Simply naming it Start() is not enough.
Related question re. running code on startup, could be helpful.
I had an issue calling this
ActivePresentation.Slides(4).ToggleButton1.Value = 0
Getting the error Method or data member not found which im assuming is a scope issue. The VBA editor doesnt autocomplete .ToggleButton1 which supports the error i was getting.
So, it appears im referencing the object incorrectly.
To correct this I have done the following.
Public Sub ToggleThisButton()
' This is code on Slide 1 referencing control on slide 4
ActivePresentation.Slides(4).Shapes("ToggleButton1").OLEFormat.Object.Value = 1
End Sub
Excerpt from MSDN
Use the OLEFormat property for a shape, inline shape, or field to
return the OLEFormat object. The following example displays the class
type for the first shape on the active document.
Use the Object
property to return an object that represents an ActiveX control or OLE
object. With this object, you can use the properties and methods of
the container application or the ActiveX control.