Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Problem with VBA opening an existing drawing.

I have been attempting to open an existing drawing, via a VBA routine in Excel. I am successfully getting VBA to reach BricsCAD, since both BricsCAD and Excel are being run as administrator. However, I can't seem to get the method to open an existing drawing to work.

Here is the example I found at AutoDesk's web site.

Sub Ch3_OpenDrawing()
  Dim dwgName As String
  dwgName = "c:\campus.dwg"
  If Dir(dwgName) <> "" Then
    ThisDrawing.Application.Documents.Open dwgName
  Else
    MsgBox "File " & dwgName & " does not exist."
  End If
End Sub

Below is my failed attempt to use it. The line that generates the error has a remark describing the error. [for some reason I have never been able to reliably get the code formatting to work, or at least to stop all the automatic formatting! Ignore any code formatting below- the forum just doesn't work correctly. ]

==========begin code===========================
Option Explicit

'Global Variables
Public acadApp As AcadApplication
Public acadDoc As AcadDocument

Sub OpenDrawing()

'Reset Global Variables
Set acadDoc = Nothing
Set acadApp = Nothing

'**** Declare Local Variables ****
Dim WorkingDir As String
Dim TemplateDwgName As String
Dim TempString As String

'*** Set Variables ****
'Set constants
WorkingDir = "D:\temp\"
TemplateDwgName = "Test.dwg"

'*** Connect to BricsCAD ****
'if autocad is open, obtain a reference to the autocad application
'if autocad is not open, open autocad and obtain a reference to it
On Error Resume Next
Set acadApp = GetObject(, "BricscadApp.AcadApplication")
'if autocad not running then error - component cannot create object
If acadApp Is Nothing Then
Set acadApp = New AcadApplication
acadApp.Visible = True
End If

'Let me know if the program could not start or connect to BricsCAD
If acadApp Is Nothing Then
MsgBox "BricsCAD did not start"
Exit Sub
End If
On Error GoTo 0

'Open the drawing.
TempString = WorkingDir & TemplateDwgName
If Dir(TempString) <> "" Then
acadDoc.Application.Documents.Open TempString '<- Generates Run-time Error 91: Object variable or With block variable not set.
Else
MsgBox "File " & TempString & " does not exist."
End If

On Error GoTo 0

'Change the focus to BricsCAD.
AppActivate "BricsCAD"

End Sub
==========end code===========================

Thank you for any advice.
-joe

Comments

  • You have:

    Set acadDoc = Nothing
    ...
    acadDoc.Application.Documents.Open TempString
    This obviously can't work.

    Try using:
    acadApp.Documents.Open TempString

  • Thank you, that worked. I though I had tried that version before, but it failed. It can be hard to remember all the variations I tried that had failed.

    I will also mention for any future readers that my effort to merge the path and file name together into one variable, before using it in the acadApp.Documents.Open TempString statement was unnecessary. I had done this in an attempt to get it to work, and tried to make the as much like the example I found on the internet.
    acadApp.Documents.Open WorkingDir & TemplateDwgName works fine.

    -Joe

This discussion has been closed.
Origami
Origami is the Japanese word for paper folding. ORI means to fold and KAMI means paper and involves the creation of paper forms usually entirely by folding.

Powered by VanillaForums, Designed by Steam