Thursday, April 29, 2010

Problems with HTA using VBScript and Office 2010

I am testing Office 2010 Beta - take a look at my post "VBA Problems in Outlook 2010 part 1" to see all about my network.

(editor's note: as a Microsoft Partner, my official RTM copy of Office 2010 was made available to me on May 5, 2010. I downloaded it, installed it, and can confirm that this same problem persists in the final version.)

I use Outlook Contacts to write letters using my letterhead(s). I am able to select from a list of letterheads - I may want my company letterhead, or my personal return address, or from several other choices. I highlight a contact, then click on an icon in the toolbar that is linked to a particular macro that calls an .hta file, that in turn pops up a radio button list of Word templates to use as the starting point for the letter.

This .hta in turn starts Word using the selected template, and the template gets the contact name, address, etc. from the Outlook contact.

The problem is that the following code will not run in 2010
set objNewDoc = objWord.Documents.Add(template2open,false)
unless a msgbox or popup is run just before you call objWord.Documents.Add

Sub OpenDoc()
Set objWord = CreateObject("Word.Application")
'required in 2010 to show a message box before calling the documents.add
'could use objWord.Version
if Left(objWord.Build,4) = "14.0" Then
intButton=WshShell.Popup("A letter will start using " & template2open,1)
End If
set objNewDoc = objWord.Documents.Add(template2open,false)
objWord.Visible = true
objWord.Activate
ExitHTA
End Sub


I'm not sure why this is happening, but the above code solves the problem. I found it quite by accident, because I was using a msgbox template2open statement just to make sure that the variable template2open actually had a value. It did, and when that line preceded the objWord.Documents.Add(template2open,false), then it started working. I also noticed that on at least one occassion, before I had found the problem, the old code worked, so this may be a spurious error.

I sent a frown to Microsoft concerning this problem, but I am not sure if they understand the problem, nor if they can fix it because the RTM version has apparently been released prior to my discovery of this problem.

No comments:

Post a Comment