Print word document with routing.

In this article we are going to see how we might be able to add in the logic of our survey to our word document. We have a couple of ideas
that you could use , but it’s a tough challenge and the results may require a little work.

Ok so we have our survey and we have our code to produce the template. In this article we are back to working inside an MDD again , but its
slightly different this time. This is our metadata,

Metadata(en-AU, Question, Label)
SingleWithOther "Single Question with an Other Specify"
categorical [1..1]
{
A "A",
B "B",
C "C",
_Other "Specify Other" other
};
Single "Single Question"
categorical [1..1]
{
A "A",
B "B"
};
Multi "Multiple Response Question"
categorical [1..]
{
A "A",
B "B",
C "C",
D "D",
E "E"
};
ToDaysDate "Date Question"
date;
DoubleQuestioName "Double Question"
double;
LongQuestionName "Long Question"
long;
TextQuestionName "Text Question"
text;
GridQuestion "Grid Question" loop
{
Sub1 "Sub 1",
Sub2 "Sub 2",
Sub3 "Sub 3",
Sub4 "Sub 4",
Sub5 "Sub 5"
} fields -
(
SingleCategoricalQuestionName "Grid Sub Categorical"
categorical [1..1]
{
A "A",
B "B",
C "C",
D "D",
E "E"
};
) expand;
End Metadata

And this is our web routing will look like this,

Routing(Web)
SingleWithOther.Ask()
If SingleWithOther = {C} Then
Single.Ask()
If Single = {C} Then Goto SkipAll
Multi.Ask()
ToDaysDate.Ask()
End If
SkipAll:
DoubleQuestioName.Ask()
LongQuestionName.Ask()
If ( LongQuestionName = "10" ) then
TextQuestionName.Ask()
Else
GridQuestion.Ask()
End If
End Routing

And the thing that is different this time is that we have added a Print routing to our MDD. If you did not know you can have many different routings in your scripts and as you will see in later articles on this blog we can use them to our advantage. Anyway, just add a print routing and place all our PrintToWord code in it. When you have done all this make sure your script works before you go any further ,because if we start off with errors we will get in a right mess. Once everything is working we will make a subroutine called TemplateFromRouting. It will look like this,

Sub TemplateFromRouting(oWord,IOM,sRouting)
Dim oQuestion,iEnd,iStart,sQuestion
Dim sScript, aLines, sLine, bFound, sLogic, sScriptLines
sScript = IOM.MDM.Routing.Script[sRouting]
aLines = split(sScript,mr.Lf)
For each sLine in aLines
sScriptLines = sScriptLines + sLine + mr.lf
iEnd = find(sLine,".Ask(" )
if iEnd > -1 Then Goto FindQuestionName
iEnd = find(sLine,".Show(" )
FindQuestionName:
If (iEnd <> -1) Then
For iStart = iEnd to 0 step -1
If ( mid(sLine,iStart,1) = " " or mid(sLine,iStart,1) = mr.Tab ) Then
sQuestion = mid(sLine,iStart+1,iEnd - iStart-1)
exit for
End If
Next
if ( sQuestion = "" ) then
sQuestion = mid(sLine,0,iEnd)
End If
bFound = true
End If
If ( bFound = true ) Then
If ( sScriptLines <> "" ) Then
MakeRoutingBox(oWord, sScriptLines)
sScriptLines = ""
End If
Set oQuestion = IOM.Questions[sQuestion]
Select Case oQuestion.QuestionType
Case QuestionTypes.qtSimple
MakeSpace(oWord)
MakeTable(oWord,oQuestion.Label)
CreateQuestions(oQuestion,"S","",oWord,0,IOM)
Case QuestionTypes.qtLoopCategorical
If ( oQuestion[0].count = 1 ) Then
MakeSpace(oWord)
MakeTable(oWord,oQuestion.Label)
CreateQuestions(oQuestion,"G","",oWord,0,IOM)
Else
MakeSpace(oWord)
MakeMultiQuestionTable(oWord,oQuestion,IOM)
End If
Case QuestionTypes.qtLoopNumeric
MakeSpace(oWord)
MakeTable(oWord,oQuestion.Label)
CreateQuestions(oQuestion,"G","",oWord,0,IOM)
Case QuestionTypes.qtBlock, QuestionTypes.qtCompound, QuestionTypes.qtPage
MakeSpace(oWord)
MakeTable(oWord,oQuestion.Label)
CreateQuestions(oQuestion,"G","",oWord,0,IOM)
End Select
sQuestion = ""
bFound = false
End If
Next
End Sub

You should notice that we use the same question data type testing as the RunTemplate function the only real difference is how we loop the
questions. We don’t in this case loop the questions in the order that they are in the MDD what we do is we use the following command

sScript = IOM.MDM.Routing.Script[sRouting]

to get a copy of the specified routing, in our case the web routing. Once we have the routing stored in a string variable we then use the
split function to split it into lines. Next we loop through each line and look for a .ask or a .show when we have found one we work out what
the name of the question is. Once we have that we get a copy of the question and create the word table as normal. Whilst all this is going on
we are also keeping a copy of the routing searched so far and when we find the next ask or show , before we add the ask to the word document
we add the logic. To do this we have created another short sub to add the logic to the word document.

Sub MakeRoutingBox(oWord,sLabel)
MakeSpace(oWord)
oWord.ActiveDocument.Tables.Add(oWord.Selection.range,1,1,1,0)
With oWord.Selection.Tables[1]
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.ApplyStyleRowBands = True
.ApplyStyleColumnBands = False
End With
oWord.Selection.Text = "ROUTING ITEM : " + sLabel
End Sub

When you have all this code working correctly and you run it you will end up with a word document looking something like this one

Questions with routing

This code we have started is very simple and will probably fail on some different MDD files, however if you have such a file that it does not work on and you would like us to have a look at it then let us know and we will help you.

Leave a Comment

%d bloggers like this: