Home > Software engineering >  For help
For help

Time:10-31

I use the following code in VB to create and save the workbook, but save time why there are two workbook?
Dim xlApp As Excel. Application
Dim xlBook As Excel. The Workbook
Dim xlChar As New Excel. Chart
Dim xlSheet As New Excel. The Worksheet

'Set xlApp=New Excel. Application
The Set xlApp=CreateObject (" Excel. Application ")
XlApp. Visible=True 'is critical, Visible shows the read and write excel Visible and
XlApp. Workbooks. Add
XlApp. Worksheets. The Add Before:=Sheets (3)
ActiveSheet. Name="ca"
ActiveWorkbook. SaveAs "D: \ CCC. Xlsx"

XlApp. Quit
The Set xlApp=Nothing
End
End Sub

CodePudding user response:

Itself is a, you have a new one, not two?

CodePudding user response:

Chaos object relationships!

Project refers to the Excel, if the first call to the global object repository (must ActiveSheet ActiveWorkbook, etc.) will automatically default instance corresponding to the first Excel example,
And you both Excel instance does exist, has built an instance xlApp,
So:
A) if the had not Excel as an example, the default instance is xlApp, ActiveSheet and ActiveWorkbook operation is A new workbook, that is only one book,
B) if the original is Excel as an example, the default instance is not xlApp, ActiveSheet and ActiveWorkbook is the original of the operation the Excel workbook, or at least two workbook,

CodePudding user response:

Thank you very much, but if the original some instances should how to delete

CodePudding user response:

Has nothing to do with the instance,
You can't operate ActiveSheet, ActiveWorkbook, there is uncertainty,
Through xlApp subordinate objects to operate is ok,

CodePudding user response:

Dim xlSheet As New Excel. The Worksheet
The NEW
  •  Tags:  
  • VBA
  • Related