ActiveDocument Not Getting Set on Document Open

J

Josh Asbury

Hi all -

I am having troubles with implementing a CommandBar in Microsoft Word
utilizing VB.net. My CommandBar appears when I start Word, but if I open an
existing document, it does not appear. My best guess is that ActiveDocument
isn't getting set correctly, but I really don't know how else to implement
this. I am attaching an external template to avoid making any changes to
Normal.dot, but my code generates the CommandBar. I have included a snip of
OnStartupComplete and OnConnection below for your inspection.

I have put the some debug code into my
OnStartupComplete. When I start Word, the first MsgBox fires with "Document
1" in it. However, when I launch an existing document by double-clicking it
within Explorer, the else condition is called and the "No documents are
open" dialog fires. I'm clueless here. Is there a way that I can call
something like Documents.Add ( or something ) from within the else block and
activate the document from there?

Thanks!
Josh

Imports Office.MsoControlType
Imports Office.MsoButtonStyle
Imports Office.MsoTriState
Imports Office
Imports Word

Friend WithEvents ThisDocument As Word.Document
Friend WithEvents ThisApplication As Word.Application

Public Class Connect
Implements Extensibility.IDTExtensibility2

Dim applicationObject as Object
dim addInInstance as object

.......................


Public Sub OnStartupComplete(ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnStartupComplete

If applicationObject.Documents.Count >= 1 Then
MsgBox(applicationObject.ActiveDocument.Name)
Else
MsgBox("No documents are open")
End If

******* FAILS HERE *****
ThisDocument = applicationObject.ActiveDocument
******* FAILS HERE *****

Dim myTemplate As Template
ThisDocument.AttachedTemplate = "C:\TempWord Add-In\BCC.dot"
myTemplate = ThisDocument.AttachedTemplate

MsgBox(myTemplate.Path & applicationObject.PathSeparator _
& myTemplate.Name)

.......

Public Sub OnConnection(ByVal application As Object, ByVal connectMode
As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef custom As
System.Array) Implements Extensibility.IDTExtensibility2.OnConnection
ThisApplication = application
applicationObject = application
addInInstance = addInInst

If (connectMode <> Extensibility.ext_ConnectMode.ext_cm_Startup)
Then _
Call OnStartupComplete(custom)
End Sub
 
T

Tom Winter

I'm not sure what you are attaching your templates to every document that's
opened (it looks that way from your code; forgive me if I'm wrong). You
should consider using a global template, and make your menu changes to it.
Those menu changes will be available in all documents. (See
Application.AddIns for the object model.) And if your menu changes are
static (that is, they don't change while Word is running), you can make your
menu changes to the .DOT file at design time, then simply load the global
template using Application.AddIns.Add and then hook up to the menu items.
You won't need to worry about active documents or anything like that.

Also, a quick note on the document not being there when Word starts. When
you double-click on a document to open it, the shell will first start the
application, then (most likely using DDE if you can believe it!) tells the
application to open the document you double-clicked on. That means that when
the application first starts, there are no documents open. (I believe that
Word is setup for this (being started by the shell) to use a command line
switch (-a I believe but I could be wrong) that tells it to start with no
open documents. OK, don't know if I explained that very well, but the
behavior you are seeing is normal.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top