Grand love to all reading, (<- I’m in a really good mood today :D)
Well I got a “What the hell man” moment in VB that I thought I’d share. Both for entertainment (I’m bored too) and in hope that this is just some common known thing in VB coding and there is an easy solution to it.
OK, so here’s the situation: I’m writting a RTF viewer that is meant to open a RTF file and display it when a user logs in. It’s basically a “Message of the day” type app. The whole program (the editor and viewer) basically work but I got a strange bug that I’ve narrowed down to one line of code.
The viewer program has the ability to take in variables through command line in URL-encoded like format.
Eg: “MOTDViewer.exe -variable=value&etc=ongoing” passes variable=value and etc=ongoing into the viewer.
These variables are used in the viewer to replace words. The variables are written in the message in the format <#variable>. This is so the message can be made to change slightly when opened and be customised to the individual user.
Eg:
Hello <#name>,
Your computer name is: <#computer>.
Execute: MOTDViewer.exe -name=Allan&computer=27-Printer Room.
Output:
Hello Allan,
Your computer name is: 27-Printer Room.
OK NOW FOR THE BUG! For some reason when the program is replacing the <#variables> with the values it’s removing the formatting of the message. Specifically it’s this function:
Msg.Text = Msg.Text.Replace("<#" & Search & ">", Replace)
If I comment this out, I stop my replacing of the words, but the formatting stays untouched. If I let this run then all the formatting of the text, including font-size, font, boldness, italic … everything for the whole message is set to the font of the first character in the message.
Here’s a little bit more of the code if your interested:
[SIZE=2]Status = [/SIZE][SIZE=2][COLOR=#800000]"Info"[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]' Get the parameters based through command line if available.[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]' URL-Encoded format. Var_1=Val_1&Var_2=Val_2&... etc etc[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Len(Command()) > 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][INDENT][SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE][INDENT][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Parameters [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/INDENT][INDENT][SIZE=2]Parameters = Mid(Command(), 2, Len(Command()))[/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Replacement [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Search [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Replace [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] i = 1 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] CountWords(Parameters, [/SIZE][SIZE=2][COLOR=#800000]"&"[/COLOR][/SIZE][SIZE=2])[/SIZE][INDENT][SIZE=2]Debug.Print([/SIZE][SIZE=2][COLOR=#800000]"i="[/COLOR][/SIZE][SIZE=2] & i)[/SIZE][/INDENT][INDENT][SIZE=2]Replacement = FindWord(Parameters, i, [/SIZE][SIZE=2][COLOR=#800000]"&"[/COLOR][/SIZE][SIZE=2])[/SIZE][/INDENT][INDENT][SIZE=2]Search = FindWord(Replacement, 1, [/SIZE][SIZE=2][COLOR=#800000]"="[/COLOR][/SIZE][SIZE=2])[/SIZE][/INDENT][INDENT][SIZE=2]Replace = FindWord(Replacement, 2, [/SIZE][SIZE=2][COLOR=#800000]"="[/COLOR][/SIZE][SIZE=2])[/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Search = [/SIZE][SIZE=2][COLOR=#800000]"status" [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][INDENT][SIZE=2]Status = Replace[/SIZE][/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2]Msg.Text = Msg.Text.Replace([/SIZE][SIZE=2][COLOR=#800000]"<#"[/COLOR][/SIZE][SIZE=2] & Search & [/SIZE][SIZE=2][COLOR=#800000]">"[/COLOR][/SIZE][SIZE=2], Replace)[/SIZE]
[/INDENT][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2] i[/SIZE]
[/INDENT][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] Ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception[/SIZE][INDENT][SIZE=2]MsgBox([/SIZE][SIZE=2][COLOR=#800000]"... something bad happening while trying to insert the parameter words, don't know what though"[/COLOR][/SIZE][SIZE=2], MsgBoxStyle.OkOnly, [/SIZE][SIZE=2][COLOR=#800000]"Error... dang."[/COLOR][/SIZE][SIZE=2])[/SIZE][/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]' Affects of Status[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Status = [/SIZE][SIZE=2][COLOR=#800000]"MUSTREAD" [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][INDENT][SIZE=2]Time = [/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Computer.Clock.TickCount[/SIZE][/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
So it works… but replacing text kills the formatting, so like, what the hell man?
PS: If I’m doing this the wrong way PLEASE tell me, I want to know the right way. I started working at a new job only a couple weeks ago and they’ve asked me to make this for them. The problem is I come from a mostly C++/PHP background… and I rocked up on the first day to be told “We need you to do it in VB”… I was like “Yea sure, no problem”… and now I’m trying to learn VB and become pro at it in a couple of days. SO ANY HELP IS WELCOME! Please post any suggestions on how to fix this.