Hi,
I'm really hoping somebody can help with this....
I have an Access database that contains code to extract information from AutoCAD and populate the database fields with it. Currently I have the 'acax18enu.tlb' AutoCAD 2010 type library referenced in the VBA project, but this causes a problem when the database is opened on a machine that doesn't have AutoCAD installed. (Obviously the type library does not exist on these machines, so the database will refuse to run because of the missing type library.)
What I am trying to do is late bind the library so that it is no longer loaded as a reference in the VBA project, but at run-time instead. Through other measures, this piece of code is unlikely to be executed on a machine without AutoCAD, and this way even if it is I can trap it and flag the problem to the user before it runs.
This is what I have so far, and it works fine. It opens AutoCAD if necessary and then opens the relevant drawing file. The problem comes after that...
If I then try to do anything further with the entities within the drawing file I strike problems. This next piece of code follows immediately after the above snippet, and the second line of this piece is where the trouble starts.
The line "If TypeOf objEnt Is AcadBlockReference Then" returns the error "Compile error: User-defined type not defined".
This is starting to get beyond the current limits of my VBA knowledge. It would appear to me that I have one of two conditions present. Either i) By late binding the AutoCAD 2010 Type Library VBA does not make available all elements of said library, or ii) By using the approach that I have shown above I am in fact not biinding the AutoCAD 2010 Type Library, but another library that has less functionality than the one that I thought I was using.
Do either of these ideas make sense to those who are more experienced than me? Or have I completely missed the boat? Should I be using a different approach altogether?
I'm really hoping somebody can help with this....
I have an Access database that contains code to extract information from AutoCAD and populate the database fields with it. Currently I have the 'acax18enu.tlb' AutoCAD 2010 type library referenced in the VBA project, but this causes a problem when the database is opened on a machine that doesn't have AutoCAD installed. (Obviously the type library does not exist on these machines, so the database will refuse to run because of the missing type library.)
What I am trying to do is late bind the library so that it is no longer loaded as a reference in the VBA project, but at run-time instead. Through other measures, this piece of code is unlikely to be executed on a machine without AutoCAD, and this way even if it is I can trap it and flag the problem to the user before it runs.
This is what I have so far, and it works fine. It opens AutoCAD if necessary and then opens the relevant drawing file. The problem comes after that...
Code:
Dim DB As DAO.Database
Dim RS As DAO.Recordset
Dim strSQL As String
Dim objAcadApp As Object
Dim strProjNo as string
Dim strDWGNo as string
Dim strDWGName As String
dim boolDWGPresent as Boolean
Dim strPath As String
Dim strFileExists As String
Me.ProjectNumber.SetFocus
strProjNo = Me.ProjectNumber.Text
Me.DWG.SetFocus
strDWGNo = Me.DWG.Text
If strProjNo <> "" Then
strDWGName = strDWGNo & "-" & strProjNo & ".dwg"
Else
strDWGName = strDWGNo & ".dwg"
End If
On Error Resume Next
Set objAcadApp = GetObject(, "AutoCAD.Application")
If Err <> 0 Then
strSQL = "SELECT txtAutoCADPath FROM tblLocalOptions WHERE PrimaryKey = 1"
Set DB = CurrentDb
Set RS = DB.OpenRecordset(strSQL)
On Error Resume Next
RetVal = ShellExecute(hwnd, "open", RS!txtAutoCADPath, "", _
"N:\DWG\", SW_SHOWMAXIMISED)
While objAcadApp.Name <> "AutoCAD"
Set objAcadApp = GetObject(, "AutoCAD.Application")
Wend
objAcadApp.Visible = True
objAcadApp.WindowState = acMax
End If
Set objAcadDocs = objAcadApp.Documents
For Each objEachDwg In objAcadDocs
If objEachDwg.Name = strDWGName Then
Set objThisDwg = objEachDwg
boolDWGPresent = 1
Exit For
Else
boolDWGPresent = 0
End If
Next
If boolDWGPresent = 0 Then
strPath = "N:\DWG\" & Format$(((Int(strDWGNo / 500)) * 500), "00000") & "-" & Format$(((Int(strDWGNo / 500)) * 500) + 499, "00000") & "\"
strFileExists = Dir(strPath & strDWGName)
If strFileExists <> "" Then
objAcadApp.Documents.Open (strPath & strDWGName)
Else
MsgBox "No Drawing File Exists.", vbCritical
Exit Sub
End If
Set objAcadDocs = objAcadApp.Documents
For Each objEachDwg In objAcadDocs
If objEachDwg.Name = strDWGName Then
Set objThisDwg = objEachDwg
Exit For
Else
End If
Next
Else
End If
objThisDwg.Activate
Set objLayout = objThisDwg.ActiveLayout
If I then try to do anything further with the entities within the drawing file I strike problems. This next piece of code follows immediately after the above snippet, and the second line of this piece is where the trouble starts.
Code:
For Each objEnt In objLayout.Block
If TypeOf objEnt Is AcadBlockReference Then
Set objBlock = objEnt
With objBlock
If .Name = "FOR_CONSTRUCTION" Then
strStatus = "For Construction"
Else
End If
End With
End If
Next
The line "If TypeOf objEnt Is AcadBlockReference Then" returns the error "Compile error: User-defined type not defined".
This is starting to get beyond the current limits of my VBA knowledge. It would appear to me that I have one of two conditions present. Either i) By late binding the AutoCAD 2010 Type Library VBA does not make available all elements of said library, or ii) By using the approach that I have shown above I am in fact not biinding the AutoCAD 2010 Type Library, but another library that has less functionality than the one that I thought I was using.
Do either of these ideas make sense to those who are more experienced than me? Or have I completely missed the boat? Should I be using a different approach altogether?