none
Word 2007 VSTO Addin Losing Focus RRS feed

  • Question

  • Hi there office coders,

    I have used VS2008 to make a COM addin for WORD 2007

    The Addin adds an extensible ribbon that appears without problems

    When a particular Template is chosen from the ribbon it is meant to load a specific template and that template should displays a task pane that relates helps the user contruct a specific template.

    The problem is the loaded template loses focus and the calling original word doc recieves focus.

    The problem is doubled because in  the development environment the whole process works as expected. When deployed on a SOE word has the focus issue.  I know the development environment has an Office design time adapter for word 2007 loaded in the com addins. BUT you would expect the non development (SOE) to behave as tested.

    I hope someone might be able to help

    Wednesday, September 21, 2011 5:09 AM

Answers

  • Hi Richard

    I think the problem would be this:

    Dim MSWord As New Word.ApplicationClass

    As you're running an Add-in there's absolutely no need for you to start a new instance of the Word application. Unless you can describe to me why you'd want to do this?

    It should be enough to use Documents.Add or Documents.Open (whichever is appropriate) to "load" the template into the running instance of Word.

    If you start a new instance, then your add-in is going to be running in two separate instances. The one won't be able to control what the other is doing.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Richard Mullan Thursday, September 22, 2011 6:20 AM
    Wednesday, September 21, 2011 7:42 AM
    Moderator

All replies

  • Hi Richard

    What you've said makes perfect sense to you, I'm sure :-) But there's a bit too much "short-hand" in it for me to follow exactly what your Add-in is doing, and what's happening.

    Let's start with SOE: What's an "SOE"?

    When the user clicks on a Ribbon control to choose a template, what code, exactly, is executed to "load" a template? What kind of a "template" is this? Is a Word *.dotx file? A Word *.docx file generated from a Word *.dotx file? Something else?

    What do you mean by "focus", exactly?

    Please note that I should move this discussion to the VSTO forum, as this concerns a VSTO add-in. But I want to make sure you're "with me" before I do, so that we don't lose focus :-)

     


    Cindy Meister, VSTO/Word MVP
    Wednesday, September 21, 2011 6:42 AM
    Moderator
  • Hi Cindy

    Sorry about the shorthand

    here is a bit more depth

     

    SOE = Standard Operating Environment

    template is .dotx with task pane already added

    focus is referring to the original doc and the loaded template. It is the loaded template that i want in focus (ie uppermost)

    ----

    I have mocked up a quick simplified version of the code i am using to show the basic methods

    The ribbon event is

    Imports Microsoft.Office.Tools.Ribbon

    Public Class Ribbon1

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click

            LoadDoc()

        End Sub
    End Class

     

    which calls this module.

    Imports Microsoft.Office.Interop.Word


    Module Module1

        Dim MSWord As New Word.ApplicationClass
        Dim doc As Word.Document


        Sub LoadDoc()

            MSWord = CreateObject("Word.Application")
            doc = MSWord.Documents.Add("C:\test\sample.dotx")
            MSWord.Visible = True

            'With Globals.ThisAddIn.Application
            '    .ActiveWindow.SetFocus()
            '    .Activate()
            'End With

        End Sub

     

    End Module

     

    hope that elaborates

     

    Cheers Richard

     

    I have commented one attempt at getting the focus.


    Richard
    Wednesday, September 21, 2011 7:08 AM
  • Hi Richard

    I think the problem would be this:

    Dim MSWord As New Word.ApplicationClass

    As you're running an Add-in there's absolutely no need for you to start a new instance of the Word application. Unless you can describe to me why you'd want to do this?

    It should be enough to use Documents.Add or Documents.Open (whichever is appropriate) to "load" the template into the running instance of Word.

    If you start a new instance, then your add-in is going to be running in two separate instances. The one won't be able to control what the other is doing.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Richard Mullan Thursday, September 22, 2011 6:20 AM
    Wednesday, September 21, 2011 7:42 AM
    Moderator
  • Thanks for the reply Cindy

    BUT

    Because this is a COM addin the 'Documents.add' or ' .open' dosent work

    -unless you can suggest the code to use.

    It requires an object according to Visual Studio

    This would work in a Template or Document type project

    I hope you can still help

    Thanks

     

    Richard

     


    Richard
    Thursday, September 22, 2011 12:30 AM
  • Hi Again Cindy

     

    I am using

    Globals.ThisAddIn.Application.Documents.Add("C:\test\sample.dotx")

    which works in development environment.

    I hope it will behave in the deployed environment (without visual studio)

    thanks

     

    Richard Mullan

    Dept of Transport Energy and Infrastructure


    Richard
    Thursday, September 22, 2011 1:11 AM
  • Hi Again Again Cindy

     

    That works on a test deployment computer (via SCCM)

    Thanks for your input.

     

    Richard Mullan

    Dept of Transport Energy and Infrastructure


    Richard
    Thursday, September 22, 2011 4:14 AM
  • Hi Richard

    You're welcome :-) Glad you were able to solve it during my night.

    If you have a moment, we'd appreciate it if you'd drop by the forum and mark the "Answer" to your question.


    Cindy Meister, VSTO/Word MVP
    Thursday, September 22, 2011 5:56 AM
    Moderator