Home > Software engineering >  VB call Excel program return activity workbook
VB call Excel program return activity workbook

Time:11-10

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

CodePudding user response:

reference weixin_40806216 reply: 3/f
thanks to the great god reply, is there any way to direct call to the open workbook, and don't need to add or open to choose

Can ah, can not find open the Excel is tip:
 
The set xlsapp=nothing
On the error resume next
The Set xlsApp=GetObject (, "Excel. Application")
On error goto 0
If xlsapp is nothing then
Msgbox "found no open Excel instance!"
The else
The Set wb=xlsApp. ActiveWorkbook
Msgbox wb. Name
End the if

CodePudding user response:

Thank you, it is still don't understand why getobject can't return to open the excel program

CodePudding user response:

reference 5 floor weixin_40806216 reply:
, is still don't understand why getobject can't return to open the excel program


I Copy your code directly tested, no problem,

Open the VB6, create a new standard EXE engineering, draw a button, Ctrl + V process code;
And then just add 1 as statements of "verification",
Grab a Excel document to open, back in VB6 run...
Authentication conclusion is "completely normal"!!!!!!

CodePudding user response:

Don't know whether the system problem, tried a few times, have no way to back to excel by getobject program

CodePudding user response:

After accidentally run and generated EXE files, programs use no problem, program debugging is invalid, concrete reason and solution for light

CodePudding user response:

Or still upload the code, you can look at convenient guide to you
  •  Tags:  
  • VBA
  • Related