[aside: I have to remember to review these articles for any tricks that’ll help me troubleshoot/improve the VBA-to-VSTO conversion I’m doing for Word2MediaWiki++…]
…and as a catch-all:
[aside: I have to remember to review these articles for any tricks that’ll help me troubleshoot/improve the VBA-to-VSTO conversion I’m doing for Word2MediaWiki++…]
…and as a catch-all:
[Previous articles in this series: Prologue, Part 1, Part 2, Part 3, Part 4, Part 5, Part 6, Part 7, Part 8, Part 9, Part 10, Part 11 (The Return), Part 12 (Initialization continued), Part 13 (VBA Oddities).]
In working through the code in MediaWikiConvert_Lists(), I ran across a block of code that purports to “replace manual page breaks”, and is using the Chr(11) construct to do so. I must’ve been feeling extra-curious ’cause I went digging into what this means, and the harder I looked, the more puzzled I became.
According to ASCIITables.com, the character represented by decimal “11” is the so-called “vertical tab”. I’ve never heard of this before (but then, there’s a whole host of ASCII & Unicode characters I’ve never paid attention to before), so I had to check with a half-dozen other references on the ‘net before I was sufficiently convinced that this wasn’t some “off-by-one” problem where the VBA coders were intending to look for Chr(10) (aka “line feed”) or Chr(12) (aka “form feed”).
On the assumption that we’re really and truly looking for “vertical tab”, I had to do some deep digging to figure out what this might actually represent in a Word document. There’s the obligatory Wikipedia entry, which only said that “The vertical tab is  but is not allowed in SGML (including HTML) or XML 1.0.”. Then I found this amusing reference to one of the Perl RFCs, which quotes Russ Allbery to say “The last time I used a vertical tab intentionally and for some productive purpose was about 1984.”. [Sometimes these quotes get better with age…]
OK, so if the vertical tab is so undesirable and irrelevant, what could our VBA predecessors be thinking? What is the intended purpose of looking for an ASCII character that is so unappreciated?
I started to notice these odd little appendages growing out of some of the newer code in the VBA macro. At first I figured there must be some special property of VBA that makes “If 1=2” a valid statement under some circumstances, and I just had to ferret out what that was.
Instead, the more I looked at it, the more puzzled I became. What the hell could this possibly mean? Under what circumstances would *any* logical programming language ever treat “If 1 = 2” as anything but a comparison of two absolute numbers, that will ALWAYS evaluate to False?
Eventually I had to find out what greater minds that mine thought about this, and so off to Google I go. As you might expect, there’s not much direct evidence of any programming practices that include adding this “If 1 = 2” statement. In fact, though it appears in the odd piece of code here and there, it’s surprisingly infrequent. However, I finally ran across what I take to be the best lesson on what this really means (even if I had to unearth it through the infamous “Google cache”):
>>>Anyone know how to comment out a whole section in VBA rather than just
>>>line by line with a ” ‘ “?
>>If the code is acceptable (won’t break because some control doesn’t
>>exist, etc), I sometimes to
>> If 1 = 2 then
>> ….existing code
>> End If
>>The code will never fire until the day 1 = 2.
> Thanks, think Id prefer the first option. The second option might
> confuse any programmers that try and read my code.
Now that’s the understatement of the year.
So as far as I’m concerned, I’m going to go back and comment out any and all instances where I find this statement, as it tells me the original programmer didn’t want this code to fire, and was thinking of coming back to it someday after their last check-in.
There are a few routines that attempt to implement localization at runtime. While this makes sense for VBA, this makes little if any sense for the use of VB.NET. Any English-only strings can be substituted in the corresponding Resources file that will accompany this code.
Thus, the MW_LanguageTexts() routine will be skipped, since it had little if any effect anyway.
I’ve been struggling for a few days to try to actually run this add-in, and after finding out why, I can say with confidence that there was no good troubleshooting guide for this.
Here’s the setup:
Could not create an instance of startup object Word2MediaWiki__.ThisAddIn in assembly Word2MediaWikiPlusPlus, Version=22.214.171.124, Culture=neutral, PublicKeyToken=1a75eafd9e81be84.
************** Exception Text **************
Microsoft.VisualStudio.Tools.Applications.Runtime.CannotCreateStartupObjectException: Could not create an instance of startup object Word2MediaWiki__.ThisAddIn in assembly Word2MediaWikiPlusPlus, Version=126.96.36.199, Culture=neutral, PublicKeyToken=1a75eafd9e81be84. —> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. —> System.NullReferenceException: Object reference not set to an instance of an object.
at Word2MediaWiki__.Word2MediaWikiPlusPlus.Convert..ctor() in C:\VS2005 Projects\Word2MediaWiki++\Word2MediaWiki++\Convert.vb:line 44
at Word2MediaWiki__.ThisAddIn..ctor(IRuntimeServiceProvider RuntimeCallback) in C:\VS2005 Projects\Word2MediaWiki++\Word2MediaWiki++\ThisAddIn.vb:line 29
— End of inner exception stack trace —
If I tried to load the add-in from within Word (using the Tools > COM Add-ins… menu — which you can add with these instructions), Word would only tell me:
Load Behavior: Not loaded. A runtime error occurred during the loading of the COM Add-in.
I won’t even bore you with the details of all the stuff I tried to do to debug this issue. It turned out that I was instantiating my Application object too early in the code (at least, the way I’d constructed it).
ThisAddin.vb (relevant chunk)
Imports Office = Microsoft.Office.Core Imports Word2MediaWiki__.Word2MediaWikiPlusPlus.Convert Public Class ThisAddIn #Region " Variables " Private W2MWPPBar As Office.CommandBar WithEvents uiConvert As Office.CommandBarButton WithEvents uiUpload As Office.CommandBarButton WithEvents uiConfig As Office.CommandBarButton Dim DocumentConversion As Word2MediaWikiPlusPlus.Convert = New Word2MediaWikiPlusPlus.Convert ' Line 29 #End Region
Convert.vb (relevant chunk)
Imports Word = Microsoft.Office.Interop.Word Namespace Word2MediaWikiPlusPlus Public Class Convert #Region "Variables"Dim App As Word.Application = Globals.ThisAddIn.Application 'PROBLEM - Line 44 Dim Doc As Word.Document = App.ActiveDocument 'PROBLEM#End Region#Region "Public Subs" Public Sub InitializeActiveDocument() If Doc Is Nothing Then Exit Sub End If
…End Sub#End Region
#Region “Public Subs”
Convert.vb (relevant chunk)
Imports Word = Microsoft.Office.Interop.Word Namespace Word2MediaWikiPlusPlus Public Class Convert #Region "Variables" Dim App As Word.Application 'FIXED Dim Doc As Word.Document 'FIXED #End Region #Region "Public Subs" Public Sub InitializeActiveDocument() App = Globals.ThisAddIn.Application 'NEW Doc = App.ActiveDocument 'NEW If Doc Is Nothing Then Exit Sub End If … End Sub #End Region
As much as I understand of this, it seems like when the ThisAddIn class tries to create a new instance of the Convert class as a DocumentConversion object, the ThisAddIn object hasn’t been instantiated yet, so the reference in the Convert class to Globals.ThisAddIn.Application can’t be resolved (how can you get the ThisAddin.Application object if its parent object — ThisAddIn — doesn’t exist yet?) causes the NullReferenceException that is the heart of the problem.
By pulling out that instantiation code from the App variable declaration, and delaying it instead to one of the Convert class’s Subs, there was no need for the managed code to “chase its tail” — trying to resolve an object reference back through the calling code, which hadn’t been instantiated yet.
Y’know, I’m sure I read somewhere over the last year that combining the declaration with the instantiation of a variable is bound to lead to subtle debugging issues, but man. Losing three days to this? What a disaster.
Lesson for the day: It never pays to take shortcuts.
I’m an avid user of Attensa for Outlook, a free Outlook add-in for aggregating RSS feeds as folders of “messages” in Outlook. I like it because it (a) allows me to search my feeds quickly via Windows Desktop Search, and (b) lets me read my feeds whether I’m connected to the ‘net or not.
However, there isn’t currently a free way to read my feeds via a web browser (e.g. from my new iPhone – hee hee!). Well, I should say I can read my feeds via Google Reader, but my read/unread status doesn’t get sync’ed from Attensa to Google or back. That means if I bravely skim through a bunch of articles in one place, I’ll likely have to wade through them (or get distracted by them) again in the other.
I had a brainwave today (stand back, that could be contagious) about how to add functionality to be able to sync back & forth, and I think I’ve just dreamt up yet another coding project for myself:
I have a pretty reasonable idea how to write managed C# or VB.NET that can integrate with Office via the Visual Studio Tools for Office model. I’m not unfamiliar with web services, or with the basics of a .NET-based HTTP client [having just wasted a weekend authoring a very rudimentary web site parser]. I am bright enough to imagine that the Attensa add-in exposes a more abstract approach to addressing feeds & articles than just crawling the raw PST file, enumerating folders and addressing message objects directly.
Now what I’d need to know is: is there an Attensa SDK and/or API which I could leverage in an Outlook application add-in using VSTO? Would there be any advantage to using that abstraction layer, as opposed to just enumerating the PST folders and messages directly? If the Attensa team only exposed an unmanaged API, would I be creating a performance nightmare to code through that (with all the PInvoke‘ing that is required) rather than just take my chances with the native Outlook object model?
I can even imagine that the Attensa client might provide me a way of finding the translation between “articles from feed ‘x'” and “messages in folder ‘y'”, that relied on Attensa’s internal database, and then I could grind through the Outlook folders themselves. That’d be a damn sight easier than trying to match up (a) feeds from the Google Reader API (article, wiki) to the folders as they’re named in the PST file, and (b) articles from the Google Reader API to the messages stored in the PST file. It’d sure help if there was an indexed search capability in (a) the Google Reader API and (b) the Outlook PST object model.
Oh, it’s fun to imagine all the ways I could make my life easier…after six months of hard dev work to get there. Madman I am.
There’s a more-complicated-than-it-probably-needs-to-be subroutine in the Word2MediaWikiPlus codebase — called MW_SurroundHeader() — that seems to only be there to cleanup and reformat text in a Word document that has one of the Headings styles. It uses a function from VBA called simply String(), which is one of the first cases of a VBA function for which I cannot find an equivalent in VB.NET.
It turns out I found out what I needed from an oreilly.com article, and after running into a few brick walls in looking for a reference to this in MSDN, I started a more intelligent search. I kept coming back to references to the String Data Type, so I next looked at the “Strings in Visual Basic” topic that was referenced by “For more information on string manipulation…”. From there the next most logical leap was to “Building Strings in Visual Basic“, which led to “How to: Create Strings Using a StringBuilder in Visual Basic“.
Once there, I figured that since this was so helpful to me, I’d like to save someone the trouble next time so I added a little of that “Community Content” sauce that I myself appreciate so much.
The MW_FontFormat() subroutine also uses a no-longer-supported VBA-ism, the Selection object. This isn’t all that well documented online either — or at least, I wasn’t able to find anything useful online to help figure out how to translate this into VB.NET. The best I could find was a mention that the Range object in VB shares some common methods & properties with the Selection object in VBA.
However, I happened to have a copy of an old book called the Microsoft Office XP Developer’s Guide, which was surprisingly results-oriented for an MSPress book. Pages 176-177 actually discuss “The Selection Object vs. the Range Object”, in which I am told that the Range object is actually superior to the Selection object, and should always be favoured wherever possible.
I’m not feeling up to the subtleties of Selection vs. Range right now, so I’ll leave this for another time.
This is another interesting puzzler… It seems that MediaWikiConvert_FontColors() calls RGB2HTML(), which calls OleConvertColor(), which calls OleTranslateColor(), which is a p/invoke to OLEAUT32.DLL. [Man, this is starting to read like a book of the Old Testament…]
I have a really strong gut instinct that there’s a managed code equivalent to this that will make the intended conversion in one step, and I intend to find it. There’s no good reason at this point to (a) have this many calls going on the stack, just to get access to a “simple” math function, or (b) to preserve an unmanaged call just because it’s been used all the way up to now.
I can think of at least three ways to try to find the managed class I’m after: search on OleTranslateColor, search on “RGB & HTML”, or start browsing books on managed web development.
According to this “Format Color for HTML” article, the call to OleTranslateColor is only necessary in cases where you’re using “system color constants” or “palette indices”. Since we’re getting very predictable input here that doesn’t appear to be using either of these two alternatives, right away we should be able to eliminate the unmanaged code.
That is, if I’m reading this right, then I should just be able to remove OleConvertColor() from the initial call in RGB2HTML() and leave the first line of code as
nRGBHex = Right("000000" & Hex(rgbColor), 6)
However, upon double-checking, it seems that other code blocks on the VBA macro are passing in some of the Word.WdColor enumeration constants — which I assume are equivalent to “system color constants”.
Rather than have the RGB2HTML() routine always thunk down to unmanaged code, it’d be smarter if we checked whether the color value of interest is a member of the Word.WdColor enumeration. But do the routines that generate the input parameter to RGB2HTML() generate either Long or WdColor values? Or alternatively, would the code implicitly convert from WdColor to Long as the RGB2HTML() routine initialized? I didn’t notice any overloaded instances of RGB2HTML() that took the input parameter as a WdColor value, so I have to assume that no matter what goes on outside this routine, all operations inside RGB2HTML() will only operate on colors of type Long.
If that assumption is correct, then we should be able to safely ignore the possibility that the input parameter may start out as a WdColor datatype, and that means we can safely eliminate the OleConvertColor() and OleTranslateColor() routines. [For the moment, having already had to dig them back up once, I’ll just comment them out and leave myself a note to delete them once I’ve had time to test these colour conversions and confirm this assumption is true.]
A more interesting question, however, is whether we’re losing colour fidelity in the conversions being performed here. According to VSTO For Mere Mortals, Chapter 4, “In VBA, colors are of type Long, and there are eight constants that can be used… In Visual Studio 2005, colors are of type Color, and there are more than 100 choices”.
Is it possible that the calls being used to derive the colours from the Active document are limited to the VBA colour constants, and that I should be looking to switch to other calls that return the .NET Color constants? I’ll just add this as another Task to the CodePlex project list, and deal with it later — it seems to me like this is hardly the biggest problem facing this Addin at the moment.
Much of this function seems to repeat the actions taken in Word2MediaWikiPlus(), so it’s a bit weird to see it done here as well (since this function is called explicitly by the other). While some of it can be immediately discarded, other bits have to be examined more closely – mostly because they’re poorly documented (at least at the point from which they’re being called):
The more I look at this image extraction code, the more complicated it gets. At this point I’ve pretty much determined that, for all the effort it’ll cost to implement these image features, it’s just not worth the trouble in v1. I’ll continue to add TODO: comments to the VSTO add-in to show where the image code will eventually go, but I’m not going to do any further work to understand the image code until the rest of the Add-in is working.
Finally, there are the control characters that are being assigned (^l, ^m, ^p, ^s). They’re not documented in the code, and I’m having a hard time finding any documentation that discusses the use of these control characters. It doesn’t help that Google and MSDN Search don’t seem to allow you to search on “^p” — it seems they treat this as either “p” or “<p".
I believe I could treat these as global constants in the Convert class, but what isn’t clear is whether these control characters are:
Aha! After trying over & over, I finally came up with a search in Microsoft’s Knowledge Base that gave me an article talking about the “^p” (which it calls a “paragraph mark”):
These appear to be ancient character sequences (as early as Word 1.0), so I’m going to first try using the native Word enumerations for these character strings wherever possible. If I have to go back to using these character sequences, then I’ll drop them back in to the Convert class as Constants.
Aside: today I stumbled on an invaluable reference: the Microsoft Word Visual Basic Reference online. This implies it’s an authoritative reference for all VBA available in Microsoft Word. Should prove useful.
From what I can tell by a single read through this routine’s code, this all appears to affect the ActiveDocument. That means all this code can go into the InitializeActiveDocument() subroutine (which I’ve conveniently already defined).
'Now, if we might have some problems, if we are in a table pg.Range.Select If Selection.Information(wdWithInTable) Then Selection.SplitTable
If GetReg("convertPageHeaders")... EndIf
It's just hard to guess what the programmer's intentions were with a never (rarely?) called piece of code.
Then there’s a lot of boring code conversion, where I’m just giving methods and variables more meaningful names, adding appropriate prefixes to all the Word enums being used, and just commenting the crap out of things where I don’t have a clue how to fix some weird or cryptic code routines.
The most interesting thing I’ve had to research so far was the problem I created for myself by implementing the code into two classes (so far). I finally got around to calling the Convert class’ public methods in the ThisAddin class’ uiConvert_Click() handler. As the naive little programmer that I am, I of course first tried to just set the Imports statement at the top of the ThisAddin class, and then call the public methods “naked” like so:
InitializeActiveDocument() InitializeConversion() PerformConversion()
Of course that didn’t work, but I didn’t know why at the time. Instead, I scratched my head for quite a while over how to handle the compiler warning “Error 232: Reference to a non-shared member requires an object reference“.
I’ve run up against this before, and I’m pretty sure I was lured at the time down the path to hell: I started adding Shared declarations all over the place. It’s really tempting — when the IDE implies you should try an easy fix like this, it’s hard to know why this should be bad. “Didn’t the IDE’s developers know what they were doing?” “Why would they lead morons like me astray?”
Unfortunately, this is akin to tugging at that first loose strand of a nice wool sweater: pretty soon I’d added so many additional Shared declarations that I’m sure the code was wide open to all sorts of future, stealthy issues I have no idea about.
This time around, once I saw that one Shared begat yet another implied request to add another Shared declaration, I stopped and did some further digging around. While I wasn’t able to find any articles or MSDN docs that really spelled it out for me, I think I figured out a worthy approach on my own. [This forum thread was as good as any.]
I’ve published the following as Community Content to the “Error 232” page on MSDN.
While this error message tempts the inexperienced programmer with the “easy” solution of just adding the Shared keyword to the requested Method, I advise strongly against it. Unfortunately there’s little documentation or advice out there aimed at the programmers like me who don’t really understand the problems they’ve created, nor the trade-offs in the possible solutions being (cryptically) recommended. Hopefully this’ll help out other folks like myself avoid the really nasty mistake I’ve already made a few times.
The trouble with adding the Shared keyword to a second Class’ Method is that it rarely stops there. Once you’ve shared a method, whether Public, Private or otherwise, many of that method’s members will also need adjustments. At least in my experience, the first Shared keyword will work as well as cutting off the Hydra’s head: it usually leads to one or more instances of the error “Error 227: Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.” The first time I tried to kill this Hydra, I had tried to rewrite a bunch of code, and ended up with a rat’s nest of Shared keywords scattered everywhere.
As the advice on this page (cryptically) recommends, try creating an instance of the class. The big fear that initially scared me off was that I’d end up either (a) unknowingly creating and destroying tons of unnecessary instances of that Class as objects, or (b) not understanding when the object I’d created fell out of scope (and would creep up on me with unpredictable garbage collection-derived errors).
What I did to alleviate this issue was to declare a “class-level” variable in the calling class of the type of the class being called, and then use that variable as the root of all subsequent uses of the called class’ methods.
This example should illustrate:
Public Class BusinessLogic ' This is the "called" class Public Sub PerformAction() Action() End Sub Private Sub Action() ... End Sub End Class Public Class UserInterface ' This is the "calling" class Imports BusinessLogic ' Doesn't help with Error 232, and may not be necessary at all Dim documentLogic As New BusinessLogic ' class-level variable Private Sub uiButton_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles uiButton.Click PerformAction() ' Causes Error 232 documentLogic.PerformAction() ' This call is OK End Sub ... End Class
Y’know, sometimes I’m just documenting this stuff for myself, since I know that in a few weeks’ time I’ll have completely forgotten the solution and the logic behind it. The rest of you happen to be benefiting from my lack of memory, and I wish I could say I was being completely selfless, but I’m getting too old to be lying to folks I never even met. 🙂
After a few weeks’ hiatus to work on some other projects (including a couple of releases of CacheMyWork — now with more filtering!), I decided to come back to the W2MWPP effort. And my overwhelming feeling right now is: thank god I was blogging as I coded! As it is, it took me probably a full hour to figure out where to focus my attention next:
There are many types of constructs in this library, many of which I’m sure will be called as part of the base Word2MediaWikiPlus() routine. However, the routine itself is only about 150 lines of code, so in itself the routine shouldn’t be too difficult to implement.
However, it’s probably a good idea to familiarize myself with some of the constructs in this library:
At this point, I’m going to implement these constructs as needed. I’d rather not add all these up front, ’cause (1) it’ll be pretty confusing for me, and (2) there’s likely some code that is no longer needed (or even has been forgotten).
Even before generating the first set of routines, I figured I should do what I could to setup the Class Library with a little forethought:
The first sixty lines or so of code in Word2MediaWikiPlus() all have to do with acquiring a handle to an active document. In the world of Document add-ins, I presume this can be very tricky, since the code itself starts from a single document’s context and has to navigate outwards from there. However, when we’re working with VSTO Application add-ins, it seems fairly easy to me to get access to an active document.
In fact, for the purposes of this tool, I’m going to assume that the user wants to convert whichever Word document is the active document. The only error condition I should need to check is that there is at least one document object open. This leads to the following code:
Dim App as Word.Application = Globals.ThisAddIn.Application Dim Doc as Word.Document = App.ActiveDocument If Doc Is Nothing 'When there's no active document open just return back to Word Exit Sub End If
The only code left in the document initialization block is this:
DocInfo.DocName = ActiveDocument.Name DocInfo.DocNameNoExt = DocInfo.DocName p = InStrRev(DocInfo.DocName, ".") If p > 0 Then DocInfo.DocNameNoExt = Left$(DocInfo.DocName, p - 1)
I was baffled by this, so a quick trip to MSDN Library cleared it up. The macro appears to be trying to parse out the document Name without the file extension. DocInfo itself is an instance of the DocInfoType Type, which is just a structure to store various properties of the document. There’s no particular reason I can see so far to use a structure for the DocName & DocNameNoExt properties, and the other properties to me don’t seem particularly related to the document itself. At this point, I’ll assume DocInfo isn’t needed. [Certainly my searches of the modWord2MediaWikiPlus.bas source only found two references to the DocInfo.DocNameNoExt property: once to assist the MW_GetImagePath() function, and once to set DocInfo.Articlename. Both should be doable without this Structure, since .NET provides a Path.GetFileNameWithoutExtension function.]
However, this leads to the MW_Initialize() function, which I would guess also should be part of the Convert class’ initialization code. I’ll check that out soon.
The last of the code in this section is the call to MW_LanguageTexts(). This is a localization macro, that will set a series of Registry values and Msg_* variables depending on a language setting retrieved from the Registry. All this can be managed quite well using Resource files, so there’s no need to mess with setting all this via code at the moment.
I’m interested in knowing whether all the Registry settings being used by this VBA project are for localization, so let’s enumerate them all…
Wow, what a…symphony of Registry settings here, only a handful of which are directly used for localization. There’s quite a number of them used for various Image manipulation operations, and others for various application settings etc. Generally, from what I can tell, there’s no reason these too can’t be stored in the project’s app.config file.
There’s the Msg_* variables as well – are all these devoted to localization as well?
Yes, that’s all of them.
The final bit of code I’ll deal with today is the “user dialog on config settings”. It appears that this calls some additional initialization code:
That’s enough for one sitting, eh?
[Note: I finally found this article in the Google search cache, so while it’s posted out of order, at least it’s finally available again.]
[This series has five previous articles: the prologue, Part 1, Part 2, Part 3 and Part 4.]
When you create an Office Add-in Project with VS2005 + VSTO, it automatically generates two Sub’s in the ThisAddIn class: ThisAddIn_Startup() and ThisAddIn_Shutdown(). Now I recall using these before for some basic background behaviour, but I have a feeling there are a few other Events in which it’s recommended to put the kind of code that’s usually used for setup, tear-down, opening persistent connections & closing them, etc.
However, the only unused Events that I can see when browsing the Method Name drop-down are New, Finalize, IsCached, StartCaching and StopCaching. Nothing really compelling so far, and none of those ring any bells. Onwards.
My plan is to work from the Word UI inwards, so that (a) if the AutoWikiBrowser folks aren’t able to grant redistribution rights for this project I’m less likely to be committed to links to their code, and (b) I’ll have more time to absorb the innards of the Word2MediaWikiPlus Macro.
Thus, I figure I’ll start with two easy tasks:
Wow, that was a little too easy. The rest of the “port” will get harder.
I already know that this code should be factored out of the Startup() method, but it’s easier to refactor than create the code and link it back. I suspected there’d be a Code Snippet for instantiating a CommandBar, so I started browsing through the Code Snippet hierarchy (Edit menu > Intellisense > Insert Snippet…) starting with Office Development > Office > Environment – Menus, Action Panes and chose “Create a Command Bar“.
It took me a while to figure out what the “CommandBarHost” replacement should be in
Dim commandBarsCollection1 As Office.CommandBars = DirectCast(commandBarHost.CommandBars, Office.CommandBars)
It kept telling me to “Replace with a reference to the Excel Application, Word Application or a Word Document.” I kept thinking I’d need to find some Object buried four levels deep in the namespaces, but it turns out all I needed was
Dim commandBarsCollection As Office.CommandBars = DirectCast(Application.CommandBars, Office.CommandBars)
[Someday I really hope I understand all this Cast nonsense – I’m sure there’s a very good reason why all this has to be done, but boy does it feel like a primitive hack on what is otherwise a pretty elegant language.]
Then the other lesson for the evening kicked in: I learned that my favoured variable naming convention – using “Word2MediaWiki++” as a common prefix wouldn’t work – Visual Studio at times tried to insert a “(” character before the plus signs, and otherwise told me that “Method arguments must be enclosed in parentheses”. Grrr, W2MWPP it is.
In trying to understand what the Set keyword was supposed to accomplish in VBA, I ran across a really strong article in MSDN that I’ve been looking for all weekend: Convert VBA Code to Visual Basic When Migrating to Visual Studio 2005 Tools for Office. Among the many, many tips on resolving specific code conversion issues is an explanation that indicates the Set keyword can simply be removed, leaving the remaining VBA intact.
A few Dim keywords here & there, a few more With… End With statements, and some string substitution to update to the name of this add-in, and we’re almost done. One last thing: what is this DoEvents method that’s called just after the toolbar is made visible? I’ve searched through all the Macro code and see it repeated dozens of times throughout, but no definition for it. Is it a built-in VBA method? Back to the “Convert VBA Code…” article it seems.
Join us again… well, you know the drill by now…
I just experienced another warning that Normal.dot was updated, and did I want to save it? That tells me that the CustomizationContext is needed. I’ve implemented a custom Word Template called “W2MWPPTemplate.dot” and the minimal code that I believe should cover this requirement. [This code was copied from a previous VSTO project of mine.]
The most interesting thing about this file’s code is that it defines a few FileOpen filters, for Access, dBASE, Text and All (*.*). I don’t know why this application would open or save from these specific file types, which leads me to wonder if this code is even in use. It’s possible it was left behind from some experiments. In any case, I’ll keep an eye out for something calling this code, but I’ll likely just use native System.IO methods once I find the caller.
If I remember correctly, the typical way to add a dialog box to a VSTO add-in is using a Windows Form. I added a Windows Form to the project and named it W2MWPP UI Config.vb. However, trying to infer what controls were on the VBA version of this form isn’t as easy as I thought it’d be from the code. However, I’ll see what I can do.
First up I added the New() function to the form’s code-behind using the drop-downs along the top of the editing panel in VS2005. In the New() method I’m adding functionality currently contained in UserForm_Initialize(). From what I’m seeing in this function, and what I’ve seen in the “old” documentation page, there are:
I’ll add the necessary controls to the Config form for (2) through (7). However, since the Word2WikiPlus VBA project doesn’t support the MS Photo Editor any longer, I’ll skip (1). As well, I’ll try using tooltips & status bar messages to provide help on what to do with each configuration control.
One of the annoyances about VSTO development in Word is that Word always seems inclined to write settings into the Normal.dot file (Word’s default template). I’ve never really looked into what these settings typically are, and I’m not sure in the case of this project either, but during the first few rounds of trying to get the CommandBar code working, I was getting asked every time whether I wanted to update the Normal.dot file (and having to create a workaround — see Part 8 for details — to avoid this).
I haven’t seen this come up in the last few days of coding, so I’m not sure if (a) I’ve gotten over whatever hurdle causes this because I’ve finally fixed a bug, or (b) I inadvertently slipped and let the Normal.dot get updated. I’ll check it out in the near future, and I’m sure I’ll have to change something because of it, but for now it’s just not worth worrying about.
I was flipping through a copy of Visual Studio Tools for Office (by the two Eric’s — Lippert and Carter) and I happened to catch the section in Chapter 7 (Working with Word Events) entitled “Visual Studio Generation of Event Handlers”. This little gem finally drilled home what the WithEvents keyword means: it allows me to select one of the controls for which I’ve tagged WithEvents in its declaration, then select any of the available Events, and VS2005 will automatically generate the Event Handler stub, complete with all those nasty parameter declarations that always give me heartburn.
So now I understand why I declared the ButtonControl, UploadControl and ConfigControl using WithEvents. Now I can easily generate the Click() Event Handlers for each of these CommandBarButtons.
The more I look at them however, the less I like the names “ButtonControl” etc. They’re not a whole lot better than “ButtonButton”, “UploadButton” and “ConfigButton” (which are obviously redundant, especially with an IDE that automatically describes each object’s type as you’re using it). Further, they really aren’t easy to find when you’re looking for one of the buttons while you’re typing code.
Somewhere along the line I picked up an elegant convention, which is to prefix UI controls with the string “ui”, as in “uiButton”, “uiUpload”, “uiConfig”. That way, any code you’re writing for UI controls will be easy to use Intellisense to narrow down to just those controls with the “ui” prefix.
I’m going to change “ButtonControl” to “uiConvert” as well. I don’t know what the name “ButtonControl” was supposed to mean (other than the most generic way of addressing this first control), and it’ll be easier to remember when the Button label and the object Name are similar.
I dropped a MessageBox.Show() call into each Event handler and Debug’d the application. What’s odd isn’t that Word 2003 complained about Macro settings (“The macro cannot be found or has been disabled because of your Macro security settings”), but that the Configure button first displayed the MessageBox, and after I clicked OK, then it complained like the others. There’s nothing different in the way I constructed the Event Handlers for these three buttons, and they all appear to be doing the same thing, but obviously there’s something happening differently with the uiConfig_Click() handler.
Once I set Breakpoints on all three of these handlers, I realized that only the uiConvert_Click() is actually executing the Sub, which is weird — this event handler is catching the Click event for the Configure button; the other two buttons are erroring before they even try to execute their code.
…Hmm, looks like I found the problem. I’d originally started out with three separate blocks of near-identical code to generate the three CommandBarButtons and all their Properties. Then, when I got the crazy idea to make my code more elegant, I collapse them into a single For Each loop. This really made the code easier to follow, but I forgot to deal with the variable used to configure each of the buttons.
Right now, the loop performs all operations in each iteration on the variable uiConvert, which is typed a CommandBarButton. What I actually need to do is have the For Each loop act on three separate variables (uiConvert, uiUpload and uiConfig) during its three iterations, so that the three buttons get configured properly.
To try to fix this, I’ve tried adding a member to the Structure of type CommandBarButton that references the CommandBarButton variable for each button. Then I use that Structure member in the For Each loop to assign all button Properties, thinking that it’d implicitly be configuring the referenced CommandBarButton variable. Unfortunately, it doesn’t seem to quite work that way.
This code fragment should explain where I’m currently at:
Structure CommandBarButtonSettings Public ButtonVariable As Office.CommandBarButton Public Tag As String Public StyleProperty As Office.MsoButtonStyle ... End Structure
Dim buttonSettings As New CommandBarButtonSettings()
Dim buttonsList As New ArrayList() buttonSettings.BeginGroupProperty = True buttonSettings.ButtonVariable = uiConvert buttonSettings.StyleProperty = Office.MsoButtonStyle.msoButtonIconAndCaption ... buttonsList.Add(buttonSettings)
For Each _buttonSettings As CommandBarButtonSettings In buttonsList 'Create the CommandBarButton if it doesn't exist If W2MWPPBar.FindControl(Tag:=_buttonSettings.Tag) Is Nothing Then (*) buttonSettings.ButtonVariable = CType(W2MWPPBar.Controls.Add(1), Office.CommandBarButton) Else ' If it already exists, do not create the CommandBarButton but just obtain a handle to it (*) buttonSettings.ButtonVariable = W2MWPPBar.FindControl(Tag:=_buttonSettings.Tag) End If Try (*) With buttonSettings.ButtonVariable .BeginGroup = _buttonSettings.BeginGroupProperty .Style = _buttonSettings.StyleProperty ... End With ... End Try Next
The problem is with the code marked with an (*), and I’m stumped on now to resolve this problem.
If you’re using VSTO SE add-in for Visual Studio, then make sure you’ve installed the VSTO SE Update that Microsoft released a couple of months ago. I didn’t know about this, and though it probably won’t affect my code, it’s never a bad thing to be sure.
OK, once more and gently (as my dad always used to say): my best theory now is that my code needs to create an object that represents the W2MWPP toolbar, and create this object whether the toolbar exists or not. Once I have that object, then I can finally get the toolbar buttons instantiated and get on with the Wiki functionality [yeah, famous last words].
I figure that the code should test whether it can find an existing instance of the toolbar. If it can’t find it, then it should create it; if it can find it, then just assign it to a variable and we’re done.
BTW, I found a great idea in VSTO for Mere Mortals (McGrath, Stubbs): rather than continuously referring to “Word2Wiki Toolbar” as a string, I could define a CONST and then reference the CONST instead. [This has the added advantage that I could change the string value very easily if “Word2Wiki Toolbar” was no longer suitable.] Why didn’t I think of this myself?
Here’s the code I’ve finally come up with to instantiate the Word2Wiki toolbar:
' Create a new CommandBar instance, if it doesn't already exist
If commandBarsCollection.FindControl(Tag:=TOOLBAR_NAME) Is Nothing Then
W2MWPPBar = commandBarsCollection.Add(TOOLBAR_NAME, Microsoft.Office.Core.MsoBarPosition.msoBarTop, False, True)
W2MWPPBar = Application.CommandBars(TOOLBAR_NAME)
Catch ex As System.ArgumentException
MessageBox.Show(TOOLBAR_NAME + "add-in's toolbar wasn't found - you won't be able to upload to the Wiki until you restart Word and/or reinstall the Add-in." + _
vbCrLf + vbCrLf + "Error: " + ex.Message, "Add-in Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Note: I’m not sure, but I suspect it’ll still thrown an exception on the first try. However, it may just “take” on the second try, so that might be good enough for now.
While researching the CommandBar methods, I found this statement: “The use of CommandBars in some Microsoft Office applications has been superseded by the new Ribbon user interface.” Oops, that’s right – some of this functionality may have to be re-written for Office 2007. I’ll try to ensure the CommandBar-specific code can be decoupled from the application functionality, so we can get maximum reuse out of these efforts.
After instantiating the Toolbar successfully, Word 2003 at shutdown will ask me twice whether I want to save changes to Normal.dot. The first prompt only allows you to overwrite Normal.dot or cancel out of the shutdown of Word.
If I hit Cancel, then the second time I try to shut down Word, it prompts me with:
“Changes have been made that affect the global template, normal.dot. Do you want to save those changes?”
This time, I can say “No” to saving the changes, which leaves Normal.dot in its original form and finally lets me close Word. The help text for this second prompt says,
“This message can appear if you made changes to items, such as macros, toolbars, or AutoText, that are stored in a global template that is attached to your document. The most commonly used global template is Normal.dot, which comes with Word.”
In the code, I’m using a well-documented sample to add the CommandBarButton to the CommandBar, and yet I’m getting the error
************** Exception Text **************
System.ArgumentException: Value does not fall within the expected range.
at Microsoft.Office.Core.CommandBarsClass.get_Item(Object Index)
at Word2MediaWiki__.ThisAddIn.ThisAddIn_Startup(Object sender, EventArgs e) in \Word2MediaWiki++\ThisAddIn.vb:line 50
at Word2MediaWiki__.ThisAddIn.FinishInitialization() in \Word2MediaWiki++\ThisAddIn.Designer.vb:line 65
Now that I know how to read this exception (see Part 7 for that whole twisty maze), I’ll spend a whole lot less time deciphering it. This time it seems clear to me that it’s a problem with allocating a handle to the toolbar. However, because I know that the toolbar is being created properly, a second glance at the offending line of code gives me the answer:
ConvertControl = CType(Application.CommandBars("W2MWPPBar").Controls.Add(1), Office.CommandBarButton)
This one is easy: I’m mistakenly calling “W2MWPPBar” rather than “Word2Wiki Toolbar”. Let’s fix that: highlight the string, right-click, choose Refactor (or Refactor!), and find…nothing. D’oh — that’s right, the VB.NET refactoring tools (even the Refactor! add-on for Visual Studio) don’t have the Rename function that I’ve gotten used to in the C# world. Guess I’m just going to have to try Edit, Find and Replace, Replace in Files instead.
With the Const now properly in place, the CommandBar and its buttons fall neatly into place. Wasn’t that easy? 😉
There’s two improvements I’ll make to the code that creates each CommandBarButton:
Here’s the current code:
For Each control As Microsoft.Office.Core.CommandBarControl In commandBarControlsCollection
If control.Tag = "W2MWPP Convert" Then
ConvertControl = control
buttonExists = True
If buttonExists = False Then
'Create a new ControlButton
ConvertControl = CType(Application.CommandBars(TOOLBAR_NAME).Controls.Add(1), Office.CommandBarButton)
And here’s my enhanced approach:
If W2MWPPBar.FindControl(Tag:="W2MWPP Convert") Is Nothing Then
ConvertControl = CType(W2MWPPBar.Controls.Add(1), Office.CommandBarButton)
ConvertControl = W2MWPPBar.FindControl(Tag:="W2MWPP Convert")
This code is creating three CommandBarButtons using the same Methods and Properties, but doing it three separate times. I know I’ve done this too, but I’d prefer to fix this. Unfortunately, there’s one slight challenge for me: there’s too many variables to pass into a Sub, and I’m not very good with multi-dimensional arrays, so I don’t know what to do with all the variable strings that need to be fed in.
However, I recall another kind of construct somewhat like a multi-dimensional array, and a little digging on the ‘net and in my books leads to Structures. Further, a nice little post to the MSDN Forums turns me on to another suitable idea: Arraylist. Combine these two, and I should be able to pass in an arraylist of structures to a InstantiateButtons() method, and I’ll be able to loop through them all in one go.
The only trick is, the articles I’m finding right now don’t seem to give me useable advice for creating a structure in VB — or perhaps it’s just that Visual Studio isn’t cooperating, because if I type “Private Structure CommandBarButtonSettings” or “Private Type CommandBarButtonSettings”, Visual Studio doesn’t seem to generate the automatic “End Structure” or “End Type” statements that appear to be necessary.
The book “The Visual Basic .NET Programming Language” (Vick) showed a very simple way to write the code for a Structure, and once I tried that VS started instructing me on what I needed to add/rearrange for this to work. One thing that hadn’t been clear is that the Structure has to appear outside of any Method, so I’ve moved it up to just after the Public Class statement.
BTW, I just stumbled across the concept of “composite formatting“, which I’m going to try to use in my MessageBox.Show() calls here. The code sample in the MSDN Forum post mentioned above, from which I borrowed, happened to use composite formatting in their Console.Writeline() call, which tipped me off to this elegant way of generating strings with dynamic content scattered throughout. I don’t know about you, but I’m a bit tired of all the ” + variable.ToString() + “ nonsense that I have to embed so often in my apps.
I’m not even sure where I read this, but it was related to some of my research into the rules that good code should follow: where an application needs to set a String variable with an empty value, we should use String.Empty instead of “”. Thus I’m making changes such as from this:
buttonSettings.DescriptionTextProperty = ""
buttonSettings.DescriptionTextProperty = String.Empty
Well I suspect you’re all just *dying* to see this app working — “wow, a toolbar with buttons that do *nothing*? What a wonder!” I’m going to mark the occasion on this day by creating a CodePlex project for this Add-In and uploading the current Source Code so everyone can have a laugh. 😉
Please have a look here, and leave any Comments, Issues or Suggestions that come to mind. Any and all such assistance is appreciated. Browse to here: http://www.codeplex.com/word2mediawikipp