G
Greg Maxey
I am monkeying around with a template that will let me make documents to
show/hide details, instructions, etc.
I am using an IF field (e.g., {IF {DOCPROPERTY ShowHide1} = "Show"{Autotext
Event1}}
It is working OK, but I need some advice on the macros. The basic problem
is that as I add events I need to define the docvariables on first use. My
first stab was the following without the amplifying directions and the
result of course was an error indicating the Object (ShowHide3) had been
deleted. The real truth is that it had never been created. The solution
was to create it once using hte immediate window as incidcated: (Please
read on)
Sub ShowHideEvent3()
Dim Toggle As String
'This macro is simplified. Create the initial document variable by using
the following code
'in the immediate window:
'ActiveDocument.Variables.Add Name:="ShowHide3", Value:="Hide"
Toggle = ActiveDocument.Variables("ShowHide3").Value
If Toggle = "Show" Then
ActiveDocument.Variables("ShowHide3").Value = "Hide"
Else
ActiveDocument.Variables("ShowHide3").Value = "Show"
End If
ActiveDocument.Fields.Update
End Sub
Next, with some earlier help from Dave Lett, I adapted the above to use an
error handler:
Sub ShowHideEvent1()
Dim Toggle As String
'This macro uses a error handler and resume command to create the variable
on first use.
Dim err_handler
On Error GoTo err_handler
Toggle = ActiveDocument.Variables("ShowHide1").Value
If Toggle = "Show" Then
ActiveDocument.Variables("ShowHide1").Value = "Hide"
Else
ActiveDocument.Variables("ShowHide1").Value = "Show"
End If
ActiveDocument.Fields.Update
End
err_handler:
ActiveDocument.Variables.Add Name:="ShowHide1", Value:="Hide"
Resume
End Sub
Dave Lett not to settle for such an ugly solution suggested something like:
Sub ShowHideEvent2()
'This macro searches for the variable name and if not found creates it.
Dim bExists As Boolean
Dim Toggle As String
Dim oVar As Variable
For Each oVar In ActiveDocument.Variables
If oVar.Name = "ShowHide2" Then
bExists = True
End If
Next oVar
If bExists = False Then
ActiveDocument.Variables.Add Name:="ShowHide2", Value:="Hide"
End If
Toggle = ActiveDocument.Variables("ShowHide2").Value
If Toggle = "Show" Then
ActiveDocument.Variables("ShowHide2").Value = "Hide"
Else
ActiveDocument.Variables("ShowHide2").Value = "Show"
End If
ActiveDocument.Fields.Update
End Sub
Each method seems to work, but doesn't feel right. Am I looking for
something that just isn't there?Please advise.
show/hide details, instructions, etc.
I am using an IF field (e.g., {IF {DOCPROPERTY ShowHide1} = "Show"{Autotext
Event1}}
It is working OK, but I need some advice on the macros. The basic problem
is that as I add events I need to define the docvariables on first use. My
first stab was the following without the amplifying directions and the
result of course was an error indicating the Object (ShowHide3) had been
deleted. The real truth is that it had never been created. The solution
was to create it once using hte immediate window as incidcated: (Please
read on)
Sub ShowHideEvent3()
Dim Toggle As String
'This macro is simplified. Create the initial document variable by using
the following code
'in the immediate window:
'ActiveDocument.Variables.Add Name:="ShowHide3", Value:="Hide"
Toggle = ActiveDocument.Variables("ShowHide3").Value
If Toggle = "Show" Then
ActiveDocument.Variables("ShowHide3").Value = "Hide"
Else
ActiveDocument.Variables("ShowHide3").Value = "Show"
End If
ActiveDocument.Fields.Update
End Sub
Next, with some earlier help from Dave Lett, I adapted the above to use an
error handler:
Sub ShowHideEvent1()
Dim Toggle As String
'This macro uses a error handler and resume command to create the variable
on first use.
Dim err_handler
On Error GoTo err_handler
Toggle = ActiveDocument.Variables("ShowHide1").Value
If Toggle = "Show" Then
ActiveDocument.Variables("ShowHide1").Value = "Hide"
Else
ActiveDocument.Variables("ShowHide1").Value = "Show"
End If
ActiveDocument.Fields.Update
End
err_handler:
ActiveDocument.Variables.Add Name:="ShowHide1", Value:="Hide"
Resume
End Sub
Dave Lett not to settle for such an ugly solution suggested something like:
Sub ShowHideEvent2()
'This macro searches for the variable name and if not found creates it.
Dim bExists As Boolean
Dim Toggle As String
Dim oVar As Variable
For Each oVar In ActiveDocument.Variables
If oVar.Name = "ShowHide2" Then
bExists = True
End If
Next oVar
If bExists = False Then
ActiveDocument.Variables.Add Name:="ShowHide2", Value:="Hide"
End If
Toggle = ActiveDocument.Variables("ShowHide2").Value
If Toggle = "Show" Then
ActiveDocument.Variables("ShowHide2").Value = "Hide"
Else
ActiveDocument.Variables("ShowHide2").Value = "Show"
End If
ActiveDocument.Fields.Update
End Sub
Each method seems to work, but doesn't feel right. Am I looking for
something that just isn't there?Please advise.