.NET

Reply
Valued Contributor
cjacobi
Posts: 55
Registered: ‎11-12-2012
Message 1 of 14 (923 Views)
Accepted Solution

Setting focus back to AutoCAD

923 Views, 13 Replies
08-14-2013 01:57 PM

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

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

 

*Expert Elite*
arcticad
Posts: 1,286
Registered: ‎06-21-2004
Message 2 of 14 (921 Views)

Re: Setting focus back to AutoCAD

08-14-2013 02:15 PM in reply to: cjacobi

 You could try this

 

Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView()

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



(defun botsbuildbots() (botsbuildbots))
Valued Contributor
cjacobi
Posts: 55
Registered: ‎11-12-2012
Message 3 of 14 (888 Views)

Re: Setting focus back to AutoCAD

08-15-2013 10:38 AM 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.

*Expert Elite*
dgorsman
Posts: 5,698
Registered: ‎10-12-2006
Message 4 of 14 (879 Views)

Re: Setting focus back to AutoCAD

08-15-2013 10:58 AM in reply to: cjacobi

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.
Adopt. Adapt. Overcome. Or be overcome.
A good question will be halfway to a good answer.


*Expert Elite*
arcticad
Posts: 1,286
Registered: ‎06-21-2004
Message 5 of 14 (876 Views)

Re: Setting focus back to AutoCAD

08-15-2013 11:29 AM 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))
Valued Contributor
cjacobi
Posts: 55
Registered: ‎11-12-2012
Message 6 of 14 (871 Views)

Re: Setting focus back to AutoCAD

08-15-2013 11:37 AM 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.

Valued Contributor
jaboone
Posts: 91
Registered: ‎12-05-2011
Message 7 of 14 (869 Views)

Re: Setting focus back to AutoCAD

08-15-2013 11:42 AM 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
Valued Contributor
jaboone
Posts: 91
Registered: ‎12-05-2011
Message 8 of 14 (864 Views)

Re: Setting focus back to AutoCAD

08-15-2013 11:51 AM 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
*Expert Elite*
arcticad
Posts: 1,286
Registered: ‎06-21-2004
Message 9 of 14 (859 Views)

Re: Setting focus back to AutoCAD

08-15-2013 11:53 AM 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))
Valued Contributor
jaboone
Posts: 91
Registered: ‎12-05-2011
Message 10 of 14 (845 Views)

Re: Setting focus back to AutoCAD

08-15-2013 12:50 PM 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
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.