I am developing a word add-in using VSTO. There are several MacroButton fields on the Word document surface involved. In order to address the fields' double-click events, I am using a VBA macro in a template (MyTestProject.dotm).
This is the structure of one of the feilds:
When field codes are hidden, it displays as TESTTAG: Test1
.
When double-clicking on the TESTTAG: Test1
, below script within the MyTestProject.dotm
excutes:
Public Sub TPS_TestTag()
Set addIn = Application.COMAddIns("MyVSTO")
Set automationObject = addIn.Object
automationObject.HandleClickEvents
End Sub
I have the following method in the AddInUtilities.cs
of my VSTO project:
public void HandleClickEvents()
{
using (MyProject.Application app = new MyProject.Application())
{
app.ExecuteMacroClickEvents();
}
}
Within the app.ExecuteMacroClickEvents()
method, I the form.ShowDialog()
method to display a modal Windows Form.
On my first double-click on the TESTTAG: Test1
, my form is appearing. Then the TESTTAG: Test1
appears selected:
If I close the form and immediately double-click on the TESTTAG: Test1
tag again, without clicking anywhere else, TPS_TestTag()
within the MyTestProject.dotm
is not executed. If I again double-click on the tag, the VBA executes and shows the dialog. If I close the dialog again and continue with the double click, dialog will not show, in next try - dialog shows.This same behavior continues.
On the other hand, when using the form.Show()
method to show the Windows Form the issue doesn't occur.
You can use the below method in the AddInUtilities.cs
in the VSTO project to replicate this issue.
public void HandleClickEvents()
{
Form objForm = new Form();
objForm.Width = 300;
objForm.Height = 300;
//objForm.Show(); /* This doesn't give the issue */
objForm.ShowDialog(); /* This gives the issue */
}
Now my question is:
I really need to use the objForm.ShowDialog()
method to show the dialog and at the same time I need to pop-up the dialog on every double-click on the TESTTAG: Test1
MacroButton field without any failures.
Update
Testing on the issue again, identified that after a double click the document is in a mode like 'not active'. When I single click on somewhere of the document, I am able to double click again. I tried below codes within AddInUtilities class and also within the TPS_TestTag()
VBA method. But non of them worked.
object word = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
wordApp = (Word.Application)word;
wordApp.ActiveDocument.Activate(); //or
wordApp.ActiveDocument.ActiveWindow.ActivePane.Selection.Select();
VB code
Public Sub TPS_TestTag()
Set addIn = Application.COMAddIns("MyVSTO")
Set automationObject = addIn.Object
automationObject.HandleClickEvents
Word.ActiveDocument.Activate
End Sub
How to solve this?
Found an answer.
Should call SetFocus
method for Active Document within AddInUtilities.cs
class right after the related code execution is finished.
public void HandleClickEvents()
{
using (MyProject.Application app = new MyProject.Application())
{
app.ExecuteMacroClickEvents();
Globals.ThisAddIn.Application.ActiveWindow.SetFocus();
}
}
or modifying the VBA code,
Public Sub TPS_TestTag()
Set addIn = Application.COMAddIns("MyVSTO")
Set automationObject = addIn.Object
automationObject.HandleClickEvents
word.Application.ActiveWindow.SetFocus
End Sub
Reference : Last comment in this forum