PDA

View Full Version : Solved: Empty Variables



Levski
05-08-2005, 04:23 AM
I am inserting a set of variables based on user input into a form, then finishing off with a field update. However, if any of the values are null, I get "Error! No document variable supplied."

I would prefer that where variable is empty, nothing appears.

Thank you.

MOS MASTER
05-08-2005, 06:05 AM
Hi Lev, :D

A bit more background please...

Could you be a little more specific on the code you are using to write the values and update of the variables.

And could you tell us exactly what kind of variables you are using? (Inserted in the document how?)

Enjoy! :thumb

Levski
05-08-2005, 06:34 AM
The variables are entere through automation; this is pascal but you will get the drift:

WordDoc.Variables.Add('Name', Name);
WordDoc.Variables.Add('Address1', tAddress1);
WordDoc.Variables.Add('Address2', tAddress2);
WordDoc.Variables.Add('Address3', tAddress3);
WordDoc.Variables.Add('Address4', tAddress4);
WordDoc.Variables.Add('Postcode', tPostcode); etc

and

WordDoc.Fields.Update;

I then have a custom menu in the toolbar in the document these values are being passed to, and all the menu items invoke this macro:

Sub InsertData()
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldDocVariable, Text:= _
Application.CommandBars.ActionControl.Caption, PreserveFormatting:=True
End Sub

However!

If, for examples sake, the address is 3 lines long (and so tAddress4 is a null value) when the user clicks on Address4 in the Word custom menu, he will get "Error! No document variable supplied."

I would prefer for nothing to happen. For the click to be ignored so nothing appears on screen. Or, if need be, an empty field that displays nothing.

Does this make any sense?

Thanks.

Levski

MOS MASTER
05-08-2005, 06:56 AM
Hi Lev, :D

I'm trying to reproduce your error wich makes sence but I'm having a hard time...maybe it's version specifc mine is (2003)

But as I understand if correct:
You're inserting the caption of the commandbarbutton to create a fieldcode that contains the value of a Variable.

If no variable is preset in the document and you run the sub on that variable you get this error?

I think it would make sence for you to check if that Variable exists in you're document. If so execute your sub if not Cancel execution.

Variables don't have an Exists method so you have to loop through the entire collection to get that information.

Like:
Sub InsertData()
If VarExists Then
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldDocVariable, Text:= _
Application.CommandBars.ActionControl.Caption, PreserveFormatting:=True
Else
Exit Sub
End If
End Sub

Private Function VarExists() As Boolean
Dim oVar As Word.Variable
For Each oVar In ActiveDocument.Variables
If oVar.Name = Application.CommandBars.ActionControl.Caption Then
VarExists = True
Exit For
Else
VarExists = False
End If
Next
End Function

You have to adjust the sub for you're Pascal Automation and set Worddoc instead of Activedocument but you can handle that youreself!

Enjoy! :whistle:

Howard Kaikow
05-08-2005, 01:43 PM
I am inserting a set of variables based on user input into a form, then finishing off with a field update. However, if any of the values are null, I get "Error! No document variable supplied."

I would prefer that where variable is empty, nothing appears.

Thank you.

Here's an old function of mine, you MUST test the value before use.


Public Function strGetDocumentVariable(docSourceDocument As Word.Document, strDocumentVariableName As String) As String
' docSourceDocument = Source document
' strDocumentVariableName = Name of document variable

On Error Resume Next
strGetDocumentVariable = docSourceDocument.Variables(strDocumentVariableName).Value
If Err.Number <> 0 Then
strGetDocumentVariable = strEmpty
End If
On Error GoTo 0
End Function

MOS MASTER
05-08-2005, 03:00 PM
Hi Howard, :D

Testing for a value is a real good idea!
I think my sub would inprove if where a variable exists there can be another If block in there to test for the Value of the variable.

You're using an error handler to check if a variable exists if 0 then it does and you can check for its value.

I like checking for it's existens with my method but yours works as well. It's basicly programming preference.

I'll add your suggestion to my own variabel sub because it's an improvent.

Bye...:whistle:

Howard Kaikow
05-08-2005, 05:03 PM
Hi Howard, :D

Testing for a value is a real good idea!
I think my sub would inprove if where a variable exists there can be another If block in there to test for the Value of the variable.

You're using an error handler to check if a variable exists if 0 then it does and you can check for its value.

I like checking for it's existens with my method but yours works as well. It's basicly programming preference.

I'll add your suggestion to my own variabel sub because it's an improvent.

Bye...:whistle:

Using the Error handler is much faster than searching thru a collection, but the technique should only be used in thoes cases where you know what the error would mean, or in cases in which the error code doesn't matter. i.e., o or not is the only thing that matters.

Levski
05-09-2005, 05:26 AM
Thanks for all the advice guys. That all works great, insofar as I described the problem (i.e. a user opens a new document, clicks custom menu item 'Address4' and if the variable is null, Error! No document variable supplied does not appear.

Using the help kindly provided by you and moving on, I decided to have some template letters with some variables already present when the document is opened:


{ DOCVARIABLE Name \* MERGEFORMAT }
{ DOCVARIABLE Address1 \* MERGEFORMAT }
{ DOCVARIABLE Address2 \* MERGEFORMAT }
{ DOCVARIABLE Address3 \* MERGEFORMAT }
{ DOCVARIABLE Address4 \* MERGEFORMAT }
{ DOCVARIABLE Postcode \* MERGEFORMAT }

Dear { DOCVARIABLE Contact \* MERGEFORMAT }


which would open as (assuming all variables have a value except for Address4)


Joe Bloggs
10 Some Road
Some Town
Some County
Error! No document variable supplied.
Some Post Code


Dear Joe...


My initial thought is to run a macro on open along the lines of


Dim oField As Word.Field
For Each oField In ActiveDocument.Fields
If (Field Contains Error) ' Proper Syntax Pending
oField.Delete
End If
Next


Your thoughts/suggestions would be much appreciated.


Thanks!

MOS MASTER
05-09-2005, 05:45 AM
Using the Error handler is much faster than searching thru a collection, but the technique should only be used in thoes cases where you know what the error would mean, or in cases in which the error code doesn't matter. i.e., o or not is the only thing that matters.

True it's much faster but it's far less readable and I wouldn't suggest it.

There's of course nothing wrong with your motivation and if you are looking for a speed upgrade in the code than this is probably the way to find it. (But for me only then would I use it)

Like I said before it all comes down to prefference. I always prefer readable code with a lot of check's in them, this also shows you have thought about the subject. (Just like you're saying only use it if you know what error to expect)

Bye..bye..:hi:

MOS MASTER
05-09-2005, 05:56 AM
Hi Lev, :D

I think you should stick with your original plan and incorparate the field existens/value check to handle your fields.

But if your going on this new approach then this is a way to delete those empty fields.


Private Sub DeleteEmptyField()
Dim oField As Word.Field
For Each oField In ActiveDocument.Fields
If oField.Type = wdFieldDocVariable Then
If Trim(oField.Result) = "Error! No document variable supplied." Or _
Trim(oField.Result) = "" Then
oField.Delete
End If
End If
Next
End Sub


Enjoy! :whistle:

Levski
05-09-2005, 03:58 PM
This is as well as the original plan, not instead of. The original question dealt preventing errors when a field is created through a user click; my later question was about preventing them when the field already exists in the document being opened.

Thanks to the patience and help of those who replied I have been able to come to a satisfactory solution on both counts, using the code donated above. :cloud9:

MOS MASTER
05-10-2005, 09:58 AM
Hi Lev, :D
You're Welcome! :beerchug: