Cannot find VBAProject

Cannot find VBAProject

Anonymous
Not applicable
553 Views
8 Replies
Message 1 of 9

Cannot find VBAProject

Anonymous
Not applicable
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
0 Likes
554 Views
8 Replies
Replies (8)
Message 2 of 9

Anonymous
Not applicable
Additional Information - If the user manually starts and then closed the VB editor the program runs OK, still cycles NumLock.

Initial error attached.

thanks !!!!
0 Likes
Message 3 of 9

Anonymous
Not applicable
I put the following code in another module -

Public Sub RollVBE()

SendKeys "%{F11}", True
SendKeys "%FC", True

SendKeys "%{F11}", True
SendKeys "%FC", True


End Sub

Put a button to it, clicked the button and then clicked the button for the previous code. this works great.

I copied and pasted the above code into the titleblockupdater code and it dumps... I'm very confused, starting with "why do I need to cycle the VB Editor" thru to "why won't the same code work in different modules"????
0 Likes
Message 4 of 9

Anonymous
Not applicable
I'm not sure, but it seems like the problem is that the document's VBA
project is not getting created on some machines. And hence the
Document.VBAProject property fails. Activating the VBA environment may
trigger the creation of this project.

Any reason why you are putting the code into a document project, rather than
the application VBA project? Putting it into the document project would save
the code with the document when it is saved and may create maintenance
issues later. In general, adding VBA code to document projects is being
discouraged.

Sanjay-
0 Likes
Message 5 of 9

Anonymous
Not applicable
Sanjay,

thanks for the response. I am adding code to the document project because it's an on-save event for drawings only. I'm fairly inexperienced at this but it's the only way I could see to make this happen.

The code being copied into the file (along with updated titleblock geometry and data) makes the titleblock and metadata of the drawing match the part or assembly file the drawing is linked to.

any idea why -

SendKeys "%{F11}", True
SendKeys "%FC", True

- cycles the numlock keyboard function?

If you are right then the documents VBA project isn't created on any of our machines. It's only on some that the orignial code I wrote will not cycle the VB Editor.

Thanks again!

Eric
0 Likes
Message 6 of 9

Anonymous
Not applicable
I would highly recommend using an Add-In for this instead of the document
project. The only good reason to use a document project is for programs
that are written to work with a specific document and aren't generaly useful
with other documents. It sounds like in your case your program is a general
purpose program. One of the big problems you'll find is maintenance of the
code. For example, if two months from now you realize there's a problem
with the program or you need to add an enhancement you've got copies of this
program all over and it's difficult to track which version is in which
document. With an Add-In there's only one program. I suspect what you want
to do will be a fairly simple Add-In and would be a good introduction to
Add-Ins.

Let me know if you want to try this and need some help getting started.
--
Brian Ekins
Autodesk Inventor API
0 Likes
Message 7 of 9

Anonymous
Not applicable
Brian, sounds like a great idea but I know nothing about add-ins, I barely know anything about Macro's!

I could use all the help I can get.

Thanks!

Eric
0 Likes
Message 8 of 9

Anonymous
Not applicable
Here are some steps to get you started.

1. If you don't already have a copy of VB.Net go ahead and download the free
version of VB Express 2005 from http://www.microsoft.com/express/2005/. VB
Express 2008 is the current version but is not officially supported for
Inventor yet so you'll have less issues if you just use the 2005 version.

2. Install the Inventor Add-In Wizard. In Inventor 2008 it's in
SDK\Tools\Developers\Wizards directory. Just run the InventorWizards.msi
installer to install it.

3. Create a new VB project, selecting "Autodesk Inventor Add-In" as the
template when creating the project.

4. Add the declaration for m_AppEvents to your project as shown below. The
first line is already there.

Private m_inventorApplication As Inventor.Application
Private WithEvents m_appEvents As Inventor.ApplicationEvents

5. Add the second line below into the Activate method. The first line is
already there.

m_inventorApplication = addInSiteObject.Application
m_appEvents = m_inventorApplication.ApplicationEvents

6. Create a handler for your event by using the combo boxes at the top of
the code window. Click the left one to choose "m_appEvents" and then click
the right one and choose "OnSaveDocument". A new sub will be inserted into
your add-in. This sub will be called by Inventor anytime a document is
saved. Here's an example of a simple implementation.

Private Sub m_appEvents_OnSaveDocument( _
ByVal DocumentObject As Inventor._Document, _
ByVal BeforeOrAfter As Inventor.EventTimingEnum, _
ByVal Context As Inventor.NameValueMap, _
ByRef HandlingCode As Inventor.HandlingCodeEnum) _
Handles m_appEvents.OnSaveDocument

If BeforeOrAfter = EventTimingEnum.kAfter Then
MsgBox("The document " & DocumentObject.FullFileName & " was just
saved.")
End If
End Sub

This just displays a message box anytime a document is saved but you can put
whatever code you currently have in your document project here. I've also
attached a paper from last years Autodesk University that discusses add-ins
a bit more and the differences between VBA and VB.Net. Good luck.
--
Brian Ekins
Autodesk Inventor API

wrote in message news:5925069@discussion.autodesk.com...
Brian, sounds like a great idea but I know nothing about add-ins, I barely
know anything about Macro's!

I could use all the help I can get.

Thanks!

Eric
0 Likes
Message 9 of 9

Anonymous
Not applicable
Sweet, now I have to get It over to install a bunch of software!

Thanks for the help Brian, much appreciated.

Eric
0 Likes