|
Thanks all I think this is just an example of carrying old ideas forward by copy/paste I began using that code construct in my early days of vbScript. I was always experimenting with forum code snippets that created temporary queries but did not delete them (exactName=False). Queries kept building up in the project as you ran the script. Another situation where deletion does not occur is with run-time errors (oops, I get lots of those) that prevent the code from completing and never gets to the query deletion code. This is mostly in the developing stage but, again, the query is left in the project. Using an obscure name like "__Query__1" would minimize the risk of having a non-query component with such a name. So maybe something like: 'VB .NET Dim Qry1 As Manifold.Interop.Query If Context.Document.ComponentSet.ItemByName("__Query__1") < 0 Then ' Create a new query Qry1 = Context.Document.NewQuery("__Query__1") ElseIf Context.Document.ComponentSet.Item("__Query__1").Type = _ Manifold.Interop.ComponentType.ComponentQuery Then ' Use the old Query Qry1 = Context.Document.ComponentSet.Item("__Query__1") Else ' Flag an error MessageBox.Show( _ text:="__Query__1 exists in the project but is not a Query Component.", _ caption:="Error", _ buttons:=System.Windows.Forms.MessageBoxButtons.OK, _ icon:=System.Windows.Forms.MessageBoxIcon.Information) Exit Sub End If Perhaps the simplest way of handling this in a .NET application would be to use exactName=False when creating a Query which would always create a new Query but allow Manifold to change the name to prevent conflicts. Use a Finally statement in the error routines to delete the temporary query. This would get rid of it even if a run-time error prevented completion of the main subroutine/function code. 'VB .NET Dim Qry1 As Manifold.Interop.Query Try ' ... Qry1 = Context.Document.NewQuery("Q1", False) Catch ex As Exception ' Error message Finally If Context.Document.ComponentSet.ItemByName(Qry1.Name) >= 0 Then Context.Document.ComponentSet.Remove(Qry1) End If End Try Better ideas very much welcomed!
|