Home > Software engineering >  Vb traverse the current name has open the Excel workbook
Vb traverse the current name has open the Excel workbook

Time:09-16

Dim WBK As Excel. The Workbook
Dim the WBS As New Application
Private Sub Command1_Click ()
For Each WBK In WBS. Workbooks
MsgBox WBK. Name
Next
End Sub
Use the above code can return to normal when the first running open work thin name.. But now it is not. Please help solve the where in addition to the problem.

CodePudding user response:

Don't think this code has a problem,

CodePudding user response:

Just say "no", who knows what the problem is!!!!!!

You can't have any abnormal reaction, or an error message, such as what's going on???????

CodePudding user response:

Quote: refer to the second floor Chen8013 response:



This code has opened 3 Excel in erection work thin case, running thin name, did not return to work, namely the WBS. Workbooks. Count normal should be=3=0

CodePudding user response:

Do you understand the office object model is a little deviation,
Excel. Application can't tube already open workbook, can only control by this ExcelApplication open or create the workbook,
Before the code to run open the excel and WBS object no relationship,

CodePudding user response:

reference 4 floor Runnerchin response:
you a little deviation on the understanding of the office object model,
Excel. Application can't tube already open workbook, can only control by this ExcelApplication open or create the workbook,
Before the code to run open the excel and WBS object no relationship,



But the actual process using this code or sometimes can not detected by normal ExcelApplication open or create the name of the workbook.. So this is my strange place

CodePudding user response:

 Dim WBK As Excel. The Workbook 
Private Sub Command1_Click ()
For Each WBK In Excel. Application. Workbooks
MsgBox WBK. Name
Next
End Sub

CodePudding user response:

refer to 6th floor zhao4zhong1 response:
 Dim WBK As Excel. The Workbook 
Private Sub Command1_Click ()
For Each WBK In Excel. Application. Workbooks
MsgBox WBK. Name
Next
End Sub


This test also not line, there is no return name

CodePudding user response:



This is not by Excel. Application open but run thin name can return to work

CodePudding user response:

 Private Sub Command1_Click () 
Dim WBK As Object
Dim app As Object
Set the app=CreateObject (" Excel. Application ")
App. Workbooks. The open c: \ "new \ project material attrition rate. XLSX"
If the app. Workbooks. Count & gt; 0 Then
For Each WBK In app. Workbooks
The Debug. Print WBK. Name
Next
The Else
The Debug. Print "No WorkBook!"
End the If
App. Workbooks. Close
Set the app=Nothing
End Sub

CodePudding user response:

Miss zhao today didn't copy and paste, fortunately, although you give a run at the code, but it is also a irrelevant answer,
If you must open the excel object, it can only use the GetObject to obtain, GetObject (file name) to get the excel object, through further obj. Application to get the Application object, and then return to come over to use app. Workbooks (" name ") to confirm whether already open (error is not open),
http://club.excelhome.net/thread-1217906-1-1.html

CodePudding user response:

Who said I to the code can run? !

CodePudding user response:


The GetObject function



Returns the ActiveX object from a file reference,

Syntax

GetObject ([the pathname] [class])

GetObject function grammar name parameters include the following:

Part of the description
The pathname optional; Variant (String), contains the full path and name of the object to retrieve the file, if you omit the pathname, the class is required,
The class optional; The Variant (String), on behalf of the object of a class String,


Among them, the format of the grammar of the class parameter for appname. Objecttype, and various parts of the syntax is as follows:

Part of the description
Appname required; The Variant (String), provide the object of the application name,
The objecttype required; The Variant (String), to create the object type or class,


Description

Use the ActiveX object in the GetObject function can access the file, and the object can be assigned to object variables, you can use the Set statement will return to the GetObject object is assigned to a variable, such as:

Dim CADObject As Object
The Set CADObject=GetObject (" C: \ \ SCHEMA CAD CAD ")

When executing the above code, will start the application that is associated with the specified pathname, activation of an object with the specified file at the same time,

If the pathname is a zero length string (" "), the GetObject returns the specified type new object instance, if the pathname parameter is omitted, the GetObject returns the current activity of the specified type of object, if the current does not specify an object of type, will go wrong,

Some applications allow only part of the activation file, its method is the filename with an exclamation mark (!) And used to identify want to activate the file part of the string, information about how to create this kind of string, please refer to the application document for creating an object,

In graphics applications, for example, a figure in the stored in a file may have layers, you can use the following codes to activate the diagram is known as a SCHEMA. The CAD layer:

The Set LayerObject=GetObject (" C: \ \ SCHEMA CAD CAD! Layer3 ")

If you do not specify the object's class, automation will according to the file name provided to determine the start Application and object is activated, however, some files may support more than one object class, for example, images may support three different types of objects: the Application object, the Drawing object, and the Toolbar object, all of these are part of the same file, to illustrate what kind of object do you want to specific activation in the file, you should use this optional class parameter, for example:

Dim MyObject As Object
The Set MyObject=GetObject (" C: \ DRAWINGS \ SAMPLE DRW ", "FIGMENT. DRAWING")

In the example above, FIGMENT is a DRAWING application, the name of the DRAWING is it supports a object type,

After the object is activated, it can be used in the code, as defined by the object variable to refer to it, in the example above, you can use the MyObject object variables to access properties and methods of the new object, for example:

MyObject. Line 9, 90
MyObject. InsertText 9, 100, "Hello, world."
MyObject. SaveAs "C: \ DRAWINGS \ SAMPLE DRW"

Note when the object of the current for instance, or to create the loaded file object, use the GetObject function, if the object current haven't instance, or don't want to start the loaded file object, should use CreateObject function,

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related