Sunday, January 16, 2011

How Matt Got His Geek Back

— Matt Harris @ 5:53 pm

A little over a year ago, I purchased a new Dell Studio XPS 8000 computer. Overall, I have been very happy with it, but a few months after I purchased it I tried to write a VBS script and got an error message: “Can’t find script engine VBScript”. Previous experience meant that I needed to install the dll, which is easy enough. Just open a command prompt and type “regsvr32.exe vbscript.dll”.

I did that and received a prompt sayuing “DllRegisterServer in vbscript.dll succeeded.”, which meant that it worked. However, when I tried running the VBS script again, I got the same error message. I spent severeal hours Googling it and trying various things. No luck. Fortunately, nothing critical I have needs VBScript. Also, I figured that it might be fixed in some windows update down the road. It still rankled, though, that my $1300+ PC couldn’t run it.

A week and a half-ago, I was working on a script at work and realized I needed a debugger. I came across VBSedit and tried it out. It seemed to be exactly what I needed, however, I would have to pay the $60 it costs myself, as technically it isn’t needed for my job. I figured that it would be much easier to swallow that if I could use it on my PC at home, so when I got home I checked to see if VBS was still broken. It was.

When I got home, I googled the problem again. This time I had a bit more luck. I found this page on NitroPDF where it mentioned a registry fix if registering the vbscript.dll didn’t work. Using that to refine my search, I found this page on answers.microsoft.com. I checked the registry for the class key mentioned. As mentioned in the thread, I found HKEY_CLASSES_ROOT\CLSID\{B54F3741-5B07-11cf-A4B0-00AA004A55E8}\InprocServer32 pointing to a McAfee directory. Now I knew what the problem was.

One of the first things I did when I bought my PC was to uninstall McAfee. I prefer F-Secure AntiVirus and already had a license for that. I guess that McAfee redirects the vbscript.dll to iteself so that it can catch script virues. When it uninstalls, it doesn’t fix the redirection, so that a) windows thinks vbscript is still installed and b) it points to a non-existant dll. So basically my problem was due to a crappy McAfee unistall program.

I considered just deleted the registry key, but figured McAfee might have left more junk around, so I ran the McAfee cleaner the thread mentioned. I reran the regsvr32.exe vbscript.dll command. This time, when I ran a vbscript – it worked perfectly.

I decided to post this just in case someone else has a problem with “regsvr32.exe vbscript.dll” not working.

P.S. I was doing the above while I was in my regular Wednesday night chat. When it worked, I mentioned to Elais that “I feel like a whole geek” again and summarized what I had done. She replied “How Matt Got His Geek Back”. Hence, the title of this post.

One comment
Monday, October 19, 2009

Adventures In Java – The Perils Of Encoding & Decoding

— Matt Harris @ 8:23 pm

A little while ago, I decided to get off my duff (figuratively speaking) and finally learn how to program Java. I installed the latest version of the JDK and Netbeans three weeks ago, did the usual “Hello, World” type programs and then began working in earnest.

I decided that I wanted to do a fanfiction downloader, similar to this program. I condsidered use Netbeans’ GUI builder, but decided to do the GUI coding by hand for my first project, for educational purposes. It was educational…and a pain.

Anyway, I had the following after a couple of hours of work:

Screenshot of Alpha Fiction Downloader

Once that was done, this weekend I began writing the backend code to download stories from fanfiction.net. After another couple of hours, I had something that would download a story – buggy and no error trapping to speak of, but a pretty decent alpha. As part of my testing, I decided to run it outside of the Netbeans IDE. It didn’t work. The stories would download, but they had gibberish in the place of quotes and some other punctuation.

Having be around the web a few times, I figured this was an encoding issue. What I didn’t understand is why it would work perfectly when I ran the program from inside Netbeans and screw up the encoding when I didn’t. I spent hours playing around with the URL and HttpURLConnection classes, without any success. I then stumbled upon a webpage that mentioned that the Scannerclass (which I used to download the webpage) would default to the system’s charset. I added some code to show the current system charset via the Charset.defaultCharset() method. Turns out the inside Netbeans, the charset is “UTF-8”. When I ran program outside the IDE, the charset was “windows-1252”.

Thinking I had solved the problem, I changed the Scanner constructor to use “UTF-8” encoding. I ran the program outside the IDE and the output html was still messed up, but in a different way. I switched the encoding inside my browser for UTF-8 to windows-1252 and everything looked perfect. Turns out the PrintWriter class also uses the system charset as the default. Added UTF-8 encoding to the PrintWriter constructor and things looked good. I decided to test the output file in Firefox (my default browser is Opera) and it rendered just fine. When I tried it in IE 6.0, more garbage. Opera and Firefox recognized the file as being UTF-8, but IE defaulted to my system settings. Added a couple of lines to the header of the file being generated, specifying that it was UTF-8, and even IE 6 read it fine.

I have to admit, I never expected the IDE to use different encoding internally than the system. Despite the several hours of frustration this caused, I had a fun and educational weekend.

No comments
Sunday, September 10, 2006

Get The Active Control In A UserForm

— Matt Harris @ 11:05 am

Recently, I had the need to find the active control on a UserForm for a dialog box I was creating for Word 2003. The problem was that most of my controls were on a MultiPage control. Whenever the active control was on the MultiPage, UserForm.ActiveControl returned the MultiPage, not the control I wanted.

My solution was to write the following function, GetActiveControl which would essentially ignore MultiPages and Frames to return the “real” active control:

Public Function GetActiveControl(ByRef Source As UserForm) As Control
Dim Ctl As Control
  Set Ctl = GetSubActiveControl(Source.ActiveControl)
  Set GetActiveControl = Ctl
End Function

Private Function GetSubActiveControl(Ctl As Control) As Control
Dim CtlType As String
  CtlType = TypeName(Ctl)
  Select Case CtlType
  Case "MultiPage"
    Set Ctl = Ctl.SelectedItem.ActiveControl
    Set Ctl = GetSubActiveControl(Ctl)
  Case "Frame"
    Set Ctl = Ctl.ActiveControl
    Set Ctl = GetSubActiveControl(Ctl)
  End Select
  Set GetSubActiveControl = Ctl
End Function

As you can see, I use a private recursive function to do the real work of finding the control. I suppose I could have done it some other way, but this seemed to be the most straightforward.

One comment
Monday, January 10, 2005

Inserting Symbol Fonts In A Textbox On A UserForm

— Matt Harris @ 6:28 pm

I recently had cause to develop a UserForm, to be called from Excel, that would allow a user to select a symbol from a group of favorites. Not wanted to use an ActiveX control, I placed 40 textboxes on the form, in rows of 10 each. The idea was I would set the font and character at runtime, reading from a list stored in Excel.

There was a problem. If the font was different from that set at design-time, the symbol would be incorrect, generally showing up as a standard alphanumeric character. After about 2 hours of frustration and searching the web, I found the solution. There is a little documented font property (it wasn’t mentioned in the help files at all) called charset. This property is 0 for normal character sets and needs to be set to 2 for Symbol characters to show up. Example:

Textbox1.Font.Name ="Wingdings"
Textbox1.Font.Charset = 2

Once I did this, the symbols would show up normally.

No comments
Thursday, July 8, 2004

Word, Acrobat and VBA – Oh My!

— Matt Harris @ 8:15 pm

At work, I have a lot of Microsoft Word documents (close to a hundred) that I need to convert into Adobe Acrobat PDF files. Printing them one at a time to Acrobat Distiller was just too mind-numbingly tedious for words. Being the geeky sort, I decided to write a VBA subroutine to distill all of the Word files that were in a single directory with a single button press.

I wrote the code and it seemed to work just fine and dandy. It certainly was very quick. Then a coworker took a look at the PDF documents and discovered that Acrobat Distiller had screwed up the pagination. The strange thing was, Distiller worked just fine from within Microsoft Word. It was only when the VBA subroutine was printing the documents that things were getting screwed up. Obviously there was something wrong with my code. Wouldn’t be the first time that had happened. So back to drawing board I went.

After another half-hour or so, I came to a remarkable discovery. In my initial design, I had decided to set the active printer to Acrobat Distiller from inside the subroutine. This was in order to prevent accidentally printing several hundred pages of documents to a real printer, which had happened in the past. I recorded a macro while manually changing the printer to Acrobat Distiller and took a look at the code the Macro Recorder produced. There was only one line of code, which I took and placed in my subroutine prior to printing:

   ActivePrinter = “Acrobat Distiller”

Turns out that this was the reason the pagination was being screwed up. If I commented out this line and set the active printer via the UI, everything worked just fine.

My remarkable discovery:
The same action when done in VBA can work differently than when done via the user interface, even when using code produced by Word’s Macro Recorder.

And how was your day? 🙂

Update: July 9, 2004 at 5:26 PM
Apparently, it wasn’t setting ActivePrinter in VBA – I am still getting the error whenever I print using code. 🙁

Update: July 12, 2004 at 2:57 PM
Also, it happens when I print to Distiller from Explorer.

Update: July 19, 2004 at 8:15 PM
Adding a single space before the carriage return at the offending sections seems to correct the problem.

No comments
Tuesday, June 15, 2004

What I Did At Work Today

— Matt Harris @ 7:07 pm

Wrote some one-shot VBA code to move stuff around between a couple of Excel worksheets:

Public Function CompareTT(T_Div As String, T_ST As String, T_Add As String, _
                          S_Div As String, S_ST As String, S_Add As String) As Integer

'Returns -1 for "<", 0 for "=", 1 for ">" .
Dim Temp As Integer<br>

T_Div = Trim(LCase(T_Div)): T_ST = Trim(LCase(T_ST)): T_Add = Trim(LCase(T_Add))
S_Div = Trim(LCase(S_Div)): S_ST = Trim(LCase(S_ST)): S_Add = Trim(LCase(S_Add))

Temp = StrComp(T_Div, S_Div, vbTextCompare)

If Temp <> 0 Then
  CompareTT = Temp
  Exit Function
End If

Temp = StrComp(T_ST, S_ST, vbTextCompare)
If Temp <> 0 Then
  CompareTT = Temp
  Exit Function
End If

CompareTT = StrComp(T_Add, S_Add, vbTextCompare)

End Function

Public Sub CopyTT()
Dim Source As Range, Target As Range
Dim SourceRow As Integer, TargetRow As Integer
Dim CompareValue As Integer

Dim TempFormula As String
Dim SFormulaReplace As String
Dim TFormulaReplace As String

Const cDIV As Integer = 1
Const cST As Integer = 5
Const cAddress As Integer = 2
Const cHardWare As Integer = 13
Const cYearBuilt As Integer = 17
Const cSqFt As Integer = 19
Const cOccupancy As Integer = 21
Set Source = ThisWorkbook.Worksheets("Source").Cells
Set Target = ThisWorkbook.Worksheets("Target").Cells

SourceRow = 4: TargetRow = 4

Do While Source(SourceRow, cDIV) <> ""
  CompareValue = CompareTT(Target(TargetRow, cDIV), _
                           Target(TargetRow, cST), _
                           Target(TargetRow, cAddress), _
                           Source(SourceRow, cDIV), _
                           Source(SourceRow, cST), _
                           Source(SourceRow, cAddress))
  Select Case CompareValue
    Case -1
      TargetRow = TargetRow + 1
    Case 0
    'Calculate new formula
    TempFormula = Source(SourceRow, cHardWare).Formula
    SFormulaReplace = "Y" & SourceRow
    TFormulaReplace = "Y" & TargetRow
    TempFormula = Replace(TempFormula, SFormulaReplace, TFormulaReplace)
    
    Target(TargetRow, cHardWare).Formula = TempFormula
    Target(TargetRow, cYearBuilt) = Source(SourceRow, cYearBuilt)
    Target(TargetRow, cSqFt) = Source(SourceRow, cHardWare)
    Target(TargetRow, cOccupancy) = Source(SourceRow, cOccupancy)
    
    TargetRow = TargetRow + 1
    SourceRow = SourceRow + 1
    Case 1
      SourceRow = SourceRow + 1
  End Select
Loop

Set Source = Nothing: Set Target = Nothing
End Sub

Sure it’s a hack, but it worked. It certainly beat looking through 500 rows of excel that span 30 columns and manually copying everything over.

No comments
Archives

  • 1530s Europe Campaign (15)
  • Books (7)
  • BTVS & Angel (24)
  • Cormaria (22)
  • Doctor Who (1)
  • Fanfiction Recs (19)
  • General (36)
  • Harry Potter (19)
  • Humor (16)
  • Java (1)
  • Microsoft Office (2)
  • Northridge Pathfinder (24)
  • OpenOffice.org (1)
  • Pathfinder (30)
  • Politics (4)
  • Programming (6)
  • Programs (5)
  • Reviews (2)
  • Role-Playing Games (D&D et. al.) (85)
  • Science (1)
  • Site News (120)
  • Smallville (2)
  • Stargate (2)
  • Tips (4)




  • Powered by Wordpress