.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Setting focus back to AutoCAD

13 REPLIES 13
SOLVED
Reply
Message 1 of 14
coralie.jacobi
4105 Views, 13 Replies

Setting focus back to AutoCAD

I have written a VB.NET function that exports data from AutoCAD and imports it into a new Excel worksheet.

The code runs as I want it to, however, when all the data has been added to Excel, I want the control to go back to AutoCAD to display a msgbox that the routine has finished.

 

The msgbox comes up, but it does not put focus back to AutoCAD. If the excel window is ontop of AutoCAD, the user has no idea when the routine is finished.

 

Any ideas how to do this would be much appreciated?

 

cj

13 REPLIES 13
Message 2 of 14
arcticad
in reply to: coralie.jacobi

 You could try this

 

Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView()

---------------------------



(defun botsbuildbots() (botsbuildbots))
Message 3 of 14
coralie.jacobi
in reply to: arcticad

Thanks for the response.

 

I added that into my code just before the MsgBox. Still no go.

 

In the code I'm adding rows into Excel so Excel is the active window. But when the routine is done, it still sits with Excel as the active window....I figured the MsgBox should make Autocad the active window.....even with your added suggestion, it does not bring it infront of Excel.

Message 4 of 14
dgorsman
in reply to: coralie.jacobi

Won't the Excel application have a means to minimize the application window, thus restoring the AutoCAD application to the foreground?

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


Message 5 of 14
arcticad
in reply to: dgorsman

There are some ideas posted here

 

http://stackoverflow.com/questions/2315561/correct-way-in-net-to-switch-the-focus-to-another-applica...

 

if you set the excel.application visible = false does this do anything helpful?

---------------------------



(defun botsbuildbots() (botsbuildbots))
Message 6 of 14
coralie.jacobi
in reply to: arcticad

thanks...I will check that link out.

 

Your sugestion of

myExcel.Application.Visible = False

 

Closes Excel right down....doesn't even ask to save the file.

Message 7 of 14
jaboone
in reply to: arcticad

I have done extensive work with Excel and have some good code for running it.  First detect if it is running, then...

 

   Private MyXL As Microsoft.Office.Interop.Excel.Application 'Excel.Application
   Private objWorkbook As Microsoft.Office.Interop.Excel._Workbook 'Excel.Workbook
   Private objWorksheet As Microsoft.Office.Interop.Excel._Worksheet 'Excel.Worksheet

Function 
... ... ... ... in closing the function: If PrintFooter = True Then objWorksheet.PageSetup.LeftFooter = System.DateTime.Today.Date objWorksheet.PageSetup.RightFooter = System.DateTime.Now.TimeOfDay.ToString ' & ":" & System.DateTime.Now.Minute.ToString ' objWorksheet.PageSetup.PrintTitleRows End If objWorksheet.PrintPreview() On Error Resume Next MyXL.Visible = True MyXL.DisplayAlerts = True If ExcelWasNotRunning = True Then objWorksheet = Nothing MyXL.Application.Quit() End If 'Free up memory, otherwise there will be a memory leak. objWorksheet = Nothing objWorkbook = Nothing MyXL = Nothing 'Screen.MousePointer = vbDefault Exit Sub

 closing out of excel seems to be causing a problem.

Learning as I go
Message 8 of 14
jaboone
in reply to: jaboone

I will also ask if you are using perimeter lines for border lines in your sheet, especially double lines?

Learning as I go
Message 9 of 14
arcticad
in reply to: jaboone

Excel has a known bug about closing down.

 

The attached code will force Excel to close.

 

The visible.false will not close excel, it is still running. You just can't see the interface, I was hoping it would switch back to AutoCAD when you tried it

 

 

---------------------------



(defun botsbuildbots() (botsbuildbots))
Message 10 of 14
jaboone
in reply to: arcticad

Yes I have had some torubles closing excel clopletely.  Thanks for the extra code to close it down.

Learning as I go
Message 11 of 14
Mikanikal
in reply to: jaboone

I haven't looked into the possibility to doing this through AutoCAD api but you could test using a Win32 API call.

 

This is one of the options posted in arcticad's reply. This just shows you which AutoCAD property to use. This is untested fyi.

 

Declare Auto Funtion SetForeGroundWindow Lib "User32.dll"(ByVal Hwnd As IntPtr) As Long

'Call these API's through your code

SetForeGroundWindow(Autodesk.AutoCAD.ApplicationServices.Application.MainWindow.Handle)

 

Message 12 of 14
Balaji_Ram
in reply to: coralie.jacobi

Another option that works in AutoCAD 2014 :

 

http://adndevblog.typepad.com/autocad/2013/03/use-of-windowfocus-in-autocad-2014.html



Balaji
Developer Technical Services
Autodesk Developer Network

Message 13 of 14
coralie.jacobi
in reply to: Balaji_Ram

I had to set this aside for a bit....I am only a part time programmer. Soooo, I had some time to try the last suggestion. I am working in VB not C# so I converted the code but get an error on the "Dynamic" type.

 

Dim acaddoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = acaddoc.Editor
        Dim acadDocObj As Dynamic = acaddoc.GetAcadDocument()

 My code does not like the Dynamic object???? I am running VS2010 with AutoCAD2014.

 

Any suggestions on what I'm doing wrong?

 

thanks

 

cj

Message 14 of 14

Well I tried something different and got an appropriate solution. I added the following before my MsgBox.

Excel now minimizes and then Autocad is the main visible application showing my message to the user.

 

Not sure why I didn't find this earlier but sometime a step away brings new ideas when you return.

 

thanks to everyone who provided responses.

 

cj

 

myExcel.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMinimized

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost