I am using Visual Studio 2022 and I have created a VSTO Word Add-In project.
I want to open a new form with a button inside my ribbon. However, my form always starts in the top left corner of my primary screen, which is problematic if the Word document is on a different screen.
In my case, my Word document usually opens on my second screen, so I need the form to dynamically start on the same screen as the Word document.
So far, I have been able to change the starting position of my form using the myForm.Location
and myForm.StartPosition
properties, as shown below. But how do I get the screen which my Word application is using?
using Word = Microsoft.Office.Interop.Word;
namespace WordTabellenAddIn
{
public partial class RibbonWordAddIn
{
private void TabellenButton_Click(object sender, RibbonControlEventArgs e)
{
Word.Document ThisDocument = Globals.ThisAddIn.Application.ActiveDocument;
//Screen screen = Screen.FromControl(ThisDocument); <-- Expects a "System.Windows.Forms.Control" Which the Object simply isn't
var myForm = new FormFenster();
myForm.Location = Screen.AllScreens[1].WorkingArea.Location;
myForm.StartPosition = FormStartPosition.CenterScreen;
myForm.ShowDialog();
}
}
}
So far I looked into the official Word interface documentation for the Word.Document class
MS Documentation but couldn’t find a clue.