How to instantiate a Graph Chart? - vba

I am currently trying to create a chart, e.g. a piechart through word vba.
The problem is that the machine(s) where this word vba will be run do not have excel available, only word and ms graph.
I have tried to create a chart with ms graph, but I am not able to make it work.
As from the code below, I can create charts with inlineshapes and chart and I get a nice bar chart presented in the word window.
I am not able to instantiate the chart element for Ms Graph. I found a french guide that shows how it should be done, but I am not able to get the "me.graphique1"-part to make sense.
Inlineshapes
Dim vlChart As Chart
Set vlChart = ActiveDocument.InlineShapes.AddChart2.Chart
Graph
Dim vlChart As Graph.Chart
Set vlChart = Me.Graphique1.Object.Application.Chart
I would expect to be able to create a graph object, but I don't know what I am doing.

No guarantees that this will still work, since MS Graph has long been deprecated. The following code sample demonstrates how to insert an MS Graph object and access its data sheet.
This code requires a reference to the MS Graph object library in VBA Tools/References. I highly recommend you use it, at least while writing the code so that you have Intellisense.
Dim grph as Graph.Chart
Dim grphData as Graph.DataSheet
Dim grphApp as Graph.Application
Dim doc as Word.Document
Dim oleF as Word.OLEFormat
Dim rng as Word.Range
Set doc = ActiveDocument
Set rng = doc.Content
Set oleF = doc.InlineShapes.AddOLEObject(ClassType:="MSGraph.Chart.8", Range:=rng).OLEFormat
oleF.DoVerb
Set grphChart = oleF.Object
Set grphApp = grphChart.Application
Set grphData = grphApp.DataSheet
grphData.Cells.Clear
'Now start entering the data and formatting the chart

Related

Word macro to set image layout and size

I'm trying to record a macro that will set the size of a pasted image to 6.5 x 4 cms and the image layout 'in front of text'. I usually set this by right clicking on the image and setting the picture properties but this isn't available when recording the macro. Using Shift-F10 does bring up the menu, but the option to set the image layout properties is greyed out.
Please help!
Thanks
Yes, that's interesting about the macro recorder...
If you need to do something like this in the future it will help you to know that an object with any kind of "text wrap" formatting (that "floats") is a Shape. An object that behaves like a character is an InlineShape. And you can convert between the two using ConvertToInlineShape / ConvertToShape. For future things of this nature that should give you a starting point :-)
The following code sample uses ConvertToShape so that text wrap formatting can be applied. (The conversion is an extra step involved that you don't have to do in the UI - Word does it for you.)
Before this happens the code changes the size, but it could also be the other way around, changing the size on the Shape object.
What else is happening in the code: When a picture is pasted inline it's not selected. So this code figures out how many pictures (InlineShapes) are already in the document up to the selection. After the paste the code then picks up the existing number of pictures up to that point, plus one, to get the picture that was just pasted.
The code uses CentimetersToPoints to convert the number of centimeters wanted to the Points measurement, since that's what Word uses to size graphical objects (and lots of other things).
How did I know to use wdWrapFront: When shp.WrapFormat.Type = is typed the VBA Editor will automatically show a list of valid entries for text wrap formatting. Similarly, when shp. is typed a list of valid properties for a Shape will appear, and so on. (This is called IntelliSense and is a wonderful help!)
Sub PasteAndSelectPicture()
Dim ils As Word.InlineShape
Dim shp As Word.Shape
Dim lNrIls As Long
Dim rngDoc As Word.Range
Dim rngSel As Word.Range
Set rngDoc = ActiveDocument.content
Set rngSel = Selection.Range
rngDoc.End = rngSel.End + 1
lNrIls = rngDoc.InlineShapes.Count
rngSel.Paste
' Debug.Print rngDoc.InlineShapes.Count, lNrIls
Set ils = rngDoc.InlineShapes(lNrIls + 1)
ils.width = CentimetersToPoints(6.5)
ils.height = CentimetersToPoints(4)
Set shp = ils.ConvertToShape
shp.WrapFormat.Type = wdWrapFront
End SUb

Excel vba to modify Catia v5 model properties

I am a novice when it comes to CATIA, but I have a fair amount of experience with VBA in excel. I am trying to develop an excel macro that goes through the all the parts in a catia assembly, renames them by splicing the text where i want, and then reorder them (alpha-numeric ascending order). I believe I can write the algorithm for the actual splicing, renaming, reordering bit. What I am struggling with is actually operating Catia using excel. There is not a lot of information on the internet. I have ticked all the boxes in the references section that start with CATIA. I have written this so far:
Dim CATIA as object
Set CATIA = Getobject(,"CATIA.Application")
Dim oMyDoc as Document
Dim oCurrentProd as Product 'I assume the individual parts within CATIA are_
referenced as products?
Set oMyDoc = CATIA.ActiveDocument
If I try and run just the above code, I get an error saying 'Class doesn't support Automation'. Which means my basics are wrong. Would appreciate help on this and any other information that would enable me to complete my task. Thanks.
When running a code from CATIA in VBA, some objects gets an error when they are properly typed. Try declaring those objects without a type or as a variant.
Dim CATIA as 'object
Set CATIA = Getobject(,"CATIA.Application")
Dim oMyDoc as 'Document
Dim oCurrentProd as 'Product
Set oMyDoc = CATIA.ActiveDocument
or
Dim CATIA as variant
Set CATIA = Getobject(,"CATIA.Application")
Dim oMyDoc as variant
Dim oCurrentProd as variant
Set oMyDoc = CATIA.ActiveDocument
I recommend you to first declare them properly typed so you can see the object's methods and properties and afterwards change it to variant

Powerpoint VBA - Set DataRange for DataLabels

I have no idea how to set DataRange for DataLabels using VBA.
Powerpoint does not have recording capabilities as well.
Can anybody tell me to do this using VBA please?
The code to accomplish this is as follows:
Dim myChart As Chart
Dim mySerCol As SeriesCollection
Dim strRange As String
strRange = "=Sheet1!$F$2:$F$5" 'To hold the range for the new labels
Set myChart = ....[put in code to get the appropriate chart]
Set mySerCol = myChart.SeriesCollection(i)
mySerCol.ApplyDataLabels 'Turn on the datalabels for this series
'The next line sets the range to get the values from
mySerCol.Format.TextFrame2.TextRange.InsertChartField msoChartFieldRange _
, strRange, 0
mySerCol.ShowRange = True 'Show the values from the range
mySerCol.ShowValue = False 'Do not show the actual values of the points
Note that this will only do this for one of the series. To do the other ones, loop through i in the myChart.SeriesCollections(i) line.
****EDIT**** See other answer. I am leaving this here because it provides some information about several objects that could be used, but doesn't actually solve the problem.
This is not a complete answer to the issue, but this is too long for a comment.
I've searched through the documentation for the Datalabels and was unable to figure out how to do this (I assume that you want to be able to define the range that the labels come from using VBA). I was able to "check" the checkbox, but couldn't figure out where the range that is attached to it is. The appropriate code to check.
To check the checkbox, use this code:
myChart.SeriesCollection(i).ApplyDataLabels
where i is the series in question and myChart is a Chart object referencing your chart. There are a bunch of parameters to this method that will allow you to show different items (percentages, values, etc.), but none of the parameters is a range.
This defaults to the values of the series if you do not enter any of the optional parameters
It is then possible to turn this on and off using:
myChart.SeriesCollection(i).DataLabels.ShowRange = True/False
It is possible to change the caption of the Datalabels using:
myChart.SeriesCollection(i).DataLabels(j).Caption = "MY CAPTION"
This will change the caption one at a time, and it will replace the value that the "ApplyDataLabels" method puts in there. It would be possible to loop through the range to set the values, but this is likely not what you are looking for.
There's also this:
myChart.SeriesCollection(i).HasDataLabels = True
but this just seems to turn them on and off and resets the captions that you may have put in there.
MSDN link uses both the hasdatalabels property and the applydatalabels method, but it is not clear why they are using both: https://msdn.microsoft.com/EN-US/library/office/ff745965.aspx
Hopefully this can at least give you something to start with.

Can't import shapes from Word to PowerPoint 2013; works in Office 2010

I'm writing a PowerPoint macro that opens a Word document and imports every picture in the document to its own slide. Because I can't place the Word InlineShape objects directly in PowerPoint, apparently, I'm using the clipboard. The relevant code is:
Dim wdApp As Object
Dim SourceDoc As Document
Dim TargetSlide As Slide
Dim Figures As Word.InlineShapes
Dim Figure As Word.InlineShape
Dim TargetShapeRange As ShapeRange
Set wdApp = CreateObject("Word.Application")
Set SourceDoc = wdApp.Documents.Open(FileName:=PathToFile, ReadOnly:=True)
Set Figures = SourceDoc.InlineShapes
For Each Figure In Figures
Figure.Range.Select
Figure.Application.Selection.Copy
Set TargetSlide = ActivePresentation.Slides.AddSlide(ActivePresentation.Slides.Count + 1, _
ActivePresentation.SlideMaster.CustomLayouts(6))
Set TargetShapeRange = TargetSlide.Shapes.Paste
Next Figure
With PowerPoint 2010 and Word 2010, this works perfectly. In PowerPoint 2013 (with Word 2013 installed, and referencing the Microsoft Word 15.0 Object Library rather than 14.0), the Set TargetShapeRange = TargetSlide.Shapes.Paste line gives me run-time error '-2147188160 (80048240)':
Shapes (unknown member): Invalid request. Clipboard is empty or contains data which may not be pasted here.
I've tried using the Copy and CopyAsPicture methods on both the Selection and Range objects. I've tried using the Shapes.PasteSpecial command. I've tried several different ways of accessing the Word Application object. Nothing works.
Notably, if I modify the code to copy the preceding paragraph of text in addition to the picture, the script works—it retrieves the text, but not the picture.
I know this question is almost two years old but I found this through google after having a similar issue so I'll post this here.
Try
Figure.Application.ActiveWindow.ScrollIntoView Selection.Range, True
right before copying. It works for me even with screenupdating disabled or if the application is not visible.

How can I export point coordinates from a CATIA product to excel

What I'm looking for is a VB script written in either excel or CATIA that can export the coordinates of points in a CATProduct to an excel spreadsheet. The process needs to be as automated as possible due to the large number of points that I am dealing with.
Eventually, I will need to export only specific points and group these points together in 4's to identify what part they belong to.
I have an excel script that allow for points to be imported, but this only takes points from a geometry set and the points in the product I'm looking at are in the part body.
Follow this link here to write to a CSV file which can be imported to excel:
http://www.coe.org/p/fo/et/thread=27438
You'll need to add the excel VBA references files to you Catia VBA Project.
Regarding your point information:
Just to show you how to drill down to a point, I used Insert > Object Resolution of a basic point and included some comments on how to get the coordinates and also where to loop. There is one thing to note, some methods are "marked as restricted" which necessitates the intermediate "hack" of setting the point object to a variant before you can use the "GetCoordinates" sub.
Sub GetPointData()
'---- Begin resolution script for object : Point.1
Dim partDocument1 As PartDocument
Set partDocument1 = CATIA.ActiveDocument
Dim part1 As Part
Set part1 = partDocument1.Part
Dim hybridBodies1 As HybridBodies
Set hybridBodies1 = part1.HybridBodies
Dim hybridBody1 As HybridBody
Set hybridBody1 = hybridBodies1.Item("Geometrical Set.1")
Dim hybridShapes1 As HybridShapes
Set hybridShapes1 = hybridBody1.HybridShapes
Dim hybridShapePointCoord1 As HybridShapePointCoord
Dim XYZ(2) As Variant
Dim var As Variant
'BEGIN LOOP THROUGH YOUR POINTS HERE
Set hybridShapePointCoord1 = hybridShapes1.Item("Point.1")
Set var = hybridShapePointCoord1
var.GetCoordinates XYZ
'WRITE XYZ TO CSV
'NEXT POINT
'END LOOP
'---- End resolution script
End Sub
I believe it can be done, what I would do is to search and select all points in CATProduct, then get parent for each selected point up to the Part, then get coordinates (of course, you need to write everything in Excel if you have the code there).
I don't know if you can upload here your excel vba but shouldn't be so difficult.