Object Variable or With block Variable Not Set - Help

Object Variable or With block Variable Not Set - Help

Anonymous
Not applicable
1,652 Views
8 Replies
Message 1 of 9

Object Variable or With block Variable Not Set - Help

Anonymous
Not applicable
I'm using the following code from the Inventor help example to set my project. But I get this error, "Object Variable or With block Variable Not Set." It may be that I'm using .NET, not sure?



Thx,



Jon



Public Sub SetProject()

' Check to make sure a document isn't open.

If ThisApplication.Count > 0 Then

MsgBox("All documents must be closed before changing the project.")

Exit Sub

End If



' Set a reference to the FileLocations object.

Dim oFileLocations As FileLocations

oFileLocations = CType(ThisApplication.FileLocations, FileLocations)



' Show the current workspace.

Debug.Print("Workspace: " & oFileLocations.Workspace)



' Set the project file to use.

' This assumes that "C:\Temp\MyProject.ipt" exists.

oFileLocations.FileLocationsFile = "F:\Global-Edit.ipj"



' Show the workspace after making the project change.

Debug.Print("Workspace: " & oFileLocations.Workspace)

End Sub
0 Likes
1,653 Views
8 Replies
Replies (8)
Message 2 of 9

Anonymous
Not applicable

There were a couple of syntax issues. You need a
set for the following line:

Set oFileLocations =
ThisApplication.FileLocations

 

Try the following macro.

 

Sanjay-

 

 

Public Sub SetProject()

 

' Check to make sure a document isn't
open.

 

If ThisApplication.Documents.Count > 0
Then

 

MsgBox ("All documents must be closed before
changing the project.")

 

Exit Sub

 

End If

 

 

 

' Set a reference to the FileLocations
object.

 

Dim oFileLocations As FileLocations

 

Set oFileLocations =
ThisApplication.FileLocations

 

 

 

' Show the current workspace.

 

Debug.Print ("Workspace: " &
oFileLocations.Workspace)

 

 

 

' Set the project file to use.

 

' This assumes that "C:\Temp\MyProject.ipt"
exists.

 

oFileLocations.FileLocationsFile =
"F:\Global-Edit.ipj"

 

 

 

' Show the workspace after making the project
change.

 

Debug.Print ("Workspace: " &
oFileLocations.Workspace)

 

End Sub
0 Likes
Message 3 of 9

Anonymous
Not applicable
Thanks Sanjay, I got it to work.
0 Likes
Message 4 of 9

Anonymous
Not applicable
Sanjay,

Here's a similar problem with the following code that is straight forward in VBA but expects something else with .net.

Public Sub ActiveDocumentSample()
' Declare a variable to handle a reference to a document.
Dim invDoc As Document
' Set a reference to the active document.
Set invDoc = ThisApplication.ActiveDocument
MsgBox "Got document: " & invDoc.DisplayName
End Sub

The "ThisApplication.ActiveDocument" portion gives me the following error:

Implicit conversion from "Object" to "Inventor Document"

I'm not sure how to cast this object.

Thx,
Jon
0 Likes
Message 5 of 9

Anonymous
Not applicable

Hi Jon,

 

ThisApplication is a property that returns
Inventor's Application object and is ONLY AVAILABLE FROM WITHIN
INVENTOR'S VBA ENVIRONMENT.  If you're using Inventor's API from anywhere
else besides Inventor's VBA you have to get access to the Application object in
a different way.  If you're writing an Add-In you can get it during the
Activate method through the Application property of the ApplicationAddInSite
object that's passed as the first argument of the Active method.  If you're
writing an EXE program then you can use the VB GetObject function.
--

Brian Ekins
Autodesk Inventor API

href="http://blogs.autodesk.com/modthemachine">http://blogs.autodesk.com/modthemachine
0 Likes
Message 6 of 9

Anonymous
Not applicable
Hi Brian,

This code is probably a bad example concerning the context of what I'm trying to do. Let me explain what I'm try to do.

Before I set a project in Apprentice I want to verify there are no Inventor documents open, which means Inventor could be active. I understand Inventor's Application object and how it is available from within VBA only. However, is there ever a time when the Application object can be accessed in different ways Within the same software application? For example, before I set my project I may want to check to see if a document is active before I run my routine. Though my application is Apprentice, wouldn't I have to use Inventor's Application object to check if a document is open in Inventor? Because obviously Apprentice doesn't have activeDocument, though I have tried some things (unsuccessfully). Perhaps I should just make sure Inventor isn't running, but I can't always be sure the end user will know to shut it down and, therefore, could potentially hose an assembly drawing. Maybe I'm fussing about nothing.

Thx,
Jon
0 Likes
Message 7 of 9

Anonymous
Not applicable
At any rate, the ApplicationAddInSite object is what I hadn't thought of during this process.
0 Likes
Message 8 of 9

Anonymous
Not applicable
Brian,

I solved it with the following. Maybe you have a better idea, but it works. I wouldn't have known without your input.

Dim oApp As Inventor.Application

Try
oApp = CType(GetObject(, "Inventor.Application"), Inventor.Application)
oApp.Documents.CloseAll()
Catch ex As Exception
Exit Try
End Try

Thx,

Jon
0 Likes
Message 9 of 9

Anonymous
Not applicable

GetObject is what I would have used to.
--

Brian Ekins
Autodesk Inventor API

href="http://blogs.autodesk.com/modthemachine">http://blogs.autodesk.com/modthemachine


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
Brian,
I solved it with the following. Maybe you have a better idea, but it works. I
wouldn't have known without your input. Dim oApp As Inventor.Application Try
oApp = CType(GetObject(, "Inventor.Application"), Inventor.Application)
oApp.Documents.CloseAll() Catch ex As Exception Exit Try End Try Thx,
Jon
0 Likes