Strip illegal character from filename

  • Thread starter Thread starter ExcelLars
  • Start date Start date
E

ExcelLars

Is it possible to do this with less code?

EmneRensket = Rensket(Emne, ":")
EmneRensket = Rensket(Emne, ";")
EmneRensket = Rensket(Emne, ".")
EmneRensket = Rensket(Emne, ",")
EmneRensket = Rensket(Emne, "\")
EmneRensket = Rensket(Emne, "/")
EmneRensket = Rensket(Emne, "*")
EmneRensket = Rensket(Emne, "[")
EmneRensket = Rensket(Emne, "]")
EmneRensket = Rensket(Emne, "?")
EmneRensket = Rensket(Emne, "!")
EmneRensket = Rensket(Emne, "'")
EmneRensket = Rensket(Emne, "<")
EmneRensket = Rensket(Emne, ">")
EmneRensket = Rensket(Emne, "|")

Function Rensket(text As String, illegal As String) As String
Dim Ch As String
For i = 1 To Len(illegal)
Ch = Mid(illegal, i, 1)
While InStr(text, Ch) > 0
text = Left(text, InStr(text, Ch) - 1) & Right(text, Len(text)
- InStr(text, Ch))
Wend
Next
Rensket = text
End Function
 
e.g.:

Sub ReplaceChars(Text$)
Dim ar()
Dim i&
Dim ReplaceBy$
ReplaceBy = ""
ar = Array(";", ":", ",")
For i=0 To Ubound(ar)
Text = Replace(1, Text, ar(i), ReplaceBy, vbTextCompare)
Next
End Sub

--
Best regards
Michael Bauer - MVP Outlook

: Outlook Categories? Category Manager Is Your Tool:
: <http://www.vboffice.net/product.html?pub=6&lang=en>


Am Mon, 19 May 2008 01:10:23 -0700 (PDT) schrieb ExcelLars:
 
looks very nice, but will not work for me.
.......
Dim ar()
Dim i&
Dim ReplaceBy$
ReplaceBy = "_"
ar = Array(";", ":", ",", "\", "/", "*", "[", "]", "?",
"!", "'", "<", ">", "|", "$")
For i = 0 To UBound(ar)
Emne = Replace(1, Emne, ar(i), ReplaceBy, vbTextCompare)
Next

filnavn = Mdato & " " & Avsendernavn & " " & Emne & ".MSG"
txtPath = TextBox1.Value

myOlSel.Item(x).SaveAs txtPath & filnavn, olMSG 'export
message
 
If errors are expected and you doesn't get one, you might turn off the On
Error Resume Next statement...

I wrote the function wrong, instead it's:

Text = Replace(Text, ar(i), ReplaceBy, , , vbTextCompare)

--
Best regards
Michael Bauer - MVP Outlook

: Outlook Categories? Category Manager Is Your Tool:
: <http://www.vboffice.net/product.html?pub=6&lang=en>


Am Tue, 20 May 2008 14:29:44 -0700 (PDT) schrieb ExcelLars:
 
Back
Top