none
How do I make the Word window active?

    Question

  • Hi all;

    We have a common operation in our AddIn where Word is active, the user clicks a button in our AddIn and we end it with:

    Range.Select();
    Document.ActiveWindow.Selection.Collapse(WdCollapseDirection.wdCollapseStart);

    And at that point Word itself is not the active window. If I click on the title bar of Word, then it is and I  have a blinking caret at the beginning of the field. But I have to make Word itself active.

    It was active before those 2 calls. It appears to be the active app in the task bar. But it's not.

    I've tried calling Activate() on the active window & pane - no luck.

    Any ideas?

    thanks - dave


    Who will win The International Collegiate Programming Championships?

    Friday, September 14, 2012 11:05 PM

All replies

  • In my experience (with all versions of Word) calling Activate() on the application object will bring Word to the front of other windows, so long as the 'active window' is not minimized.

    To check if it is minimized, and restore it, you can use the active window's WindowState property.

    Saturday, September 15, 2012 12:53 AM
  • Hi Joseph;

    No joy. I added that and it looks like it is active for a split second, then becomes inactive. It appears to be sort-of active where Windows views it as the active app. But I have to click on Word for the caret to appear. Simply clicking on the title bar is sufficient.

    But before I click the title bar and the button down in the task bar both show it as the active app. So it's active, but no caret until clicking.

    My code now is:

    Field.Select();
    Document.ActiveWindow.Selection.Collapse(WdCollapseDirection.wdCollapseStart);
    Document.Application.Activate();
    IRibbonUI.Invalidate();

    I tried with the IRibbonUI.Invalidate() commented out - still didn't work. I think the Activate() is working because I see the caret at the start of the field for that split-second. That can only occur if after the Collapse() it is active.

    ??? - thanks - dave


    Who will win The International Collegiate Programming Championships?


    • Edited by DavidThi808 Monday, September 17, 2012 5:52 PM
    Monday, September 17, 2012 5:45 PM
  • Hmm. I don't know.

    What happens if you call the Application object's ScreenRefresh method? Just a shot in the dark.

    I'm a little difficulty replicating the problem. I'm trying:

    document.Paragraphs[1].Range.Select();

    document.ActiveWindow.Selection.Collapse(Word.WdCollapseDirection.wdCollapseStart); this.ribbon.Invalidate(); // either with or without this line

    Using a COM AddIn, VSTO but an XML ribbon. The code is called from a button on the ribbon. Word 2007. In my case, no clicking the title bar or anywhere else is necessary; I immediately see the flashing cursor at the top of the document.
    Monday, September 17, 2012 7:56 PM
  • Try adding a Field and then calling Field.Select() instead of Range.Select(). I think that may be the issue.

    thanks - dave


    Who will win The International Collegiate Programming Championships?

    Monday, September 17, 2012 9:30 PM
  • After manually inserting a field:

    Word.Document document = Globals.ThisAddIn.Application.ActiveDocument;
    
    document.Fields[1].Select();
    document.ActiveWindow.Selection.Collapse(Word.WdCollapseDirection.wdCollapseStart);
    
    this.ribbon.Invalidate();

    Works fine for Word 2007. I think I heard you say in other threads you were using 2010? If that's the case, hopefully someone else can pick this up, because I don't have easy access to 2010 atm.

    Monday, September 17, 2012 10:22 PM
  • Hi DavidThi808,

    Thanks for posting in the MSDN Forum.

    Did you have solved your issue? I think JosephFox's reply is so better that mark it as answer. Please feel free to unmark it if you don't think so.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Monday, September 24, 2012 6:34 AM
  • No, I still have the problem. I'm trying to create a simple example to demonstrate.

    thanks - dave


    Who will win The International Collegiate Programming Championships?

    Monday, September 24, 2012 7:14 PM
  • Hi DavidThi808,

    Would you please share your sample on skydrive?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, September 25, 2012 6:59 AM
  • Hi all;

    This is really frustrating. I tried to create a sample to show the problem, but the sample works. What's frustrating is that the final code in both is identical. Both are in a ribbon method, both end doing the select. In our app there is no blinking caret while in the sample it is blinking.

    Word is active at the end of this. It's the active app in the taskbar. It has a single window. But the caret is not blinking. If I click on the title bar of Word - then the caret appears.

    Any ideas???

    thanks - dave


    Who will win The International Collegiate Programming Championships?

    Tuesday, October 09, 2012 9:33 PM
  • I have an example!!! It requires having a dialog form just before. Example at http://www.windward.net\temp\WordNotActive.zip

    Any idea how to handle this?

    thanks - dave


    Who will win The International Collegiate Programming Championships?

    Wednesday, October 10, 2012 3:47 PM
  • Tom - I provided an example. Can you please tell me how to make the Word document active?

    thanks - dave


    Who will win The Windward International Collegiate Programming Championships?

    Monday, October 29, 2012 9:43 PM
  • Hi David,

    As i am a C++ developer i am not familiar with much of VBA but in C++ to make the word window active we uses the word class name to find the handle of word window and than set it as a foreground process.

    Something like this

     HWND wnd = ::FindWindow(_T("Opusapp"), NULL);   
       if (wnd != NULL)SetForegroundWindow(wnd);

    here OpusApp is the word class name.

    you can do something like this in VBA.

    Hope this helps

    Guru......

    Tuesday, October 30, 2012 10:03 AM
  • Hi Guru;

    Yes, I tried that - didn't work. The issue appears to be that the window is active, but only sort-of. If I look to see what window is active programatically, it's Word. But the caret is not blinking in it.

    thanks - dave


    Who will win The Windward International Collegiate Programming Championships?

    Tuesday, October 30, 2012 1:25 PM
  • Hi David,

    Like JosephFox i am also not able to reproduce the issue.

    I just want to know are you calling any other process or application in you addin ??

    because sometimes if we call some process, the process can acquire the ForegroundProcess handle and lock it.

    If you are calling any other process or application you can lock the foreground process for word and than make a call.

    Guru.....

    Wednesday, October 31, 2012 4:49 AM
  • Hi Guru;

    Please try the example at http://www.windward.net/temp/WordNotActive.zip - it demonstrates the problem. And no, no other app involved.

    thanks - dave


    Who will win The Windward International Collegiate Programming Championships?

    Wednesday, October 31, 2012 1:22 PM
  • Sorry I'm not able to look at your example (as I doubt I'd understand it).  However, below is a VBA example that might suggest an alteration in how your add-in works.  I have speculated that your add-in has, for some reason not yet understood, moved the focus away from the insertion point in the active document and that you want to reactivate the cursor at the insertion point following the work done by your add-in application.

    Public Sub ReactivateCursorInSpecificDocument()
        '   USING VBA.
        Dim objDOC As Word.Document
        Dim objRNG As Word.Range
           
        
        '   Point to active document:
        Set objDOC = Word.Application.ActiveDocument
        
        '   Point a range object variable to the current
        '   cursor position in the active document:
        Set objRNG = objDOC.ActiveWindow.Selection.Range
        
        '   Do something here that takes focus away from
        '   current cursor position in the active document,
        '   i.e. run your existing addin code.
        
        '   Activate document and reactivate
        '   the insertion point:
        objDOC.Activate
        objRNG.Select
        
        
    Exit_ReactivateCursorInSpecificDocument:
        '   Destroy objects:
        On Error Resume Next
        Set objRNG = Nothing
        Set objDOC = Nothing
        Exit Sub
    End Sub

    Sunday, November 11, 2012 11:53 PM
  • Geoff;

    The key part of my example is it creates a Form, closes it, and then selects. That form before is what causes the problem. Your sample will work because there is no creation/closing of a dialog box.

    thanks - dave


    Who will win The Windward International Collegiate Programming Championships?

    Sunday, November 11, 2012 11:56 PM
  • Tom - I have provided an example at  http://www.windward.net\temp\WordNotActive.zip - can you please get someone to take a look and tell me how to make Word active after creating/closing a form?

    thanks - dave


    Who will win The Windward International Collegiate Programming Championships?

    Sunday, November 11, 2012 11:57 PM
  • Dave,

    I would suggest:

    1.    Before the UserForm opens, grab pointers to the Word Application, Document and Insertion Point objects.
    2.    Then hand over the opening of the UserForm to a Class module, in which its terminate event procedure activates the Word application, the relevant document, and the previously-selected Insertion Point. 

    Below is a VBA code example. 

    Geoff

    STANDARD MODULE

    Public Sub OpenUserForm()
        '   USING VBA.
        Dim objWRD As Word.Application
        Dim objDOC As Word.Document
        Dim objRNG As Word.Range
        Dim objShowUserForm  As clsShowUserForm
        '   Point to Word Application:
        Set objWRD = Word.Application
        '   Point to active document:
        Set objDOC = Word.Application.ActiveDocument
        '   Point a range object variable to the current
        '   cursor position in the active document:
        Set objRNG = objDOC.ActiveWindow.Selection.Range
        '   Instantiate class to show user form:
        Set objShowUserForm = New clsShowUserForm
        
        '   Call the ShowForm public method of the class,
        '   passing pointers to the objects.
        objShowUserForm.ShowForm objWRD, objDOC, objRNG
        
    Exit_OpenUserForm:
        '   Destroy objects:
        On Error Resume Next
        
        '   The next code line will fire the terminate event
        '   of the class, which will activate Word, the
        '   document and the insertion point:
        Set objShowUserForm = Nothing
        
        Set objRNG = Nothing
        Set objDOC = Nothing
        Set objWRD = Nothing
        Exit Sub
    End Sub

    CLASS MODULE (NAMED clsShowUserForm)

     

    Option Explicit
    Private mobjWRD As Word.Application
    Private mobjDOC As Word.Document
    Private mobjRNG As Word.Range
    Public Sub ShowForm( _
        objWRD As Word.Application, _
        objDOC As Word.Document, _
        objRNG As Word.Range)
        '   Store incoming objects:
        Set mobjWRD = objWRD
        Set mobjDOC = objDOC
        Set mobjRNG = objRNG
        
        '   The following code line will suspend code
        '   execution until the form is closed:
        UserForm1.Show
    End Sub
    Private Sub Class_Terminate()
        
        '   This terminate event will fire when
        '   the class is destroyed.
        
        '   Activate Microsoft Word:
        mobjWRD.Activate
        '   Activate document:
        mobjDOC.Activate
        
        '   Select range:
        mobjRNG.Select
        
        '   Destroy objects:
        On Error Resume Next
        Set mobjRNG = Nothing
        Set mobjDOC = Nothing
        Set mobjWRD = Nothing
    End Sub

    Monday, November 12, 2012 5:11 PM
  • Hi;

    Good idea but didn't work. I'm in C# and I assume the Closed event is the same as Terminate in VB. I did the app.Activate(), doc.Activate(), range.Select() and it's still in that weird state where the window looks like it's active, but the caret isn't there.

    ??? - thanks - dave


    Who will win The Windward International Collegiate Programming Championships?

    Friday, November 16, 2012 12:08 AM
  • Dave,

    In order for me (and others) to see what's going on, can I install and then - after I'm finished - uninstall your add-in?  If so, how do I do this? 

    I see you have setup.exe in the AddInMenuTestSetup folder. Does that install the add-in and, if so, is there an uninstall program?  (I don't want to install if I can't uninstall.)

    I've used the "Add In" group on the Developer Ribbon in Word 2010 and discovered that the dlls in the "lib" folder are not recognised as valid add-ins (because I've not run setup.exe?)

    I don't know if it'd make any difference - as you don't seem to be having a problem with activating the Microsoft Word application - but just to mention that the VBA object library has a VBA.AppActivate method (which takes the name of the application, e.g. "Microsoft Access", or what's in the titlebar of the application, e.g. "[name of document] - Microsoft Word", as a string argument to bring to the fore an-already running application).  I assume this method is a wrapper for a more challenging Windows API function, so an equivalent may be available in the C# library, although, as I say, I don't know if it'd make any difference.  (Just a thought for now.)

    Geoff.

    Sunday, November 18, 2012 4:15 PM
  • Yes, the setup program does both install and uninstall. The uninstall is through the control panel uninstall.

    When you run this you'll see that the Word window is active in every respect except for the blinking caret.

    thanks - dave


    Who will win The Windward International Collegiate Programming Championships?

    Monday, November 19, 2012 3:01 PM
  • Setup indicates it's built for a 64-bit system but that it will install on Intel. I don't think it installed on my 32-bit system. Not sure what I'd be looking for in Control Panel.
    Monday, November 19, 2012 10:42 PM
  • Control Panel has an applet to uninstall installed applications. It will be listed there.

    thanks - dave


    Who will win The Windward International Collegiate Programming Championships?

    Monday, November 19, 2012 11:40 PM
  • Asking again - have you been able to get a solution? It's been over 3 months I have been waiting. And I provided a simple example.

    thanks - dave


    Who will win The Windward International Collegiate Programming Championships?

    Friday, December 21, 2012 9:21 PM