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
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
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...
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!
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".
"Jean-Guy Marcil" <no-...@leaveme.alone> skrev i meddelandet
news:eANZnd7U...@TK2MSFTNGP11.phx.gbl...
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.