Results 1 to 10 of 10

Thread: [2008 VSTO] Giving focus back to Word document

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    [2008 VSTO] Giving focus back to Word document

    Hi,

    I am in the process of creating a small Add-in to Word 2007 which is basically just a list of Greek symbols I frequently have to use in a CustomTaskPane.

    I came up with the idea because every time I have to add a greek symbol I have to manually change the font, type the letter, then change it back again, which I thought could be automated pretty easily.

    The original idea of my add-in is pretty much working completely:

    I have a set of buttons, each displaying a different greek symbol. Once I click a button, I set the current Font to "Symbol" (which hosts the greek symbols). Then I use TypeText to type the button's Text property (which is the letter) to the document, and finally I change the Font back to "Times New Roman".

    In code:
    vb.net Code:
    1. Private Function getWordApp() As Word.Application
    2.         Dim wordApp As Word.Application = DirectCast(Microsoft.VisualBasic.Interaction.GetObject(, "Word.Application"), Word.Application)
    3.         If wordApp IsNot Nothing Then
    4.             Return wordApp
    5.         Else
    6.             Return Nothing
    7.         End If
    8.     End Function
    9.  
    10. Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Z.Click, Y.Click, X.Click, W.Click, V.Click, U.Click, T.Click, S.Click, R.Click, Q.Click, P.Click, O.Click, N.Click, M.Click, L.Click, K.Click, J.Click, I.Click, H.Click, G.Click, F.Click, E.Click, D.Click, C.Click, B.Click, A.Click
    11.         'Type button text
    12.         Dim appWord As Word.Application = getWordApp()
    13.         If appWord Is Nothing Then Exit Sub
    14.  
    15.         Dim btn As Button = DirectCast(sender, Button)
    16.  
    17.         SetSymbol(appWord)
    18.         appWord.Selection.TypeText(btn.Text)
    19.         SetTimes(appWord)
    20.  
    21.         'Return focus to document (not working!!)
    22.         appWord.Application.ActiveDocument.Activate()
    23.     End Sub
    24.  
    25.     Private Sub SetSymbol(ByVal appWord As Word.Application)
    26.         'Changes font to Symbol
    27.         If appWord Is Nothing Then Exit Sub
    28.  
    29.         appWord.Selection.Font.Name = "Symbol"
    30.         appWord.Selection.Font.Italic = True
    31.         'If CheckBox1.Checked THen appWord.Selection.TypeText(" ")
    32.     End Sub
    33.  
    34.     Private Sub SetTimes(ByVal appWord As Word.Application)
    35.         'Changes font to Times
    36.         If appWord Is Nothing Then Exit Sub
    37.  
    38.         appWord.Selection.Font.Name = "Times New Roman"
    39.         appWord.Selection.Font.Italic = False
    40.         If CheckBox1.Checked = True Then appWord.Selection.TypeText(" ")
    41.     End Sub

    Now, everything works just fine except for returning focus to the document so I can immediately start typing after inserting the greek symbol.

    I have tried various combinations but none are working...:
    Code:
    appWord.Application.ActiveDocument.Activate()
    appWord.Activate()
    appWord.Application.Activate()
    All just leave the focus on the button I clicked, and I have to click into the document to be able to start typing again...

    Any help would be much appreciated!

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [2008 VSTO] Giving focus back to Word document

    I have tried yet another approach:

    Code:
    appWord.ActiveDocument.Activate()
    Also does not work.

    However!

    When I debugged the code and put a breakpoint on that line, so I could see if 'appWord.ActiveDocument' actually contained something, I couldn't see anything, it wouldn't show me.
    Now the weird thing, as soon as I stepped out of the Button_Click() sub, it DID put the focus back!

    So if I run the code as normal the document does not gain focus.
    If I run step through the code slowly in debug mode, it DOES!

    What is going on..??

  3. #3

  4. #4
    New Member
    Join Date
    Feb 2010
    Posts
    4

    Re: [2008 VSTO] Giving focus back to Word document

    Was there a solution ever found to this? I am having the same issue.

  5. #5

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [2008 VSTO] Giving focus back to Word document

    try using doevents after activating the document
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7
    New Member
    Join Date
    Feb 2010
    Posts
    4

    Re: [2008 VSTO] Giving focus back to Word document

    That worked beautifully. Thank you!

  8. #8
    New Member
    Join Date
    Nov 2010
    Posts
    1

    Re: [2008 VSTO] Giving focus back to Word document

    Hello,

    I have the same problem. I'm developing in C#. The situation is that the user opens Word and then default a blank document is loaded. I added a new menu item so the user is able to open a form where he can select another Word document stored on a server.

    He picks one document and presses a button 'Open file'.

    Then I want to leave the default blank document open and create a new document where I load the selected document. The problem is that all the time when I run the code without debugging the default document is staying active instead of the new opened document.

    After the document is opened, the popup form closes.

    I use the DoEvent method, but that isn't working for me...

    This is my code:
    Code:
    //Word.Document wdActive = wdApp.ActiveDocument;
                            //wdActive.Application.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMinimize;
                            //Application.DoEvents();                        
    
                            Word.Document wdDoc = wdApp.Documents.Open2000(ref fileName, ref missing, ref strfalse, ref strfalse, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref strtrue);
                            //wdDoc.Application.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMaximize;
                            //Application.DoEvents();
                            wdDoc.Activate();
                            Application.DoEvents();
    
                            frmWait.Hide();
                            frmWait.Close();
                            this.Hide();
                            this.Close();
                            Application.DoEvents();

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [2008 VSTO] Giving focus back to Word document

    is the document actually opening?
    normally word would bring the newly opened document to the top

    i am not familiar with c#
    can you check if the opened document is referenced by the wddoc variable?

    try wdapp.documents(filename).acivate
    if that works it would appear that you wddoc does not reference the opened document
    Last edited by westconn1; Nov 10th, 2010 at 03:23 PM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  10. #10
    New Member
    Join Date
    Mar 2012
    Posts
    1

    Re: [2008 VSTO] Giving focus back to Word document

    Here is a solution in C# which is working very well. It simulates the press of "F10" and "Esc" keys.

    // Activate Ribbon = deactivate CustomTaskPane (is there other way to do that?)
    System.Windows.Forms.SendKeys.Send("{F10}");
    // Deactivate Ribbon - document get the focus
    System.Windows.Forms.SendKeys.Send("{Esc}");

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width