Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ActiveDocument.Variables

180 views
Skip to first unread message

Senad Isanovic

unread,
Jun 16, 2004, 9:29:43 AM6/16/04
to
I have a word template with a userform. The values are entered into
textboxes in the user form, then you click on the OK button and the values
are inserted into the doc. Save and close the document. So far so good. Now
I want to assign the values from the document to the form next time the
document is opened (and the form is loaded) and when clicking on OK button I
want to rewrite the document. I tried writing information to the Variables
and then reading from Variables when the form is loaded next time.

If CheckIfVariableExists("chkLogotyp") Then
ActiveDocument.Variables("chkLogotyp").Value = chkLogotyp.Value
Else
ActiveDocument.Variables.Add Name:="chkLogotyp", Value:=chkLogotyp.Value
End If

But obviously something is missing because the old values from the template
show up, and not those from the document (that I want). Thanks! Senad


Jean-Guy Marcil

unread,
Jun 16, 2004, 10:24:38 AM6/16/04
to
Bonjour,

Dans son message, < Senad Isanovic > écrivait :

What code are you using on the OK button to save the DocVariables when also
inserting the text in the document?
What code are you using when you want to retrieve the DocVariables values in
the form initialize event?

By the way, you can use


ActiveDocument.Variables("chkLogotyp").Value = chkLogotyp.Value

by itself. If the DocVariables do not exist, Word creates them on the fly.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarci...@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Senad Isanovic

unread,
Jun 16, 2004, 10:50:40 AM6/16/04
to
When I write to the doc (ok button on the form)

ActiveDocument.Bookmarks("mNamnAdress").Range.Text = txtNamnAdress.Text

Then I add the variable

ActiveDocument.Variables.Add Name:=" txtNamnAdress ", Value:=
txtNamnAdress.Value

Then Form Initialize

If CheckIfVariableExists ("txtNamnAdress") Then

txtNamnAdress.Value = ActiveDocument.Variables("txtNamnAdress").Value

End If

Thanks!

/Senad


"Jean-Guy Marcil" <no-...@leaveme.alone> skrev i meddelandet
news:erPql16...@TK2MSFTNGP11.phx.gbl...

Jean-Guy Marcil

unread,
Jun 16, 2004, 11:36:16 AM6/16/04
to
Bonjour,

Dans son message, < Senad Isanovic > écrivait :
In this message, < Senad Isanovic > wrote:

I see three potential problems:

|| When I write to the doc (ok button on the form)
||
||
||
|| ActiveDocument.Bookmarks("mNamnAdress").Range.Text = txtNamnAdress.Text
||

Presumably, you want to save the text to a DocVariables because you want to
load it AND give the user a chance to change the text n a later session.
YOur code as it is now deletes the bookmark. YOu have to recreate it. Maybe
you are already doing that, in which case, sorry to amke you waste time read
this! If not, here is some code:
'_______________________________________
Dim bmRange As Range

Set bmRange = ActiveDocument.Bookmarks("mNamnAdress").Range
bmRange.Text = txtNamnAdress.Text
ActiveDocument.Bookmarks.Add "mNamnAdress", bmRange
'_______________________________________

||
|| Then I add the variable
||
||
||
|| ActiveDocument.Variables.Add Name:=" txtNamnAdress ", Value:=
|| txtNamnAdress.Value
||

This code, when ran a second time will try to create a variable that already
exists, and will generate an error. Either you check for its existence, or
it is much easier to just use this code which works regardless of the
pre-existence or not:

ActiveDocument.Variables(" txtNamnAdress ").Value = txtNamnAdress.Value

||
|| Then Form Initialize
||
||
||
|| If CheckIfVariableExists ("txtNamnAdress") Then
||
|| txtNamnAdress.Value = ActiveDocument.Variables("txtNamnAdress").Value
||
|| End If
||

Here you are checking for a DocVariable called "txtNamnAdress", but above,
you are creating a DocVariable called " txtNamnAdress ".... notice the extra
spaces?
It is much safer to use variables or constants when you are using a name
more than once. This way, it prevents typos and it is much easier if you
decide to modify the name to change it once than read the whole code looking
for all the instances. In this case, you could have the following in the
declaration part (Right at the top, before any Subs) of the userform code:
Const DocVarName As String = "txtNamnAdress"
Const BookMarkName As String = "mNamnAdress"
and replace the name strings in the code with their equivalent constants.

Good luck!

phil

unread,
Jun 17, 2004, 8:39:47 AM6/17/04
to
Hi Jean-Guy
I'm trying to understand this writing to variables and I apologies for my
ignorance
with the code "ActiveDocument.Variables(" txtNamnAdress ").Value =
txtNamnAdress.Value"
dose this only reinsert the text for the one textbox or is the hole userform
reinserted
thanks phil
"Jean-Guy Marcil" <no-...@leaveme.alone> wrote in message
news:eANZnd7U...@TK2MSFTNGP11.phx.gbl...

Jean-Guy Marcil

unread,
Jun 17, 2004, 8:48:42 AM6/17/04
to
Bonjour,

Dans son message, < phil > écrivait :


In this message, < phil > wrote:

|| Hi Jean-Guy
|| I'm trying to understand this writing to variables and I apologies for my
|| ignorance
|| with the code "ActiveDocument.Variables(" txtNamnAdress ").Value =
|| txtNamnAdress.Value"
|| dose this only reinsert the text for the one textbox or is the hole
userform
|| reinserted

This line loads the DocVariable called " txtNamnAdress " (Are you sure you
want those spaces in the name?) with the value of the control called
"txtNamnAdress".

Senad Isanovic

unread,
Jun 22, 2004, 9:16:39 AM6/22/04
to
I'm using the document variables as described above. Still have the same
problem. The values that are assigned to the variables are saved in the
template, not in the document created based on that template. Do I need to
initialise the variables some other way? Thanks! Senad


"Jean-Guy Marcil" <no-...@leaveme.alone> skrev i meddelandet

news:eANZnd7U...@TK2MSFTNGP11.phx.gbl...

Jean-Guy Marcil

unread,
Jun 22, 2004, 11:42:12 AM6/22/04
to
Bonjour,

Dans son message, < Senad Isanovic > écrivait :
In this message, < Senad Isanovic > wrote:

|| I'm using the document variables as described above. Still have the same
|| problem. The values that are assigned to the variables are saved in the
|| template, not in the document created based on that template. Do I need
to
|| initialise the variables some other way? Thanks! Senad

If you have a template that has a userform which contains a command button
running the following code:

'_______________________________________
Private Sub CommandButton1_Click()

Const DocVarName As String = "txtNamnAdress"

ActiveDocument.Variables(DocVarName).Value = txtNamnAdress.Value

End Sub
'_______________________________________

then the variable is saved to the document, not the template. I am not even
sure how to assign a variable to a template without actually opening the
said template (Creating a document from the same template is different)
because variables are document properties or objects, not template's.

Make sure that when you test/run the code, you do so from a document created
from the template, not from the template itself.

0 new messages