简体   繁体   中英

Console Outputs not showing in VSTO Visual Studio 2019

I am doing a tutorial on how to write Add-ins for Outlook(2016). I am using Visual Studio 2019 as IDE and C# as Language. When I run the following code, Outlook opens, nothing else happens. I am just trying to figure out the code so I put some outputs inside. However, they are not working and as I got told, "Console.WriteLine" will not work in VSTO. So I use "Debug.WriteLine" in the beginning, just to make sure this method gets executed.

But it is not found in the output. Also not found when I am debugging the application.

I hope you know why this happens and can guide me in the right direction.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;
using System.Diagnostics;

namespace OutlookAddIn1
{
public partial class ThisAddIn
{
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        Debug.WriteLine("HELLO WORLD");
        // Get the Application object
        Outlook.Application application = this.Application;

        // Get the Inspector object
        Outlook.Inspectors inspectors = application.Inspectors;

        // Get the active Inspector object
        Outlook.Inspector activeInspector = application.ActiveInspector();
        if (activeInspector != null)
        {
            // Get the title of the active item when the Outlook start.
           // MessageBox.Show("Active inspector: " + activeInspector.Caption);
            Console.WriteLine("Active Inspector: " + activeInspector.Caption);
        }

        // Get the Explorer objects
        Outlook.Explorers explorers = application.Explorers;

        // Get the active Explorer object
        Outlook.Explorer activeExplorer = application.ActiveExplorer();
        if (activeExplorer != null)
        {
            // Get the title of the active folder when the Outlook start.
          //  MessageBox.Show("Active explorer: " + activeExplorer.Caption);
            Console.WriteLine("Active explorer: " + activeInspector.Caption);
        }
        Console.ReadKey();
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {

    }

    #region Von VSTO generierter Code

    /// <summary>
    /// Erforderliche Methode für die Designerunterstützung.
    /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
    /// </summary>
    private void InternalStartup()
    {
        this.Startup += new System.EventHandler(ThisAddIn_Startup);
        this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
    }

    #endregion
}
}

The Console.WriteLine method makes sense if you display a console window. If not, you may use the Debug.WriteLine method which writes information about the debug to the trace listeners in the Listeners collection. By default, the output is written to an instance of DefaultTraceListener . I'd suggest setting a breakpoint in the code to make sure it is run at all.

If you don't get any output from your add-in, 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 a 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.

We have developed a Word Add-In to allow users to edit Chemistry inside Microsoft Word. In this, we have plenty of Debug.WriteLine statements.

When running outside of the Visual Studio IDE we have to use the excellent System Internals Debug Viewer to view the debug output

When running inside the Visual Studio IDE we have to set the startup project as the VSTO being developed and the debugger to start an external program in our case MS Word as shown below在此处输入图片说明 You should obviously set this to the version of Outlook which you have installed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM