tags:

views:

52

answers:

2

How should I define large strings in VBA? Is there a better way than coding something like the below?

Dim largeString as String
largeString = "This is a long block of text that I want to fill " & _
              "into a form field. I need to make sure I pay attention " & _
              "to spacing and carriage return issues while doing so. " & _
              "I also have to use quotes liberally, the concatenation " & _
              "operator, and the continuance underscore to make sure " & _
              "VBA can parse my code." & vbCr & vbCr & _
              "It's kind of a pain in the ass and I wish I could use " & _
              "a heredoc instead, letting me copy and paste the block" & _
              "of text I need from another source and shove it into " & _
              "a string."

Edit: Ugh, and there's a 25 line continuation limit too? So much for nice indenting and 80 characters of width, that only gives me enough room for a couple decent paragraphs.

+3  A: 

No, this is as good as it gets.

For really long strings it might be an option to keep the string in a separate file, or use some application feature. For example, in Word, you might want to store the string in a document variable, as hidden text or AutoText. In Excel, you might consider a hidden sheet for storing long string constants.

0xA3
Yeah, I was considering something like that. Another option would be to make a form that never gets shown that contains text boxes that contain the text I want then just use the .Value() of them. Only downside is that I can see maintenance of a workaround like that being a huge nightmare. It's not like VBA maintenance is a walk in the park though :/
Oesor
I like this answer but maybe something like a text stream to a text file. Maybe that's too much though?
Chris
@Chris: I'm not sure what you mean. Actually keeping the string in a text file was the first option I mentioned.
0xA3
The downside to doing it in a text file instead of code embedded in an Office document is now you have multiple files you need to maintain. For a small, self-contained VBA app that's going a little overboard.
Oesor
+1  A: 

I prefer doing it this way:

Dim lStr As String
lStr = ""

lStr = lStr & "This is a long block of text that I want to fill "
lStr = lStr & "into a form field. I need to make sure I pay attention "
lStr = lStr & "to spacing and carriage return issues while doing so. "
lStr = lStr & "I also have to use quotes liberally, the concatenation "
lStr = lStr & "operator, and the continuance underscore to make sure "
lStr = lStr & "VBA can parse my code." & vbCr & vbCr
lStr = lStr & "It's kind of a pain in the ass and I wish I could use "
lStr = lStr & "a heredoc instead, letting me copy and paste the block"
lStr = lStr & "of text I need from another source and shove it into "
lStr = lStr & "a string."

I think this method is easier to work with the line continuation method and there are no line number limits to get in the way. You can comment out individual lines which is usefully for debugging SQL strings.

When doing long strings, I find it easier to use short variable names because VBA does not have the equivilent of a += operator. largeString = largeString & " takes up too much space and gets repetitive, so shorten the string name to make the format somewhat bearable.

For very large blocks of text, write it in a text editor then copy and paste it into your procedure. Then copy

lStr = lStr & "

and paste it at the beginning of each line. The VBA editor will automatically add the quotes at the end of the line making the process simple to do.

BrianZ
I like this syntax for line continuation but I ask "Why" Why would someone need such a large string that couldn't be placed elsewhere? And, this reminds me of all those VBA bandits who think the height of programming is concatenation.
Chris