VBA in FrontPage

Adding Code Samples

Back when I was using FrontPage in the Preview version of Office 2000, I was doing the ATL examples for my web site.  I was putting the code samples inside <blockquote> and <pre> tags.  What I was doing was switching to HTML view and entering the HTML around the code which I had pasted in.  I also had to do a lot of manual formatting on the code listings after I put the tags around it.  The trick is to put the tags in first, then paste the code listings in.  I was asking on one of the newsgroups, if there was an easier way to do this.  Someone sent me this Macro:

Sub WrapBqPre()
    Dim objTxtRange As IHTMLTxtRange
    Dim temp As String
    'This will surround the html of the selection
    'with pre and blockquote start and end tags.
    Set objTxtRange = ActiveDocument.selection.createRange
    temp = objTxtRange.htmlText
    temp = "<blockquote><pre>" & temp & "</pre></blockquote>"
    objTxtRange.pasteHTML temp
End Sub

I was still changing the font using the font panel in FrontPage, after I had the code pasted in, which gave me: "<blockquote><pre><code><strong><font color="#800000" size="4">".  Attempts to add this to the Macro failed.  As you will see below, this was due to how I was running the Macro to insert the HTML.

Checking my copy of "HTML & XHTML" to see what those tags really mean, and trying a few things, I came up with this Macro for wrapping my code samples.

Sub WrapSampleCode()
    Dim objTxtRange As IHTMLTxtRange
    Dim temp As String
    ' This will surround the html of the selection
    ' with pre, strong and font tags.
    Set objTxtRange = ActiveDocument.selection.createRange
    temp = objTxtRange.htmlText
    temp = "<pre><strong><font color=""#800000"" size=""4"">" _
        & temp & "</font></strong></pre>"
    objTxtRange.pasteHTML temp
End Sub

I ran into some strange behaviour while testing this Macro.  If  I just clicked on the page then run the Macro, all I got was the <pre></pre> tag.  It seems that FrontPage determines that the rest doesn't do anything so optimizes it away.  By entering some text, highlighting it, then running the Macro, gives the desired results.  The code listing can then be pasted in to replace the text.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“My definition of an expert in any field is a person who knows enough about what's really going on to be scared.” - P. J. Plauger