VBA MS Word conditional formatting - vba

I have a VBA code that gives a value in a variable and then adds the variable in a document
Dim NoGFIs As Integer
NoGFIs = 1
ActiveDocument.Variables.Add Name:="GFIs ", Value:=NoGFIs
and I used a conditional statement in the word document
No of GFIs: {DOCVARIABLE GFIs}
Check IF: {IF <<GFIs>> = 1 "ONE" "NOT ONE"
But the outcome shows that something is wrong:
No of GFIs: 1
Check IF: NOT ONE
It shows that the GFIs value gets out from the VBA to the document, but IF fails to evaluate it??
Has anyone seen anything like that before?

Your if condition seems to be wrong. Please try the below:
{ IF { DOCVARIABLE GFIs \* MERGEFORMAT } = 1 "ONE" "NOT ONE"\* MERGEFORMAT }
Also, did you notice the trailing space in your variable name "GFIs " in ActiveDocument.Variables.Add Name:="GFIs ", Value:=NoGFIs?
Results:

Related

Why would joining an array work, but not accessing individual elements?

I've just started working on customizing my Shopify template, but I've ran into a basic issue, where the docs didn't help.
<script>console.log("_{{ item.properties | join: ", " }}_");</script>
// prints "_foo1, foo2_"
<script>console.log("_{{ item.properties[0] }}_");</script>
// prints "__"
<script>console.log("_{{ item.properties }}_");</script>
// prints "_EscapedHashDrop_"
Thanks!
you are getting value in string in first console and try to fetch value as a array in second console so it's not possible
you have to convert your string value in array by using .split() function.
Try this code it will give result according to you.
var itemvalue = "_{{ item.properties | join: ", " }}_";
// console.log(itemvalue); ---> print "_foo1, foo2_"
var myvalue = itemvalue.split(",");
console.log(myvalue[0]);

Checking if GroovyRowResult field is empty string

I am using sql.firstRow to check if a row exists in the postgres database based on some criteria.
def cur = sql.firstRow(r, '''
SELECT "some_thing"
FROM "my_table"
WHERE "customer_name" = :customer_name
AND "sad_date" = :sad_date
AND "forgiver" = :forgiver
''')
I find that this works:
if (cur){
log.debug("Found Some thing " + cur["some_thing"])
log.debug("Cur: " + cur.keySet())
}
however this lets in any rows that don't have some_field inside it.
ISSUE
To avoid this, when we try and check for the existance of a non empty value for some_field on the result row like this:
if (cur && "${cur.some_thing}" ){
log.debug("Found Some thing " + cur["some_thing"])
}
ERROR
I get an error suggesting that:
No signature of `String.positive` for argument types for the given type.
I have read this question and changed from cur.some_thing and cur['some_thing'] to "${cur.some_thing}" but the error does not go away
I have also tried this post and tried to use cur.getProperty("some_thing") and it still throws the same error.

How to document Microsoft Word mail merge templates? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I'm using the directory mail merge feature in Word 2010 to create a formatted document (of requirements) using an Excel 2010 spreadsheet as the source of data.
Things work, but I have a compact mess of braces, formatted single characters, and fields, with very little whitespace (as this would change my output). I inserted fields with CTRL-F9 and Word treats them as their own objects rather than individual characters.
The complications come from having a lot of conditional fields, some of which are nested, and don't have the typical cues that most code has for seeing the nesting structure (line breaks, indentation, etc.)
Is there a straightforward way to document the mail merge template code from within the document? Or will I need to maintain a separate text file if I want to take advantage of whitespace and other readability cues?
Things you can do to help with field code documentation include the following. But there are quite a few gotchas, which complicates the answer. I've tried to cover them below.
Use formatting characters such as paragraph marks inside field code
braces to improve layout
Use field code types with empty results such as "SET" to insert
documentation
Use hidden text inside field codes to insert documentation
Remove of QUOTATION MARK (") characters that are not absolutely
necessary
An example of (1):
If you have something like:
{ IF { MERGEFIELD A } = { bookmarka } "{ MERGEFIELD B }" "{ IF { MERGEFIELD C } = { bookmarkb } "result2" "result3" }" }
Then you can lay it out like this without altering the result of the IF fields:
{ IF { MERGEFIELD A } = { bookmarka }
"{ MERGEFIELD B }"
"{ IF { MERGEFIELD C } = { bookmarkb }
"result2"
"result3" }" }
by clicking after { bookmarka } and pressing Enter to enter a normal Word paragraph mark, then inserting some spaces before the QUOTATION MARK character.
Different people might choose different indentation patterns and ways of dealing with closing "}". There are no standards or widely used traditions in this area as there are with, say, C code.
There is a drawback. Every time you switch between field code and field results views, Word may repaginate in a long document and the chances are that you will "lose your place" in the code.
An example of (2) is that you can insert a { SET } field in many places in your code (but not all) and put pretty much any text after the "SET variablename " part. Or you could define a bookmark called "REMARK" ("COMMENT" seems too easily confused with the built-in field code COMMENTS)
e.g.
{ SET REMARK I wouldn't use "COMMENT" }
or put
{ SET REMARK "" }
at the beginning of your code then use
{ REMARK I wouldn't use "COMMENT" }
{ REMARK because it might be confused with the "COMMENTS" field }
Just bear in mind that you can't put a field code absolutely anywhere in your field code "block", because in some places Word's intepretation of the field coding will change. e.g.
{ IF 1 = 0 { MERGEFIELD X } { MERGEFIELD Y } }
should always insert the value of { MERGEFIELD Y }
but
{ IF 1 = 1 { MERGEFIELD X }{ REMARK insert X } { MERGEFIELD Y } }
would insert the result of { REMARK insert X }
You could use
{ IF 1 = 1 "{ MERGEFIELD X }{ REMARK insert X }" { MERGEFIELD Y } }
or in this case,
{ IF 1 = 1 { MERGEFIELD X { REMARK insert X } } { MERGEFIELD Y } }
On point (3) Word generally ignores hidden text in field codes, so you can use hidden text to insert documentation. But again, you have to be careful where you put it. For example, this approach is useless for field types that are normally marked as Hidden, such as TC and XE. Personally, I do not think the idea of inserting documentation then hiding it is ideal - quite apart from anything else, it would be very easy to delete without even knowing it was there. But it can be done.
As for point 4, the way Microsoft Word inserts some field types and the documentation (such as it is) for the "field code language" means that there is a tradition of surrounding some things with QUOTATION MARK characters. Examples typically show this in IF fields...
{ IF comparand-1 operator comparand-2 "result if true" "result if false" }
In that particular case, you would need the quotation marks, but if the "result if true" or "result if false" are either a single word (as far as Word is concerned) or the result of a single field code, then you can omit the quotation marks, e.g.
{ IF comparand-1 operator comparand-2 true false }
or
{ IF comparand-1 operator comparand-2 { REF bookmarka } { REF bookmarkb } }
Personally, I prefer to use the quotation marks, because it is the pattern that many users are familiar with, and if you modify the field code in some way you don't have to work out whether you need to re-insert quotaion marks, and so on. But that is just my preference. In the case where you have nested IF fields in particular, you very often have individual field codes as results and the quotation marks probably do not really increase clarity.
You also have to bear in mind that in some cases you must surround fields with quotation marks. For example, if you want to ensure that two values are compared as strings, at the very least you should quote them.
For example, suppose you have a mergefield X that could contain any text string and you want to compare that string with "3". If you use
{ IF { MERGEFIELD X } = 3 True False }
then if X is "1+2", "3", "4-1" you will get a True result. In this case you need
{ IF "{ MERGEFIELD X }" = 3 True False }
and if the "3" was replaced by some kind of variable field, you would probably need
{ IF "{ MERGEFIELD X }" = "{ theVariable }" 3 True False }
Word's tendency to evaluate simple arithmetic expressions in such scenarios is not the only thing that can go wrong. If { MERGEFIELD X } evaluates to the name of a bookmark that you have defined in your document, Word tends to dereference that, and use the value of that bookmark instead of the value of the MERGEFIELD field, unless you surround the { MERGEFIELD X } by quotation marks.

Select everything from //

I'm making like a "mini programming language" in visual basic.
Mostly just for practice and for fun.
I just have one problem. I want to make a commenting system.
I got an idea how it would work, but i don't know how to do it.
So this is what i want to do:
I want to start to select all text from //
So for example, if i write:
print = "Hello World!"; //This is a comment!
it will select everything from the // so it will select
//This is a comment!
Then i would just replace the selected text with nothing.
You can use String.IndexOf + Substring:
Dim code = "Dim print = ""Hello World!""; //This is a comment!"
Dim indexOfComment = code.IndexOf("//")
Dim comment As String = Nothing
If indexOfComment >= 0 Then comment = code.Substring(indexOfComment)
If you want the part before the comment dont use String.Replace but also Substring or Remove:
code.Substring(0, indexOfComment)

Writing a PHP if Blank statement

I'm new to PHP so this will sound basic to most people but I need to write the code for when a variable returns nothing (blank).
My variable is $winner, but sometimes there is no winner, in this case it just leaves the page blank, I would like it so if there is no winner then it will display "no winner".
This is my attempt:
if empty($winner) {
echo "no winner";
}
You can make a function to check the variable's valaue with null or empty...
function IsEmptyString($Season){
return (!isset($Season) || trim($Season)==='');
}
This function can be used to check the same.
Just use:
if (!$winner) { // will catch "", null
echo "no winner"
}