VBA set Active Document not working?

VBA set Active Document not working?

Anonymous
Not applicable
6,582 Views
6 Replies
Message 1 of 7

VBA set Active Document not working?

Anonymous
Not applicable

Hello,

 

I have inherited a project with some AutoCAD VBA code that works fine with AutoCAD 2013. However, I'm trying to upgrade this project to AutoCAD 2015 as a temporary solution and am running into a problem trying to set the Active Document. (Both AutoCAD versions are 32 bit)

 

When all code is the same, this statement works in AutoCAD 2013, but not in AutoCAD 2015:

ThisDrawing.Application.Documents.Item(1).Activate

 

 

The same is true for the following statement:

ThisDrawing.Application.ActiveDocument = AcadApplication.Documents.Item(1)

 

Is there another way to perform this action for AutoCad 2015? 

 

thank you in advance!

 

Patrick

0 Likes
Accepted solutions (1)
6,583 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable

what do you mean by "does not work", exactly?

 

if you have to activate a document and have the user select any element by means of some GetXXX() method, then there seems to be an AutoCAD issue relating from 2015 version up (see this thread)

 

if that is not your case, then you may want to add more details about your actual need (why do you need to activate a document? what doesn't work?) and see if anybody here could help

0 Likes
Message 3 of 7

Anonymous
Not applicable

 

Some more details on the issue...

Here is some sample code that I ran on both AutoCAD 2013 and AutoCAD 2015. You can see the differences in outputs. It should be noted that there is an 'On error Resume Next', which is used on one line in AutoCAD 2013 only and identified in a comment...

 

    On error resume next

    Dim NewDwg As AcadDocument
    Dim sPath As String
    sPath = "c:\airecad201x\templates\ICD_BomTemplate.dwt"
    'Initial Values
    Debug.Print "1) ThisDrawing.Application.ActiveDocument.Name = " & ThisDrawing.Application.ActiveDocument.Name
    Debug.Print "1) AcadApplication.Documents.Count = " & AcadApplication.Documents.count
    Debug.Print "1) ThisDrawing.Application.Documents.Item(0) = " & ThisDrawing.Application.Documents.Item(0).Name
   
    Set NewDwg = AcadApplication.Documents.Add(sPath)
    Debug.Print " 2) NewDwg.Name = " & NewDwg.Name
    Debug.Print " 2) ThisDrawing.Application.ActiveDocument.Name = " & ThisDrawing.Application.ActiveDocument.Name
    Debug.Print " 2) AcadApplication.Documents.Count = " & AcadApplication.Documents.count
    Debug.Print " 2) ThisDrawing.Application.Documents.Item(0) = " & ThisDrawing.Application.Documents.Item(0).Name
    Debug.Print " 2) ThisDrawing.Application.Documents.Item(1) = " & ThisDrawing.Application.Documents.Item(1).Name
   
    ThisDrawing.Application.ActiveDocument = NewDwg       'On error resume next' - needed for this line in 2013. Line may not be needed in 2013?
    Debug.Print "  3) ThisDrawing.Application.ActiveDocument.Name = " & ThisDrawing.Application.ActiveDocument.Name
   
    ThisDrawing.Application.ActiveDocument = AcadApplication.Documents.Item(1)
    Debug.Print "   4) ThisDrawing.Application.ActiveDocument.Name = " & ThisDrawing.Application.ActiveDocument.Name
   
    ThisDrawing.Application.Documents.Item(1).Activate
    Debug.Print "    5) ThisDrawing.Application.ActiveDocument.Name = " & ThisDrawing.Application.ActiveDocument.Name
   
    ThisDrawing.Application.Documents.Item(0).Activate
    Debug.Print "     6a) ThisDrawing.Application.ActiveDocument.Name = " & ThisDrawing.Application.ActiveDocument.Name
   
    ThisDrawing.Application.Documents.Item(1).Activate
    Debug.Print "     6b) ThisDrawing.Application.ActiveDocument.Name = " & ThisDrawing.Application.ActiveDocument.Name

 

 

AutoCAD 2013 Output

1) ThisDrawing.Application.ActiveDocument.Name = Drawing1.dwg
1) AcadApplication.Documents.Count = 1
1) ThisDrawing.Application.Documents.Item(0) = Drawing1.dwg
 2) NewDwg.Name = Drawing2.dwg
 2) ThisDrawing.Application.ActiveDocument.Name = Drawing2.dwg
 2) AcadApplication.Documents.Count = 2
 2) ThisDrawing.Application.Documents.Item(0) = Drawing1.dwg
 2) ThisDrawing.Application.Documents.Item(1) = Drawing2.dwg
  3) ThisDrawing.Application.ActiveDocument.Name = Drawing2.dwg
   4) ThisDrawing.Application.ActiveDocument.Name = Drawing2.dwg
    5) ThisDrawing.Application.ActiveDocument.Name = Drawing2.dwg
     6a) ThisDrawing.Application.ActiveDocument.Name = Drawing1.dwg
     6b) ThisDrawing.Application.ActiveDocument.Name = Drawing2.dwg

 

AutoCAD 2015 Output

1) ThisDrawing.Application.ActiveDocument.Name = Drawing1.dwg
1) AcadApplication.Documents.Count = 1
1) ThisDrawing.Application.Documents.Item(0) = Drawing1.dwg
 2) NewDwg.Name = Drawing2.dwg
 2) ThisDrawing.Application.ActiveDocument.Name = Drawing1.dwg
 2) AcadApplication.Documents.Count = 2
 2) ThisDrawing.Application.Documents.Item(0) = Drawing1.dwg
 2) ThisDrawing.Application.Documents.Item(1) = Drawing2.dwg
  3) ThisDrawing.Application.ActiveDocument.Name = Drawing1.dwg
   4) ThisDrawing.Application.ActiveDocument.Name = Drawing1.dwg
    5) ThisDrawing.Application.ActiveDocument.Name = Drawing1.dwg
     6a) ThisDrawing.Application.ActiveDocument.Name = Drawing1.dwg
     6b) ThisDrawing.Application.ActiveDocument.Name = Drawing1.dwg

 

 

AutoCAD 2013 responds to setting the active document, and AutoCAD 2015 does not in my example. Thanks for the help...

 

 

 

0 Likes
Message 4 of 7

Anonymous
Not applicable
Accepted solution

 

I have found that if you try to close the file immediately after opening, ACAD2015 will go ahead and set this document as the active document...

 

 

For a new document:

 

On error resume next

Set NewDwg = AcadApplication.Documents.Add(sPath)

ThisDrawing.Application.ActiveDocument = NewDwg

ThisDrawing.Application.Documents.Item(ThisDrawing.Application.Documents.count - 1).Close (False) 'This line fails, but is then set to the active doc.

 

To Switch documents:

 

On error resume next

ThisDrawing.Application.Documents.Item(1).Activate

ThisDrawing.Application.Documents.Item(1).Close (False) 'This line fails, but it then sets to be the active document.

 

Okay so this is a real ugly fix... Is there a better way?

 

thanks,

Patrick

 

 

Message 5 of 7

Anonymous
Not applicable

Tried it in AutoCAD 2016 and unfortunately the file I open or select is properly closed with the command you've posted, no error is generated if I attempt to close the file immediately after opening.

 

Thanks for posting. Will hunt further for the solution to Activate the drawing in a way to be able to use GetPoint..

 

0 Likes
Message 6 of 7

Anonymous
Not applicable

Hi Patrick,

 

Just found the solution in AutoCAD2016, this might work in 2015 too without the ugly fix:

 

Switching between opened drawings does cause the GETPOINT error and the problem is the Activation of the drawing you switch to. 

 

Below I sketch the solution that works for me in my application to ALIGN one drawing towards coordinates that I pick from another (Base) drawing:

 

Switching between drawings whilst my application is running does not work (can't Activate the drawing that was not opened when I start my application)

 

 

So instead of opening all drawings I need to work with to ALIGN, I only open the drawing that is to be ALIGNed.

 

I start my application => use GETFILE to open the Base drawing (where to get the base points to ALIGN TO from) => Activate that drawing => use GETPOINT to gather the three points I need => send them to the Windows Clipboard => Close this Base drawing without saving => proceed in the original drawing to use GETPOINT to point where the 3 base points refer to => carry out the ALIGNment based on this info => Check the three points by placing circles on the original coordinates from the Clipboard for QC purposes => close my application.

 

Somehow AutoCAD 2016 allows to activate a newly loaded drawing as the Current one for GETPOINT to function and when I close this drawing the originally loaded drawing is automatically Activated to use GETPOINT in too.

 

This solves my problem! Hope it solves yours too, your post certainly set me in the right direction.

 

Thanks! Hope this helps.

 

0 Likes
Message 7 of 7

Anonymous
Not applicable

Based on a reply found elsewhere I tried simply sending a regen

ThisDrawing.SendCommand "regen" & vbLf

GetPoint works afterwards.

0 Likes