Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open Dwg and run a script - Excel to Draftsight

Status
Not open for further replies.

Cheyenne2018

Computer
Apr 11, 2018
4
Hi,

I need to open a dwg file, run a script (.scr) and close Draftsight. I'm started with open the document.I found this code from jrice174 and I've tried to edit for Draftsight:

Code:
Sub openDraftsight()

Dim dsApp As Object
Dim NewFile As Object
Dim dsAppPath As String
Dim bReadOnly As Boolean


    On Error Resume Next
    Set dsApp = GetObject(, "DraftSight.Application")
    If (Err <> 0) Then
        Err.Clear
        Set dsApp = CreateObject("DraftSight.Application")
        If (Err <> 0) Then
            MsgBox "Could Not Load DraftSight!", vbExclamation
            End
        End If
    End If
    
    'If you want to see AutoCAD on screen
    dsApp.Visible = True
    
    MydsAppPath = "C:\test.dwg"
    bReadOnly = True
    'Set NewFile = Draftsight.Documents.Open(MydsAppPath, bReadOnly)
    
    If (NewFile Is Nothing) Then
ErrorMessage:
        If NewFile = "False" Then End
            MsgBox "Could not find the required spreadsheet that should be located at" & vbCr & MydsAppPath & vbCr & "Please rename or relocate the specified file as needed."
        End
    End If
    
    'Close Draftsight Process
    'dsApp.Quit
    
    Set dsApp = Nothing
    Set NewFile = Nothing

End Sub

Return me a error: method or data member not found to
Code:
Documents

If I comment the line
Code:
Set NewFile = Draftsight.Documents.Open(MydsAppPath, bReadOnly)
Draftsight start but without opening the test.dwg


Thanks for the help..



 
Replies continue below

Recommended for you

Hi,

You created the Draftsight application object, dsApp.

Then you TOTALLY disregard that object!
Code:
Set NewFile = [s]Draftsight[/s] [b]dsApp[/b].Documents.Open(MydsAppPath, bReadOnly)

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Dude... I already told you (on a different forum) that you can't do this without DraftSight Professional. Yes, you can run scr files inside of DraftSight using the free version, but you can't use API calls from VBA to open files and run commands.

 
SkipVought said:
You created the Draftsight application object, dsApp.

Thanks for the help, I'm not an expert of Vba! I changed as you suggested. Now Draftsight run without error but without opening the file "test.dwg"

handleman said:
but you can't use API calls from VBA to open files and run commands

I hope there is a secondary option! Meanwhile it's fine for me to open the file...
 
The only thing you're going to be able to do with this method is open DraftSight. You will not be able to open the file or execute the .scr through VBA.

Another approach that will get you one step further would be to tell Windows to open the .dwg file using DraftSight. This would be accomplished with a shell command:

Shell """[whatever the path is to the DraftSight .exe]"" ""[whatever the path is to your .dwg]""", 1

You will still have to manually run the .scr file inside DraftSight, and manually save and close etc.

 
Thanks for the suggestion [bigsmile] Now I'm using the professional ( in trail mode) and even in this case Draftsight starting without opening the specific file...I don't know where's my mistake..
 
You will have to "speak" Draftsight's "Language". Every program that you automate with VBA has certain functions and calls that are specific to that program.

You have the line:
Set NewFile = Draftsight.Documents.Open(MydsAppPath, bReadOnly)
However, this is the syntax for either AutoCAD or Excel, which you've copied this macro from.

When you tell ACAD/XL (whichever it was) ".Documents.Open....", that's its language. It knows what to do with that.
I don't know what DraftSight's API calls are for opening a document. You need to look at DraftSight API documentation for that.

 
You are using late binding, so IDE will not help enumerate methods and members. You may be able to use object explorer to find methods and members exposed from your created object.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor