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
Solved! Go to Solution.
You could try this
Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView()
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.
Won't the Excel application have a means to minimize the application window, thus restoring the AutoCAD application to the foreground?
There are some ideas posted here
if you set the excel.application visible = false does this do anything helpful?
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.
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.
I will also ask if you are using perimeter lines for border lines in your sheet, especially double lines?
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
Yes I have had some torubles closing excel clopletely. Thanks for the extra code to close it down.
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)
Another option that works in AutoCAD 2014 :
http://adndevblog.typepad.com/autocad/2013/03/use-of-windowfocus-in-autocad-2014.html
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
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.