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'."
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();
I found my answer in the InDesign Scripting Samples - the Neon sample script gave grouping examples
Related
I need a list of printers that DO NOT print direct. Getting a list that do print direct seems reasonably easy. But how to do the opposite?
Dim PrintServer As New SysPrint.PrintServer
Dim arrFlags(0) As SysPrint.EnumeratedPrintQueueTypes
arrFlags(0) = System.Printing.EnumeratedPrintQueueTypes.DirectPrinting
Dim QColl As SysPrint.PrintQueueCollection = PrintServer.GetPrintQueues(arrFlags)
PrintServer.GetPrintQueues Method
EnumeratedPrintQueueTypes Enumeration
MSDN says that the EnumeratedPrintQueueTypes has a FlagsAttribute attribute that allows a bitwise combination of its member values. So I should be able to specify NOT direct somehow. How do I do it?
I tried to do this arrFlags(0) = Not System.Printing.EnumeratedPrintQueueTypes.DirectPrinting but that returned no results. Clearly incorrect.
So how do I manipulate the flags attribute to eliminate all printers printing direct?
This is one way to do it but it seems very inelegant:
'get full list
Dim PrintServer As New SysPrint.PrintServer
Dim QColl As SysPrint.PrintQueueCollection = PrintServer.GetPrintQueues()
'get those not printing direct
Dim Qcoll2 As List(Of SysPrint.PrintQueue) = QColl.Where(Function(x) Not (x.IsDirect)).ToList
'select name only
Dim strList As List(Of String) = Qcoll2.Select(Function(x) x.Name).ToList
I've recently implemented reflection to replace the more tedious aspects of our data retrieval from a SQL database. The old code would look something like this:
_dr = _cmd.ExecuteReader (_dr is the SQLDataReader)
While _dr.Read (_row is a class object with public properties)
_row.Property1 = Convert.ToInt16(_dr("Prop1"))
_row.Property2 = Convert.ToInt16(_dr("Prop2"))
_row.Property3 = Convert.ToInt16(_dr("Prop3"))
If IsDBNull(_dr("Prop4")) = False Then _row.Prop4 = _dr("Prop4")
...
Since my code base has a lot of functionality like this, reflection seemed like a good bet to simplify it and make future coding easier. How to assign datareader data into generic List ( of T ) has a great answer that was practically like magic for my needs and easily translated into VB. For easy reference:
Public Shared Function GenericGet(Of T As {Class, New})(ByVal reader As SqlDataReader, ByVal typeString As String)
'Dim results As New List(Of T)()
Dim results As Object
If typeString = "List" Then
results = New List(Of T)()
End If
Dim type As Type = GetType(T)
Try
If reader.Read() Then
' at least one row: resolve the properties
Dim props As PropertyInfo() = New PropertyInfo(reader.FieldCount - 1) {}
For i As Integer = 0 To props.Length - 1
Dim prop = type.GetProperty(reader.GetName(i), BindingFlags.Instance Or BindingFlags.[Public])
If prop IsNot Nothing AndAlso prop.CanWrite Then
props(i) = prop
End If
Next
Do
Dim obj = New T()
For i As Integer = 0 To props.Length - 1
Dim prop = props(i)
If prop Is Nothing Then
Continue For
End If
' not mapped
Dim val As Object = If(reader.IsDBNull(i), Nothing, reader(i))
If val IsNot Nothing Then SetValue(obj, prop, val)
Next
If typeString = "List" Then
results.Add(obj)
Else
results = obj
End If
Loop While reader.Read()
End If
Catch ex As Exception
Helpers.LogMessage("Error: " + ex.Message + ". Stacktrace: " + ex.StackTrace)
End Try
Return results
End Function
The only caveat with this is that it is somewhat slower.
My question is how to optimize. Sample code I find online is all in C# and does not convert neatly into VB. Scenario 4 here seems like exactly what I want, but converting it to VB gives all kinds of errors (Using CodeFusion or converter.Telerik.com).
Has anyone done this in VB before? Or can anyone translate what's in that last link?
Any help's appreciated.
Couple ideas for you.
Don't use the DataReader when reading ALL records at once, it is slower than using a DataAdapter.
When you use the DataAdapter to fill a DataSet, you can iterate through the rows and columns which does NOT use reflection and will be much faster.
I have a program I created (and many other programmers do this too) that generate the code from a database for me. Each table and row is a class that is specifically named an generated in such a way that I can use intellisense and prevents many run-time errors by making them compile-time errors when data changes. This is very much like the EntityFramework but lighter because it fits MY specific needs.
I have vb.net application which fetches data from the excel and perform some operation.
I am using excel object Range to get the values from excel.
Below is the line of code I am using,
xlsRangetrans = xlsWorkbook.Worksheets(SHEET_1).Range("Range1")
Dim transArray(,) As Object = xlsRangetrans.Value
The above code runs successfully if I have more than one values in the Range1.
But if I have only one value I am getting error.
Eg: xlsRangetras.value has "123.0,124.0,nothing,nothing" as System.Array which runs successfully
But if xlsRangetras.value has "123.0" as Double, it gives me error.
How to convert Double value to Array in my case.
Please help.
Dim helper As Object = xlsRangetrans.Value
Dim transArray(,) As Object = _
If(TypeOf helper Is Object(,), helper, {{DirectCast(helper, Object)}})
Update
Obviously the If operator didn't exist in older .NET versions. In this case that piece of code might help.
Dim helper As Object = xlsRangetrans.Value
Dim transArray(,) As Object
If TypeOf helper Is Object(,) Then
transArray = helper
Else
transArray = {{DirectCast(helper, Object)}}
End If
The title is to make this easy to find for others having this error. I'm new to Threading, so this is really giving me heck. I'm getting this runtime error that crashed Cassini. This is code that I'm maintaining originally developed as a website project in VS 2003 and converted to a VS 2008 website project.
Important Info:
The number of objects in the manualEvents array is 128 in this case.
products is an array of Strings
Need to support .NET 2.0
For Each product As String In products
If Not product.Trim().ToUpper().EndsWith("OBSOLETE") Then
calls += 1
End If
Next
Dim results(calls - 1) As DownloadResults
'Dim manualEvents(calls - 1) As Threading.ManualResetEvent '128 objects in this case.
Dim manualEvents(0) As Threading.ManualResetEvent
manualEvents(0) = New Threading.ManualResetEvent(False)
'NOTE: I don't think this will work because what is not seen here, is that
' this code is being used to populate and cache a long list of products,
' each with their own category, etc. Am I misunderstanding something?
'initialize results structures
'spawn background workers
calls = 0
For Each product As String In products
If Not product.Trim().ToUpper().EndsWith("OBSOLETE") Then
Dim result As New DownloadResults
'manualEvents(calls) = New Threading.ManualResetEvent(False)
'Moved above For Each after declaration of variable
result.params.product = product
result.params.category = docType
'result.ManualEvent = manualEvents(calls)
result.ManualEvent = manualEvents(0)
result.Context = Me._context
results(calls) = result
Threading.ThreadPool.QueueUserWorkItem(AddressOf ProcessSingleCategoryProduct, results(calls))
Threading.Interlocked.Increment(calls) 'Replaces below incrementation
'calls += 1
End If
Next
Threading.WaitHandle.WaitAll(manualEvents) 'CRASHES HERE
Thread Helper Function (for the sake of completion)
Public Shared Sub ProcessSingleCategoryProduct(ByVal state As Object)
Dim drs As DownloadResults = CType(state, DownloadResults)
Dim adc As New cADCWebService(drs.Context)
drs.docs = adc.DownloadADC(drs.params.category, drs.params.product)
drs.ManualEvent.Set()
End Sub
You don't need an array of 128 manual events to check for completion of all 128 threads.
Create only one manual reset event and a plain integer starting at 128. Decrement that integer using Interlocked.Decrement at the end of ProcessSingleCategoryProduct, and only signal the event when the count reaches zero:
if (Interlocked.Decrement(ByRef myCounter) = 0) myEvent.Set();
Then declare only one Threading.ManualResetEvent as opposed to an array of them, and you can call WaitOne instead of WaitAll on it, and you are done.
See also usr's comment for an easier alternative in case you have .NET 4.
I am trying to make an array of hashtables. I don't know if this is the best solution, in PHP I would just do a multi-dim array, but it's not so easy in .NET. I am pretty new o VB, so if there is a better solution for this please explain.
I have 2 emails fields for a contact, and a contact can have many. I just want to load in the first two on the edit page for editing, whatever they may be.
Private Sub loadPrimaryContactEmails(ByVal db As CRMDataDataContext, ByVal contactID As Guid)
Dim q = (From ce In db.ContactEmails Where ce.ContactID = contactID Select ce).Take(2)
Dim Emails As Array
Dim count = 0
For Each email In q
Emails(count) = New Hashtable
Emails(count).Add("email", email.Email)
Emails(count).Add("label", email.Label)
Emails(count).Add("id", email.ContactEmailID)
count = count + 1
Next
txtPCEmail1.Text = Emails(0).Item("email")
txtPCEmail1Label.Text = Emails(0).Item("label")
lblEmail1ID.Text = Emails(0).Item("id")
txtPCEmail2.Text = Emails(1).Item("email")
txtPCEmail2Label.Text = Emails(1).Item("label")
lblEmail2ID.Text = Emails(1).Item("id")
End Sub
I get the error the first time I try to reference my array:
txtPCEmail1.Text = Emails(0).Item("email")
The error is:
Object variable or With block variable not set.
It builds, so I thought it might work. I can't just loop through my datasource because I have to explicitly set textbox fields. Is there a better way to go about doing this? Or is there a way to make an array of hashtables work?
EDIT - here is the good code:
So I went w/ the HybridDictionary...
Private Sub loadPrimaryContactEmails(ByVal db As CRMDataDataContext, ByVal contactID As Guid)
Dim q = (From ce In db.ContactEmails Where ce.ContactID = contactID Select ce).Take(2)
Dim Emails As New HybridDictionary()
Dim count = 1
For Each email In q
Emails.Add("email" + NCStr(count), email.Email)
Emails.Add("label" + NCStr(count), email.Label)
Emails.Add("id" + NCStr(count), email.ContactEmailID)
count = count + 1
Next
txtPCEmail1.Text = Emails("email1")
txtPCEmail1Label.Text = Emails("label1")
lblEmail1ID.Text = Emails("id1")
txtPCEmail2.Text = Emails("email2")
txtPCEmail2Label.Text = Emails("label2")
lblEmail2ID.Text = Emails("id2")
End Sub
SO yeah, kind of a hack, but I don't feel like I should have to making special methods just to load some data into a dictionary or array or whatever.
Arrays in VB.NET are different than in PHP. You will need to define the size of your array before attempting to set elements of the array.
Better yet, consider using the generic List<T> collection.
Yes Phil is right you haven't specified the Initial Size of the Array.
And as suggested by him Use generic list or I would recommend
"System.Collections.Specialized.StringCollection" Class or "System.Collections.Specialized.HybridDictionary" class
Build the hashtable first and then build the array.
Dim hash As New Hashtable()
hash.Add("Header", shortModel)
hash.Add("SpecInfo", specinfo)
hash.Add("SerialNumber", serie & "-L")
hash.Add("SerialNumber2", serie)
hash.Add("seriel", serie & "-L")
hash.Add("serie", serie)
hash.Add("Product", modelBase)
hash.Add("varBC", bc)
hash.Add("box_id", boxId.Substring(4).ToString)
Dim dt As DataTable = DbUtil.GetCursor("SFISM4.PKG_AGENCY.sp_get_print_param", {New OracleParameter("in_serie", "3CE5151ZW4")})
For Each row As DataRow In dt.Rows
hash.Add(row("NAME"), row("VALUE"))
Next
Dim mArray(hash.Count() - 1, 1) As String
Dim i As Integer = 0
For Each row As DictionaryEntry In hash
mArray(i, 0) = row.Key.ToString()
mArray(i, 1) = row.Value.ToString()
i = i + 1
Next