vb.net in InDesign Scripting - Grouping TextFrames
-
29-09-2019 - |
Question
I want to group textframes in my InDesign CS3 vb.net script. It worked for InDesign 2.0 but it does not work with InDesign CS3. Here is my code:
Dim myDoc As InDesign.Document = Nothing
Dim myGroup As InDesign.Group = Nothing
Dim myObjectList(2)
myObjectList.SetValue(myOuterTextFrame, 0)
myObjectList.SetValue(myInnerTextFrame, 1)
myObjectList.SetValue(myContentTextFrame, 2)
myGroup = myDoc.Groups.Add(myObjectList)
Getting error "Unable to cast object of type 'System.Object[]' to type 'InDesign.Objects'."
Solution 3
I found my answer in the InDesign Scripting Samples - the Neon sample script gave grouping examples
OTHER TIPS
I know you asked for this a long time ago so I'm mostly answering for future searches. I haven't found a fully managed way to do this using the .Net Framework and believe me, I've searched for it. I've tried a million different casts, subclassing, reflection, you name it. What ultimately worked in the end was JavaScript. Below is a method that takes an InDesign.Document object and two or more integers that represent InDesign item IDs. It then creates some JavaScript and has InDesign execute it. Finally it returns an InDesign.Group created from those objects.
Public Function GroupObjects(ByVal indesignDocument As InDesign.Document, ByVal ParamArray objectIds() As Integer) As InDesign.Group
'Sanity checks
If indesignDocument Is Nothing Then Throw New ArgumentNullException("indesignDocument")
If objectIds Is Nothing OrElse objectIds.Count < 2 Then Throw New ArgumentException("You must pass at least 2 object ids")
'We'll assign a unique label to the group that we create in JavaScript so that we can find it in managed code later
Dim GID = Guid.NewGuid().ToString()
'Create the JavaScript
Dim Buf As New StringBuilder()
Buf.AppendLine("var items = new Array();")
For Each ID In objectIds
Buf.AppendFormat("items.push(app.activeWindow.activePage.pageItems.itemByID({0}));", ID)
Buf.AppendLine()
Next
Buf.AppendLine("var g = app.activeWindow.activePage.groups.add(items);")
Buf.AppendFormat("g.label='{0}';", GID)
Dim IA = indesignDocument.Parent
IA.DoScript(Buf.ToString(), InDesign.idScriptLanguage.idJavascript)
'Loop through all document groups looking for the object with the label created above
For Each G As InDesign.Group In indesignDocument.Groups
If Not String.IsNullOrWhiteSpace(G.Label) AndAlso G.Label = GID Then Return G
Next
Return Nothing
End Function
To use it in your code you'd say:
Dim MyGroup = GroupObjects(myOuterTextFrame, myInnerTextFrame, myContentTextFrame)
This one worked for me:
Type type = Type.GetTypeFromProgID("InDesign.Application");
Host = (InDesign.Application)Activator.CreateInstance(type);
InDesign.Objects o = Host.CreateCollection();