Total Newbie Question on Variables

W

W.G.

How do I pass a variable from my input box using VBA into my word document.
I know it's a dumb question, but I'm trying to learn this stuff on my own
and I just got started.

Thanks in advance for your help.
 
D

Doug Robbins - Word MVP

Hi W.G,

Several ways:

Selection.InsertBefore InputBox("Message", "Title", "Default")

ActiveDocument.Bookmarks("BMName").Range.InsertBefore InputBox("Message",
"Title", "Default")

ActiveDocument.Variables("varname").Value = InputBox("Message", "Title",
"Default") and use a {DOCVARIABLE "varname"} field in the document

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
J

JulieD

Hi WG

(bit of a long answer but ) here's the way i teach it in class -
get the students to open a new word document ... and record a macro that
puts their name on the first line, then they press enter and enter their
department (co, favourite colour whatever) and then press enter and stop
recording the macro.

we then go into the vb editor window and display the code - e.g.
Sub Macro28()
Selection.TypeText Text:="julie"
Selection.TypeParagraph
...rest of code here....
End Sub

then i talk about interacting with users using two methods - msgbox and
inputbox, we do a msgbox example by adding
msgbox "Nearly time for lunch"
above the end sub
and then i get onto inputboxes & variables & such.

my take on variables is that there's three things you need to do with them
1) declare - using the DIM statement
2) populate - using an inputbox (in this case)
3) use - get the info back into word

the example then ends up looking like this:
Sub Macro28()
dim pname as string
pname = Inputbox("What is your name")
Selection.TypeText Text:=pname
Selection.TypeParagraph
...rest of code here....
End Sub

later on we get to using the methods that Doug mentioned in his reply.

Hope this helps
Regards
JulieD
 

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