MS Office and VBA

This category holds articles regarding general things in MS Office VBA independent from the MS Office application.  

The below method will replace all special or escape characters in a provided String.

This is of use, if you have to use before unknown Strings from any source for example in a file name.

Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
' @Author - Alexander Bolte
' @Description - replaces all characters, which match the pattern "[^A-Za-z0-9-_ ]" in a given String.
' @Param aText - a String in which invalid characters should be replaced.
' Returns the handed String from aText excluding all replaced characters.
Public Function replaceSpecialCharacters(ByVal aText As String) As String
    Dim regEx As New RegExp
    Const cReplEscapeChar As String = "[^A-Za-z0-9-_ ]"
    
    regEx.pattern = cReplEscapeChar
    regEx.Global = True
    regEx.IgnoreCase = True
    aText = regEx.Replace(aText, "")
    
    replaceSpecialCharacters = aText
End Function

Enjoy. 

Rarely I've come across the following problem in Excel VBA. 

Some source code is definitely correct, but it just won't compile. I could one time confirm different Excel versions to be the root cause of the issue. Even though I slightly adjusted the source code in the target version of Excel, the compiler still showed funny error messages.

The trick that did solve the issue in the end was to cut the whole source code, compile and save the empty source code objects (modules, classes, etc..), paste the source code into target objects again and then compile as well as save in the target version of Excel.

From my understanding the file still held bits of the older version of Excel. However, I experienced this also using the same version of Microsoft Access on the same machine. Cutting and then pasting the source code with saving in between did the trick again. 

If you want to export a collection of type Scripting.Dictionary into a text file the below function may come handy.