Home > Scripting > Format Long String for VBA with Python

Format Long String for VBA with Python

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 – http://stackoverflow.com/questions/9475241/split-python-string-every-nth-character .  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: , , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: