kuehso
kuehso

Reputation: 300

"object doesn't support this property or method" when using "print"

I'm kind of new to VBA (using it with Excel) and I'm currently going through some tutorials to get an idea of how it's different to Java oder C#. But that's not the question ... ;-)

This one tutorial tells me to use "Print" in the Form_Initialize-procedure (I'm using Office XP with VBA6 ... don't ask me why, it's not my decision :-P). But when I do so, it gives me the above mentioned error (or at least I guess that the error must read something like this in english, my version is german :D).

Here's what I'm trying to do in an extremely easy example:

Private Sub UserForm_Initialize()
   Print "Hello World!"
End Sub

Following the tutorial, this should print "Hello World" to the "surface" of the form about to be initialized... Following the help (F1), it should do just about the same...
But it doesn't ;-)

Anybody got any idea where I'm wrong? I don't get it ...

Thanks in advance and greetings
gilaras

================================UPDATE================================
The tutorial tells me to write

Print " a", "      b", "a*b", "a+b", "Int(a/b)"

This should give me a tabular kind of expression on the form.
Is that even possible using a TextBox?

Upvotes: 1

Views: 2053

Answers (3)

Potter Rafed
Potter Rafed

Reputation: 481

You can print in the immediate window/console (ctr+g) by using debug.print http://www.cpearson.com/excel/DebuggingVBA.aspx

Upvotes: 1

Kasnady
Kasnady

Reputation: 2279

VBA not same with Win32 app. Using print is old version, now is not same. If you want to print something to the surface, you must add a thing to write it. Like...

Add a Textbox(Object) from the Toolbox to the Form, add a label(object) too Then your textbox default name is Textbox1 and label default name is Label1

So, on your code :

Private Sub UserForm_Initialize()
   textbox1.text = "Hello World"
Label1.text = "Hello World"
End Sub

Upvotes: 1

Peter Albert
Peter Albert

Reputation: 17495

You need insert some kind of control to the user form in which you place your text. E.g. place a control of the type Caption and name it Caption1(in the properties list F4).

Then you can assign a value with:

Me.Caption1 = "Hello world!"

Alternatively, depending on what you need, you can use Debug.Print "Hello world!" to simply output the string in the debug console (only helpful during development) - or use MsgBox "Hello world!" to show a message to the user. In this case you need no form at all, but simply need to trigger the macro, e.g. from button in Excel.

Upvotes: 4

Related Questions