Message 1 of 9
Cannot find VBAProject

Not applicable
05-06-2008
11:27 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hey y'all - little problem with some long running code. Since moving to Vista Business (64) half of my users have had trouble with the following code. Firstly the code hangs (see attached image) when trying to access the target documents VBProject. (The initial VB error will be contained in the next post). Secondly, the code seems to be cycling the NumLock setting when it fails. 3 of my Vista users are experiencing this while 3 others are not. Complete code set below. Any advice on what is causing this? Any suggestions in cleaning up my code also greatly appreciated!
Note - In XP I had to cycle the VB editor open and closed, I don't know why, to get the code to work. I have a sneaking suspicion that this action is definately related to the problems.
Thanks!
Eric
SysSpecs
IV 2008Pro SP1 - PS2008 (Admin) - WinVistaBusiness64 - Xeon 4core 1.86GHz64
8GBRAM - NVIDIA Quadro FX 4600 (driver 7.15.11.6002 - 4/26/2007) 768MB
*************************************************
Public Sub TitleBlockUpdater()
'Set Active Application
Dim oApp As Application
Set oApp = ThisApplication
'Get the active document and make sure it's a drawing.
Dim oTargetDoc As Document
Set oTargetDoc = ThisApplication.ActiveDocument
If oTargetDoc.DocumentType kDrawingDocumentObject And oTargetDoc.DocumentSubType.DocumentSubTypeID "{BBF9FDF1-52DC-11D0-8C04-0800090BE8EC}" Then
MsgBox "Must be in Drawing Document to run the Title Block Update Tool! "
Exit Sub
End If
'Open the VB Editor
SendKeys "%{F11}", True
'Copy in the VBA code.
Dim oTargetDocProj As InventorVBAProject
Set oTargetDocProj = ThisApplication.ActiveDocument.VBAProject
Dim oVBProj As Object
Set oVBProj = oTargetDocProj.VBProject
Dim oVBModule As Object
Set oVBModule = oVBProj.VBComponents.Item("Module1").CodeModule
Call oVBModule.AddFromFile("J:\Shared\Library\Inventor\ZZCode-12\Inventor\UpdateCode.bas")
'Save the names of the title block definition used for each sheet
'and delete the title blocks.
Dim colDefNames As New Collection
Dim oTargetSheet As Sheet
For Each oTargetSheet In oTargetDoc.Sheets
'Activate the sheet.
oTargetSheet.Activate
' Save the name of the title block definition used for this sheet.
colDefNames.Add oTargetDoc.ActiveSheet.TitleBlock.Definition.Name
' Delete the title block.
oTargetSheet.TitleBlock.Delete
Next
'Copy the new definitions from the source into the target document.
oTargetDoc.Activate
Dim oSourceDoc As Inventor.DrawingDocument
Set oSourceDoc = ThisApplication.Documents.Open("J:\Shared\Library\Inventor\ZZTemplates-12\Standard.idw")
Dim oSourceTB As TitleBlockDefinitions
Set oSourceTB = oSourceDoc.TitleBlockDefinitions
Dim oSourceSS As SketchedSymbolDefinitions
Set oSourceSS = oSourceDoc.SketchedSymbolDefinitions
Call oSourceTB.Item("ENTEK - A").CopyTo(oTargetDoc, True)
Call oSourceTB.Item("ENTEK - B").CopyTo(oTargetDoc, True)
Call oSourceTB.Item("ENTEK - C").CopyTo(oTargetDoc, True)
Call oSourceTB.Item("ENTEK - D").CopyTo(oTargetDoc, True)
Call oSourceTB.Item("ENTEK - E").CopyTo(oTargetDoc, True)
Call oSourceSS.Item("Revision Block").CopyTo(oTargetDoc, True)
Call oSourceSS.Item("Mass Adder").CopyTo(oTargetDoc, True)
'Add the title blocks to the sheets.
Dim i As Integer
For Each oTargetSheet In oTargetDoc.Sheets
'Activate the sheet.
oTargetSheet.Activate
'Add the title block.
i = i + 1
Call oTargetSheet.AddTitleBlock(oTargetDoc.TitleBlockDefinitions.Item(colDefNames.Item(i)))
Next
'Add any iProperties.
On Error Resume Next
Dim oCustomSet As PropertySet
Set oCustomSet = oTargetDoc.PropertySets.Item("Inventor User Defined Properties")
Call oCustomSet.Add("NIL", "FirstViewScale")
On Error GoTo 0
'Clean up.
oApp.CommandManager.ControlDefinitions.Item("AppVBAEditorCmd").Execute2 (True)
AppActivate oApp.VBE.mainwindow.Caption
SendKeys "%FC", True
oSourceDoc.Close
Set oTargetDoc = Nothing
Set oTargetSheet = Nothing
Set oSourceDoc = Nothing
Set oSourceTB = Nothing
Set oCustomSet = Nothing
Set oProp = Nothing
End Sub
Note - In XP I had to cycle the VB editor open and closed, I don't know why, to get the code to work. I have a sneaking suspicion that this action is definately related to the problems.
Thanks!
Eric
SysSpecs
IV 2008Pro SP1 - PS2008 (Admin) - WinVistaBusiness64 - Xeon 4core 1.86GHz64
8GBRAM - NVIDIA Quadro FX 4600 (driver 7.15.11.6002 - 4/26/2007) 768MB
*************************************************
Public Sub TitleBlockUpdater()
'Set Active Application
Dim oApp As Application
Set oApp = ThisApplication
'Get the active document and make sure it's a drawing.
Dim oTargetDoc As Document
Set oTargetDoc = ThisApplication.ActiveDocument
If oTargetDoc.DocumentType kDrawingDocumentObject And oTargetDoc.DocumentSubType.DocumentSubTypeID "{BBF9FDF1-52DC-11D0-8C04-0800090BE8EC}" Then
MsgBox "Must be in Drawing Document to run the Title Block Update Tool! "
Exit Sub
End If
'Open the VB Editor
SendKeys "%{F11}", True
'Copy in the VBA code.
Dim oTargetDocProj As InventorVBAProject
Set oTargetDocProj = ThisApplication.ActiveDocument.VBAProject
Dim oVBProj As Object
Set oVBProj = oTargetDocProj.VBProject
Dim oVBModule As Object
Set oVBModule = oVBProj.VBComponents.Item("Module1").CodeModule
Call oVBModule.AddFromFile("J:\Shared\Library\Inventor\ZZCode-12\Inventor\UpdateCode.bas")
'Save the names of the title block definition used for each sheet
'and delete the title blocks.
Dim colDefNames As New Collection
Dim oTargetSheet As Sheet
For Each oTargetSheet In oTargetDoc.Sheets
'Activate the sheet.
oTargetSheet.Activate
' Save the name of the title block definition used for this sheet.
colDefNames.Add oTargetDoc.ActiveSheet.TitleBlock.Definition.Name
' Delete the title block.
oTargetSheet.TitleBlock.Delete
Next
'Copy the new definitions from the source into the target document.
oTargetDoc.Activate
Dim oSourceDoc As Inventor.DrawingDocument
Set oSourceDoc = ThisApplication.Documents.Open("J:\Shared\Library\Inventor\ZZTemplates-12\Standard.idw")
Dim oSourceTB As TitleBlockDefinitions
Set oSourceTB = oSourceDoc.TitleBlockDefinitions
Dim oSourceSS As SketchedSymbolDefinitions
Set oSourceSS = oSourceDoc.SketchedSymbolDefinitions
Call oSourceTB.Item("ENTEK - A").CopyTo(oTargetDoc, True)
Call oSourceTB.Item("ENTEK - B").CopyTo(oTargetDoc, True)
Call oSourceTB.Item("ENTEK - C").CopyTo(oTargetDoc, True)
Call oSourceTB.Item("ENTEK - D").CopyTo(oTargetDoc, True)
Call oSourceTB.Item("ENTEK - E").CopyTo(oTargetDoc, True)
Call oSourceSS.Item("Revision Block").CopyTo(oTargetDoc, True)
Call oSourceSS.Item("Mass Adder").CopyTo(oTargetDoc, True)
'Add the title blocks to the sheets.
Dim i As Integer
For Each oTargetSheet In oTargetDoc.Sheets
'Activate the sheet.
oTargetSheet.Activate
'Add the title block.
i = i + 1
Call oTargetSheet.AddTitleBlock(oTargetDoc.TitleBlockDefinitions.Item(colDefNames.Item(i)))
Next
'Add any iProperties.
On Error Resume Next
Dim oCustomSet As PropertySet
Set oCustomSet = oTargetDoc.PropertySets.Item("Inventor User Defined Properties")
Call oCustomSet.Add("NIL", "FirstViewScale")
On Error GoTo 0
'Clean up.
oApp.CommandManager.ControlDefinitions.Item("AppVBAEditorCmd").Execute2 (True)
AppActivate oApp.VBE.mainwindow.Caption
SendKeys "%FC", True
oSourceDoc.Close
Set oTargetDoc = Nothing
Set oTargetSheet = Nothing
Set oSourceDoc = Nothing
Set oSourceTB = Nothing
Set oCustomSet = Nothing
Set oProp = Nothing
End Sub