Gera
Gera

Reputation: 31

How to use Workbook Object

I want just get the workbook name. Why VBA returns a error when i try use Workbook´s Name property directly, like this:

MsgBox "workbook name = " & Workbook.Name

The compiler says: Variable not declared. Workbook is an object. Why do i have to declare a variable associated to it?

These work:

MsgBox "workbook name = " & ThisWorkbook.Name

MsgBox "workbook name = " & ActiveWorkbook.Name

Why do I have to use Application properties (ThisWorkbook and ActiveWorkbook) instead of Workbook.Name directly?

Upvotes: 1

Views: 166

Answers (1)

gaurav5430
gaurav5430

Reputation: 13882

Workbook is not an object

Workbook is the class which represents all the workbooks,

(for more details: http://www.functionx.com/vbaexcel/topics/workbooks.htm)

and ThisWorkbook(or ActiveWorkbook) is the instance

classes don't have values for properties (except static) .i.e. in other words, you cannot access instance variables through a class.

i hope you understand.

Upvotes: 3

Related Questions