Archive for the ‘Scripting’ Category

Format Long String for VBA with Python

October 15, 2016 Leave a comment

The following python script was useful when I had a long payload that I needed to add into a VBA macro.  It takes the string and then breaks it up into lines that can be added to the VBA.  VBA rules won’t let you have more than 25 line continuations and there’s also a certain length of string that can be in there as well.  The solution largely came from one of the responses here – .  The ‘320’ value is the number of characters I decided to make each line, you could easily make the script figure out how many characters per line were needed to keep within VBA’s boundaries.

text = "superlongstringtobreakup"

def split_by_n( seq, n ):
    """A generator to divide a sequence into chunks of n units."""
    while seq:
        yield seq[:n]
        seq = seq[n:]

commandlist = list(split_by_n(text,320))

f = open('powershell32_vba.txt', 'w')

for line in commandlist:
    f.write('& "' + line + '" _\n')
Categories: Scripting Tags: , , , ,