- Remove From My Forums
-
Question
-
For no apparent reason, my printer has started printing the above message in the bottom right-hand corner of each page. It clearly a problem with the desktop rather than the printer, as it does not happen when I print from my laptop. Please tell
me what to do about this in very simple terms. I have read some of the discussions on this topic but cannot follow them. For example, I have no idea what a macro is.Thank you.
-
Moved by
Monday, April 11, 2016 9:35 AM
off topic
-
Moved by
Answers
-
Hi, Patrick
Macrory>>I have no idea what a macro is.
You can Press Alt + F11 key to start the VBE in which you can write Macro.
>>if talk about the error then from error we can get the idea that value is not supplied to the document variable.
Here I have give you some examples to create, assign the value and delete document variable.
How to Set and Retrieve the Value of a Document Variable
Sub GetSetDocVars() Dim fName As String fName = "Jeff Smith" ' Set contents of variable "fName" in a document using a document ' variable called "FullName". ActiveDocument.Variables.Add Name:="FullName", Value:=fName ' Retrieve the contents of the document variable. MsgBox ActiveDocument.Variables("FullName").Value End Sub
How to Delete a Document Variable
Sub GetSetDeleteDocVars() Dim fName As String fName = "Jeff Smith" ' Set contents of variable "fName" in a document using a document ' variable called "FullName." ActiveDocument.Variables.Add Name:="FullName", Value:=fName ' Retrieve the contents of the document variable. MsgBox ActiveDocument.Variables("FullName").Value ' Delete the variable. ActiveDocument.Variables("FullName").Delete End Sub
Regards
Deepak
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click
HERE to participate the survey.-
Proposed as answer by
Deepak Saradkumar PanchalMicrosoft contingent staff
Wednesday, March 30, 2016 7:31 AM -
Marked as answer by
Deepak Saradkumar PanchalMicrosoft contingent staff
Friday, April 1, 2016 10:29 AM
-
Proposed as answer by
- Remove From My Forums
-
Question
-
I have a macro enabled document template (.dotm) that I open and replace some named fields with real data. After I have the document completed, if I try to print it, all of the fields change to «Error! No document variable supplied». The same thing happens
if I try to save it as a .PDF file. The same thing happens if I save it as a .DOCX to disable the macros first and then try to print.How do I get it to stop modifying my finished document?
microwave tech
Answers
-
Hi «drew»
I suspect the problem is with how you’re replacing the «named fields». From the title and the little you tell us, I’m guessing that you’ve inserted DocVariable fields in the document, but that there is no corresponding Document.Variable object stored
in the file. So when the fields update, you get an error message. Only when a field updates does it dynamically execute the field code and content, and most field types aren’t constantly updating — Word couldn’t function if they did. But the actions you describe
do trigger an update, which explains why you’re seeing the described behavior.It becomes apparent, therefore, that DocVariable fields weren’t meant to be used in this manner. If you were to pass the content to Document Variable objects in the document, it should work correctly. Something like this:
ActiveDocument.Variables(«field1») = «This is the text for field 1»Another possibility, since you’ve already written the code you have, would be to lock the fields so that they cannot update: ActiveDocument.Fields(1).Locked = True
You could also remove the DocVariable fields and insert Bookmarks, instead. Bookmarks were designed to be «data targets»: ActiveDocument.Bookmarks(«Name»).Range.Text = «This is the text for field 1»
Cindy Meister, VSTO/Word MVP,
my blog-
Marked as answer by
Friday, November 30, 2012 7:26 AM
-
Marked as answer by
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,
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,
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,
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,
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,
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,
You’re Welcome! :beerchug:
Powered by vBulletin® Version 4.2.5 Copyright © 2023 vBulletin Solutions Inc. All rights reserved.
-
05-04-2012, 08:45 AM
#1
Registered User
Avoid «Error! No document variable supplied»
Using DocVariables (and VBA) to write a large number of text variables from Excel file to Word doc. Getting the above error if DocVariable field is empty, and I want to get rid of the error.
Successful method so far is to nest the DocVariable inside an If statement in Word, but by the time I do that to all the DocVariables, the document will look horrendous when fields are showing.
I could also check each variable in VBA before sending it to Word, but that would require a large number of If statements (one per variable).
I could also add a space (» «) to each variable before sending it (e.g Range(FirstName) = FirstName & » «), but that then puts unwanted spaces in the Word document.
I tried comparing variables in Excel with » «, with a view to accepting the » » value if the variable was empty (i.e. «»), but I haven’t been able to find a function that will work with «».
Any ideas, or variations on the above???
-
05-04-2012, 09:34 AM
#2
Re: Avoid «Error! No document variable supplied»
you could just write a separate routine that takes a DocVariable and a value and have that routine do any variable testing for you. then you only have to write one test instead of many if statements.
Josie
if at first you don’t succeed try doing it the way your wife told you to
-
05-10-2012, 10:08 PM
#3
Registered User
Re: Avoid «Error! No document variable supplied»
Thanks for the suggestion, JosephP. While I was trying to figure out how I might do that, I discovered that the method of putting in a » » was OK — it didn’t work when I first tried it because I had been overwriting the » » with «». My bad!