Skip to main content
Announcements
Qlik Connect 2025: 3 days of full immersion in data, analytics, and AI. May 13-15 | Orlando, FL: Learn More
cancel
Showing results for 
Search instead for 
Did you mean: 
dgreenberg
Luminary Alumni
Luminary Alumni

Macro to loop through and enumerate all field selections - wanted to share works under Ajax

Below is a macro that I think is pretty cool.  It even works under Ajax.

The only limitation is the field size in the macro.  I have successfully put over 1.5 million characters in the field but somewhere above that it blows up.

This macro reads a variable, writes to the variable and loops through all fields to enumerate their selections into an email.  This is useful because without this you would end up with County: 30 of 50,000 instead of listing all counties.

Assumptions:

Variable called vMessageBody

1 or more field selections

function sendSelectionsBySMTP

fieldName="$Field"

  myText=" "

  Set val=ActiveDocument.Fields(fieldName).GetPossibleValues(20000)

  For i=0 to val.Count-1

       set valsCount=ActiveDocument.Fields(val.item(i).Text).GetSelectedValues

       Set val2=ActiveDocument.Fields(val.Item(i).Text).GetSelectedValues

       'if val2.Count>0 then

                 myText=myText & chr(10) & val.Item(i).text & chr(10)

            for z=0 to val2.Count -1

                 myText=myText & val2.Item(z).text & chr(10)

            next

     end if 

Next

  ActiveDocument.Variables("vMessageBody").SetContent getVariable ("vMessageBody")& myText, true

  Activedocument.Fields(fieldName).Clear

  Set val=Nothing

Set objMsg = CreateObject("CDO.Message")

Set msgConf = CreateObject("CDO.Configuration")

' Server Configuration

msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

'msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mailserver.com"

'msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

'msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 0

msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "sender@domain.com"

msgConf.Fields.Update

 

'MailToVar = ActiveDocument.Variables("To").GetContent.string

objMsg.To = "recipient@domain.com"'

'MailFromVar= ActiveDocument.Variables("From").GetContent. String

objMsg.From =  "sender@domain.com"

 

objMsg.Subject = "Please run this report for me"

objMsg.HTMLBody = getVariable("vMessageBody")

objMsg.Sender = "sender@domain.com"

'objMsg.AddAttachment "Path\Document.xls"

'objMsg.AddAttachment "Path\Document.xls"

'objMsg.AddAttachment "Path\Document.xls"

Set objMsg.Configuration = msgConf

' Send

objMsg.Send

END function

FUNCTION getVariable(varName)

set v = ActiveDocument.Variables(varName)

getVariable = v.GetContent.String

END FUNCTION

0 Replies