I'm setting up a variable letter that we merge frequently. I'm provided a Microsoft Word document that is completely static. I plan on copy and pasting this into a FusionPro text box, which preserves the formatting.
There are elements within this Microsoft Word Document that I would like to use in other parts of the variable letter. I am attempting to create these variables as JavaScript Global Variables. Attached is my code (In JavaScript Globals) so far:
var frame = FindTextFrame('Letter');
var letter = frame.content.split('</para>');
var lines = [];
//Strips out tagged text formatting in letter, and places it in 'lines' variableName
for (var i=0; i<letter.length; i++){
if (letter[i] != '')
lines.push(Trim(RawTextFromTagged(letter[i])));
}
lines = lines.filter(String); // tidy's up array
//Assumes Date is in 3rd line of letter. Would like make more robust. i.e. find line that matches "Wednesday, April 1, 2020";
var EventDate = lines[2];
//Looks for the Company Name, and returns the two lines following.
for (var i=0; i<lines.length; i++){
if (lines[i] == 'Company Name'){
var EventAddress = lines[i+1];
var EventCity = lines[i+2];
break;
}
}
//Looks for "Guest" and returns everything after the colon;
for (var i=0; i<lines.length; i++){
if (lines[i].search('Guest') == 0){
var PresenterLine = lines[i].split(':');
var GuestSpeaker = PresenterLine[1];
break;
}
}
//Looks for a phone number... not working. Needs to match "(###)###-####." because it's usually at end of sentence.
for (var i=0; i<lines.length; i++){
var words = lines[i].replace(') ',')').split(' '); //Removes space after parentheses so phone number ends up as one word.
for (var j=0; j<words.length; j++){
if (words[j].match(/\(?[\d]{3}\)?[\d]{3}?[\d]{4}$\./)){
var CompanyPhone = words[j];
break;
}
}
}
The biggest issue is I'm having trouble matching the phone number. Most of the time, the phone number is in the format "(###)###-####" but can deviate slightly. It's always at the end of the sentence, so it will end up with a period at the end.
I'm also wondering if there is a better way of extracting the date from this letter. It will always be on its own line in the format: "Wednesday, April 1, 2020"