A visual basic program code, need to return the excel program of current active workbook, wb code returned now is nothing, for help how to modify the
Private Sub Command6_Click () Dim wb, SHT, RNG, xlsApp On the Error Resume Next The Set xlsApp=GetObject (, "Excel. Application") If xlsApp Is Nothing Then The Set xlsApp=CreateObject (" Excel. Application ") End the If The Set wb=xlsApp. ActiveWorkbook End Sub
Found in the process of program debugging timely open the excel program, still use createobject to create excel program, the returned xlsApp is a program that does not contain any workbook, how to break?
CodePudding user response:
Create Excel. The Application is equivalent to click the icon to Excel, you open the Excel, you need to do one thing at this time, or create a new workbook, or open an existing.xls files isn't it? So you want to call Application. Workbooks. The Add or create a new Application. The Workbooks. Open to open an existing workbook
CodePudding user response:
Give you change, but is VBS, you need to define variables, or remove the option explicit:
Xlsapp=null On the error resume next The Set xlsApp=GetObject (, "Excel. Application") On error goto 0 If isnull (xlsapp) then The Set xlsapp=createobject (" excel. Application ") Xlsapp. Visible=true Xlsapp. Workbooks. Add End the if The Set wb=xlsApp. ActiveWorkbook Msgbox wb. Name
CodePudding user response:
Thanks to the great god reply, is there any way to direct call to the current open workbook, and don't need to add or open to choose