|
-
Sep 3rd, 2008, 04:59 PM
#1
[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:
Private Function getWordApp() As Word.Application
Dim wordApp As Word.Application = DirectCast(Microsoft.VisualBasic.Interaction.GetObject(, "Word.Application"), Word.Application)
If wordApp IsNot Nothing Then
Return wordApp
Else
Return Nothing
End If
End Function
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
'Type button text
Dim appWord As Word.Application = getWordApp()
If appWord Is Nothing Then Exit Sub
Dim btn As Button = DirectCast(sender, Button)
SetSymbol(appWord)
appWord.Selection.TypeText(btn.Text)
SetTimes(appWord)
'Return focus to document (not working!!)
appWord.Application.ActiveDocument.Activate()
End Sub
Private Sub SetSymbol(ByVal appWord As Word.Application)
'Changes font to Symbol
If appWord Is Nothing Then Exit Sub
appWord.Selection.Font.Name = "Symbol"
appWord.Selection.Font.Italic = True
'If CheckBox1.Checked THen appWord.Selection.TypeText(" ")
End Sub
Private Sub SetTimes(ByVal appWord As Word.Application)
'Changes font to Times
If appWord Is Nothing Then Exit Sub
appWord.Selection.Font.Name = "Times New Roman"
appWord.Selection.Font.Italic = False
If CheckBox1.Checked = True Then appWord.Selection.TypeText(" ")
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!
-
Sep 4th, 2008, 02:52 AM
#2
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..??
-
Sep 7th, 2008, 04:14 AM
#3
Re: [2008 VSTO] Giving focus back to Word document
Bump I still need this...
-
Feb 24th, 2010, 10:50 AM
#4
New Member
Re: [2008 VSTO] Giving focus back to Word document
Was there a solution ever found to this? I am having the same issue.
-
Feb 24th, 2010, 11:10 AM
#5
Re: [2008 VSTO] Giving focus back to Word document
I never found a solution, though I didn't look much further. There might be more help available this time around though, who knows
-
Feb 24th, 2010, 03:13 PM
#6
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
-
Feb 24th, 2010, 04:11 PM
#7
New Member
Re: [2008 VSTO] Giving focus back to Word document
That worked beautifully. Thank you!
-
Nov 10th, 2010, 11:13 AM
#8
New Member
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();
-
Nov 10th, 2010, 03:16 PM
#9
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
-
Mar 8th, 2012, 09:51 AM
#10
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|