Jacob Bischoff
Jacob Bischoff

Reputation: 146

string modifiers and properties do not work

I am trying to use Methods and Properties of the String Class to modify a string, but I keep getting an Invalid qualifier compile error. I even copied the following code* directly from the MSDN website and it throws the same error.

Public Sub Main()
    Dim original As String
    original = "aaabbb"
    Dim modified As String
    modified = original.Insert(3, " ")
End Sub

'This is the original code, but I had to change it slightly because the word-vba
'programming environment didn't like the syntax and highlighted everything red.

'Public Sub Main()
   'Dim original As String = "aaabbb"
   'Console.WriteLine("The original string: '{0}'", original)
   'Dim modified As String = original.Insert(3, " ")
   'Console.WriteLine("The modified string: '{0}'", modified)
'End Sub

Does word-vba not support string class modifiers and properties, am I not initializing the string correctly, or is there some other problem?

Upvotes: 1

Views: 91

Answers (1)

Mathieu Guindon
Mathieu Guindon

Reputation: 71157

modified = original.Insert(3, " ")

You're thinking in VB.NET, but writing VBA. Strings (or any primitive or UDT type) don't have members in VBA. Not your fault, finding official VBA documentation is getting harder every day, with every "VBA" search yielding results for VB.NET.

That original code is clearly VB.NET.

If you mean to concatenate 3 spaces in front of original, then what you want to do is this:

modified = String(3, " ") & original

If you mean to get a new string in which a specified string is inserted at a specified index position in this instance (MSDN), then you want to do this (thanks @A.S.H!):

modified = Left$(original, 3) & " " & Right$(original, Len(original) - 3)

Upvotes: 3

Related Questions