VSTO C# in Powerpoint: WindowSelectionChange Event - c#

I am new to VSTO and C# and have a question regarding events.
I'm trying to fire an event when the user selects something within PowerPoint (e.g. a shape, slide, etc).
I found a working solution for VBA in the Office documentation here and some more info for a Word selection handler here, however I'm clueless on how and where to add it in my ThisAddIn.cs context.
I'm currently working from a fresh C# PowerPoint VSTO Addin
Any pointers into the right direction are highly welcome. Thanks!

Found this and it works:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.WindowSelectionChange += Application_WindowSelectionChange;
}
private void Application_WindowSelectionChange(PowerPoint.Selection Sel)
{
//throw new NotImplementedException();
MessageBox.Show("Hello");
}

Related

How to stop webview2 from opening new browser window rather than inside the browser

i want to know if there is anyway of stopping the webview2 component from opening a browser window
in win forms
What is happening
i looked everywhere but could not find one, i did find one though, but it used XAML/UWP
one page used xaml but the code wont work because its XAML and im using c#
To stop the link from opening in a new window, you subscribe to the CoreWebView2_NewWindowRequested as you have found out.
To do that, the easiest way is to subscribe to the CoreWebView2InitializationCompleted first.
In properties window for the WebView2 control, double click CoreWebView2InitializationCompleted - that will auto generate the eventhandler:
Now you add the CoreWebView2_NewWindowRequested eventhandler and set e.NewWindow to the current CoreWebView2.
Here's the code (assuming your WebView2 control is called webView21):
private void WebView21_CoreWebView2InitializationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e)
{
webView21.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
}
private void CoreWebView2_NewWindowRequested(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NewWindowRequestedEventArgs e)
{
e.NewWindow = webView21.CoreWebView2;
}
Now the link opens in the same window (your WebView2 control).
To complement #Poul Bak answer I was having this exact problem in VB.Net but all the answers were for C# so im going to post it in here if anyone else needs it.
First make sure to import this:
Imports Microsoft.Web.WebView2.Core
Imports Microsoft.Web.WebView2.WinForms
And then add this 2 events just replace wVBrowser with your Webview2 control name.
Private Sub wVBrowser_CoreWebView2InitializationCompleted(sender As Object, e As CoreWebView2InitializationCompletedEventArgs) Handles wVBrowser.CoreWebView2InitializationCompleted
AddHandler wVBrowser.CoreWebView2.NewWindowRequested, AddressOf CoreWebView2_NewWindowRequested
End Sub
Private Sub CoreWebView2_NewWindowRequested(ByVal sender As Object, ByVal e As Microsoft.Web.WebView2.Core.CoreWebView2NewWindowRequestedEventArgs)
e.Handled = True
End Sub
I tried like Poul Bak mentioned above it worked for me, may be your event subscription place might be wrong, try like below.
Subscribe for NewWindowRequested event after CoreWebView2 initialized.
For Ex:
public Form1()
{
InitializeComponent();
//https://learn.microsoft.com/en-us/microsoft-edge/webview2/get-started/winforms
InitializeAsync();
}
async void InitializeAsync()
{
await webView.EnsureCoreWebView2Async(null);
webView.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
}
private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
{
e.NewWindow = webView.CoreWebView2;
}
Happy Coding ... !!!!
There is a good practice to make your app deals with new tabs like real browsers

How to programatically disable Word Start Screen

I need to disable Microsoft Word start screen programatically using C# in Visual Studio. I don't want to do it manually like this .
Anyone can help ?
I found a good workaround for it. It is simply to open a new blank document on startup
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
if (Application.Documents.Count == 0)
{
Application.Documents.Add();
}
}

VSTO custom task pane

I am trying to create a custom task pane in Excel 2016.As far as I see, there is same code all site to create it.But common problem is taskpane is unvisible. The code doesn't give any error.
help please
In AddIns:
private TaskWaterMark taskPaneControl1;
private Microsoft.Office.Tools.CustomTaskPane taskPaneValue;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
taskPaneControl1 = new TaskWaterMark();
taskPaneValue = this.CustomTaskPanes.Add(taskPaneControl1, "MyCustomTaskPane");
taskPaneValue.Visible = true;
//taskPaneValue.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionFloating;
//taskPaneValue.Height = 500;
//taskPaneValue.Width = 500;
//taskPaneValue.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionRight;
//taskPaneValue.Width = 300;
this.Application.WorkbookActivate += new Excel.AppEvents_WorkbookActivateEventHandler(Application_WorkbookActivate);
taskPaneValue.VisibleChanged +=new EventHandler(taskPaneValue_VisibleChanged);
}
private void Application_WorkbookActivate(Microsoft.Office.Interop.Excel.Workbook wb)
{
}
public void taskPaneValue_VisibleChanged(object sender, System.EventArgs e)
{
Globals.Ribbons.Ribbon1.toggleButton2.Checked = taskPaneValue.Visible;
}
public Microsoft.Office.Tools.CustomTaskPane TaskPane
{
get { return taskPaneValue;}
}
In Rıbbon:
private void toggleButton2_Click(object sender, RibbonControlEventArgs e)
{
Globals.ThisAddIn.TaskPane.Visible = ((RibbonToggleButton)sender).Checked;
}
Did you try to debug the code? Do you get any exceptions in the code?
The fact is that Office may disable the add-in automatically if it fires an exception at startup. Here is what you may see in MSDN:
Microsoft Office applications can disable VSTO Add-ins that behave unexpectedly. If an application does not load your VSTO Add-in when you try to debug it, the application might have hard disabled or soft disabled your VSTO Add-in.
Hard disabling can occur when an VSTO Add-in causes the application to close unexpectedly. It might also occur on your development computer if you stop the debugger while the Startup event handler in your VSTO Add-in is executing.
Soft disabling can occur when a VSTO Add-in produces an error that does not cause the application to unexpectedly close. For example, an application might soft disable a VSTO Add-in if it throws an unhandled exception while the Startup event handler is executing.
When you re-enable a soft-disabled VSTO Add-in, the application immediately attempts to load the VSTO Add-in. If the problem that initially caused the application to soft disable the VSTO Add-in has not been fixed, the application will soft disable the VSTO Add-in again.
Read more about that in the How to: Re-enable a VSTO Add-in That Has Been Disabled article.

Outlook 2013: Visual Studio Plugin to get access to email content

I'm trying to write a plugin for Outlook 2013 that processes content of emails.
So far, I've created an Outlook Addin-project in Visual Studio. Furthermore, I created a ribbon button, with
RibbonType = Microsoft.Outlook.Mail.Read
to show the button only when an email is read.
Is it possible get access to the email in order to e.g. show the subject or the content (consisting of plain text)?
private void button1_Click(object sender, Ribbon ControlEventArgs e)
{
System.Windows.Forms.MessageBox.Show( ... );
}
Use the RibbonControlEventArgs.Control property to get to the RiibbonControl object. You can then use the IRibbonControl.Context property and cast it to the Inspector object (for inspectors) or Explorer (for the Explorer buttons). Once you have the Inspector object, use the Inspector.CurrentItem property.
Thanks Dmitry for your help!
private void button1_Click(object sender, RibbonControlEventArgs e)
{
var mailItem = ((Inspector) e.Control.Context).CurrentItem;
MessageBox.Show(mailItem.Subject);
MessageBox.Show(mailItem.Body);
}

Alternative to TextViewCreated such as (TextViewChanged)?

I am creating a small Visual Studio 2010 extension in C# which uses the IWpfTextViewCreationListener and TextViewCreated to capture when a new TextView is opened in the VS environment. The problem I am having is that this method only fires when a new window is opened via the VS Solution Explorer window, and not fired when VS already contains opened windows when started, and switching window tabs. I have tried looking for something like TextViewChanged, but could not find such method. Is there anyway to capture the new TextView when another tabbed window is selected?
Any help would be greatly appreciated.
This question has also been posted on the MSDN VS Extensibility forum:
VSX 2010 - Alternative to TextViewCreated such as (TextViewChanged)?
Thanks
John
There is no TextViewCreated, but if you register to IWpfTextView.GotAggregateFocus as it is created, you get a hook to every switch between files:
public void TextViewCreated(IWpfTextView textView)
{
textView.GotAggregateFocus += TextViewCameToFocus;
// Do stuff when a new IWpfTextView is created...
}
void TextViewCameToFocus(object sender, EventArgs e)
{
var focusedTextView = (IWpfTextView)sender;
// Do stuff when a specific IWpfTextView comes to focus...
}
You may also want to keep track of the IWpfTextView objects, if you want to be able to link between the fired events to your logic of each textview.

Categories