Word variables - public? static?

G

Gaz uk

Hi

I'm sure there is a simple answer to this but I can't find it.

I have a word document that asks a question in the Private Sub
Document_open() and puts it in to a Variable. The same sub creates a Submit
Button that calls Public Sub cmdSubmit_Click(). The problem is the cmdSubmit
coding does not get the value given to the variable in the Document_open.

I have tried declaring the variables Public and Static and all that kind of
stuff (I think) but without success.

Thanks in advance

Gaz
 
D

Doug Robbins

Show us the code.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
G

Gaz uk

This isn't the exact code, but you get the idea......

Public vName

Private Sub CommandButton1_Click()
x = MsgBox(vName)
End Sub

Private Sub Document_Open()
vName = InputBox("Input Name")

Set vCmdSubmit =
Selection.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1")
With vCmdSubmit.OLEFormat.Object
.Name = "CommandButton1"
.Caption = "Submit"
.AutoSize = True

End With
End Sub


Thanks

Gaz
 
J

Jean-Guy Marcil

Gaz uk was telling us:
Gaz uk nous racontait que :
This isn't the exact code, but you get the idea......

Public vName

Private Sub CommandButton1_Click()
x = MsgBox(vName)
End Sub

Private Sub Document_Open()
vName = InputBox("Input Name")

Set vCmdSubmit =
Selection.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1")
With vCmdSubmit.OLEFormat.Object
.Name = "CommandButton1"
.Caption = "Submit"
.AutoSize = True

End With
End Sub

Where is Public vName declared (In which module)?
From your "inexact" code, it looks like it is declared in the userform code
module. It helps those who are trying to help (on their own time) to provide
as much information as possible.

This is fine as long as the variable is not needed by other modules.
In your case, you should declare your public variable in a regular module
(Not in ThisDocument module, which, like the userform module, is a class
module). Class modules do not share public variables with other modules.

So, create a regular module and declare your public variables there.

I know some people frown on public variables... For example, in your case,
there would be a way to pass the variable as a parameter when calling the
userform and avoid the need for a public variable...
But, for simple stuff like this, I do not think that a public variable can
be a problem.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top