Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
WCrihfield
in reply to: mrako

Usually when you create a new instance of the Excel application by code, it will not already have a Workbook open.  There is a checkbox setting within Excel > File menu > Options > General > (under the "Start up options" heading) > called "Show the Start screen when this application starts".  This will allow you to skip the splash screen and/or the screen that would normally pop-up asking you to select either a new blank workbook or start one from a template, or open an existing one, when you manually start up Excel, but I don't believe it will behave this way by default.

Also, there is a setting within Excel > File menu > Options > General > (under the "When creating new workbooks" heading) > where you can set "Include this many sheets:".  This can only be set between 1 and 255, so 0 is not an option.  However, this setting may only effect when you create a Workbook through manual means, and may not have any effect when creating one by code.

 

What you can do before attempting to get/set the Workbook variable is check If the Workbooks.Count property = 0 (then Add one), else get either the ActiveWorkbook or the first one.

Dim oWB As Workbook
If oExcel.Workbooks.Count = 0 Then
	oWB = oExcel.Workbooks.Add()
Else
	oWB = oExcel.Workbooks.Item(1)
	'or
	'oWB = oExcel.ActiveWorkbook
End If

Same with the Worksheets and specifying your Worksheet variable.  You can check the Worksheets.Count to see if it is greater than 0.  If the count = 0 then, Add one, else Get either the first one or the ActiveSheet.

Dim oWS As Worksheet
If oWB.Worksheets.Count = 0 Then
	oWS = oWB.Worksheets.Add
Else
	oWS = oWB.ActiveSheet
	'or
	'oWS = oWB.Worksheets..Item(1)
End If

 

Also, if you have two version of Excel installed on your machine, it may be opening the wrong version.  There are ways to open a specific application, by specifying its full path and file name, if that's what you need.  Since I only have one version of Excel installed, I usually start it for my code with the following line:

Dim oExcel As New Microsoft.Office.Interop.Excel.ApplicationClass
oExcel.DisplayAlerts = False
oExcel.Visible = True

But you can use the Process route too, which you may need to do if it's opening the wrong version.

   Similarly, in Inventor, there are many actions that when done manually, there are often intuitive helpers listening to your actions in the background that will help you do what you're trying to do, but then when you try to do the same thing by code, you often have to specify more details and or settings to accomplish the same thing.

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' :thumbs_up:.

If you have time, please... Vote For My IDEAS :light_bulb:and Explore My CONTRIBUTIONS

Inventor 2020 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum

Wesley Crihfield

EESignature

(Not an Autodesk Employee)