Monthly Archives: September 2010

ShiftThisSwift SMTP Configuration for WordPress

3
Filed under Blogging, WordPress

I’m in the throes of conversion from dasBlog (a great ASP.net based blogging engine, btw) to WordPress, as you can no doubt tell from the sometimes random gobbledygook littering these pages!

One bit that was particular annoying was that when comments come in, the administrator is supposed to be emailed a notification of such. But this wasn’t working.

I hit all the pages on Google pertaining to the problem (and it has been a problem for some time, apparently, centered around hosting WordPress on various webhosts), but still no luck.

Finally, I came across a mention of the ShiftThis Swift SMTP Plugin for WordPress. Looked like exactly what I was needing.

A quick download (and upload) later, and I was looking at the configuration screen.

Unfortunately, my first set of config didn’t work. Actually, it through a big, nasty exception all over the screen.

So did rounds 2 and 3. Round 4 got a little closer, the exception indicated that “authorization had failed for the username and password”.

Better.

Eventually, I worked out this set of config values (I’ve annoted the config screen below), and they DO seem to work just fine with my webhost (hosting under Windows).

image

Of course, your mileage may vary.

The key points for me, anyway, were:

  • The Server Address. It needs to be the name of the SMTP server OF THE WEB HOSTING MACHINE your website is hosted on. For instance, if WordPress is configured for the site MyDomain.com, this will likely be smtp.mydomain.com.
  • The Username. For me, it needed to be the FULLNAME (with the @domain.com part) of a mailbox configured ON THE WEB HOSTING MACHINE your website is hosted on. Following from the above example, I might use mail@mydomain.com
  • The Password. Even though my mailbox has a password, I had to leave this blank, or authentication would fail every time.

And there you have it!

Code Garage – Escaping and UnEscaping XML Strings

4
Filed under Code Garage, XML

image If you work much with XML, eventually, you’ll end up needing to take raw string data and either Escape it (replace characters that are illegal in XML in the string with legal XML markup) or UnEscape it (replace any XML markup in the string with the represented characters).

For instance, a “>” (greater than) symbol is illegal in the content of an XML node, and must be represented by >

There are any number of approaches out there to handle this. Most involve simple brute force string search and replaces. They work, but I thought there must be a more elegant (and already thought out) solution to this problem.

However, after quite a bit of searching, I gave up and wrote my own, making use of the XML functions already defined in the .net framework.

To ENCODE a string into legal XML node content…

    Public Function EncodeXML(ByVal s As String) As String
        If Len(s) = 0 Then Return ""
        Dim encodedString = New StringBuilder()
        Dim writersettings = New System.Xml.XmlWriterSettings
        writersettings.ConformanceLevel = Xml.ConformanceLevel.Fragment
        Using writer = System.Xml.XmlWriter.Create(encodedString, writersettings)
            writer.WriteString(s)
        End Using
        Return encodedString.ToString
    End Function

To reverse the process and Decode a string…

    Public Function DecodeXML(ByVal s As String) As String
        If Len(s) = 0 Then Return ""
        Dim decodedString As String = ""
        Dim readersettings = New System.Xml.XmlReaderSettings
        readersettings.ConformanceLevel = Xml.ConformanceLevel.Fragment
        Dim ms = New System.IO.StringReader(s)
        Using reader = System.Xml.XmlReader.Create(ms, readersettings)
            reader.MoveToContent()
            decodedString = reader.ReadString
        End Using
        Return decodedString
    End Function

No, I haven’t performed any exhaustive performance measures on these. I’ve generally not used them in any kind of ultra high volume situations, but they’re clean, simple and leverage existing code that already performs the necessary functions.

Software Design Pattern Quick Reference

5
Filed under Software Architecture, VB Feng Shui

I’m a somewhat reluctant believer in software design patterns. On the one hand, it can be handy to have a lingua franco to discuss common patterns with. On the other, the concept has seemed to me to be so wildly hyped over the past few years, that you’d think it was the brand new answer to everything.

Truth is, I’m guessing most every programmer who’s been working for any length of time recognizes most of the common patterns, and has applied them innumerable times over the years.

Still, I’ve never spent much time truly digging into the meat and potatoes of each pattern.

Then I came across Jason McDonald’s Design Pattern Quick Reference PDF here.

image

To the right is a reduced view of one of the pages.

Really well done reference sheets for those that want to put the names with the patterns they likely already know quite well.

Highly recommended!

Word and the Disappearing Cell

1
Filed under Office, Troubleshooting, VB Feng Shui

image

So I was off working on a new bit of code today when an email came through:

“The tool is blanking out a cell when it’s not supposed to!”

Ack.

No big deal though. I fire up the project, load up the document in question and give it a run.

Sure enough, a cell that contains some text and a merge field (under my control) is being completely cleared out. Only the merge field should be being cleared.

I dig a little more and eventually narrow the culprit down to a single function.

Private Function MyFunc(Rng as Range)
   ....
   Rng.Delete
   ....
End Function

When I hit that Rng.Delete line, the ENTIRE CELL was being cleared. Including text that had nothing to do with my merge field (other than the fact that it was also in the same table cell).

After a bit more single stepping through code, I discovered that the Range being deleted was actually empty. In other words, it’s Start position was the same as it’s End position.

Now, I’ve know from dealing with Word for far too many years that when you’re working with an empty range like that, there are a good number of things that don’t work quite right. Many will throw errors, but I was getting no error here.

Then it hit me. As a test, I added a few chars of text at the VERY END of the cell, after my merge field.

Sure enough, everything worked, even though the range being deleted was still empty.

The End-of-Cell Marker.

Word stores an End-of-Cell marker character at the end of each cell in a table. The marker is a Chr(7) if you retrieve it, but you wouldn’t normally ever even bother with it.

However, if you end up with an empty Range object that is situated right at the end of a cell, then that Range is effectively pointing at the End of Cell marker, and if you invoke the Delete method on the Range, you’ll end up invoking it on the End of Cell Marker.

And what happens if you try and delete an End of Cell Marker?

Well, duh. The cell is cleared.

Moral of the Story

The short version of all this is simple:

If you plan on deleting a Range, make sure that the Range.Start <> Range.End before you do:

If Range.Start <> Range.End Then
   Range.Delete
End If

Technically speaking, if start = end, then there’s nothing in the range anyway and so there shouldn’t be anything there to delete. But doing so can definitely have detrimental effects in some cases.

And, as usual, your mileage may vary.