Skip to main content

3 posts tagged with "VSTO"

Visual Studio Tools for Office

View All Tags

Centralizing VSTO add in exception management with postsharp 6

· 3 min read
Mark Burton
Software Engineer & Technical Writer

After much reading around trying to find the best way to implement a global exception handler for a VSTO add-in on social msdn, stackoverflow and the Add-in Express forum I came across this solution using postsharp, this was the best solution I found but was 11 years old and relevant to postsharp 2, the library has changed a lot since then and the PostSharp.Laos namespace no longer exists as explained the postsharp support forum. ## The Solution in PostSharp 6 Happily this is still possible with PostSharp Community and is well documented in the postsharp handling exceptions documentation. First create a class which inherits from OnExceptionAspect for example VstoUnhandledExceptionAttribute. csharp using Microsoft.Extensions.Logging; using PostSharp.Aspects; using PostSharp.Serialization; namespace MarkZither.KimaiDotNet.ExcelAddin \\\{ [PSerializable] public class VstoUnhandledExceptionAttribute : OnExceptionAspect \{ public override void OnException(MethodExecutionArgs args) { ExcelAddin.Globals.ThisAddIn.Logger.LogCritical(args.Exception, "Handled by postsharp OnExceptionAspect"); args.FlowBehavior = FlowBehavior.Return; \\} } } This step will vary depending on how you are doing logging in your VSTO Add-In, I chose to use Microsoft.Extensions.Logging, while it arrived with .NET Core it is a .NETStandard 2.0 library and compatible all the way back to .NETFramework 4.6.1. csharp private void ThisAddIn_Startup(object sender, System.EventArgs e) \\{ instantiate and configure logging. Using serilog here, to log to console and a text-file. var loggerFactory = new Microsoft.Extensions.Logging.LoggerFactory(); var loggerConfig = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo.File("c:\\temp\\logs\\myapp.txt", rollingInterval: RollingInterval.Day) .CreateLogger(); loggerFactory.AddSerilog(loggerConfig); // create logger and put it to work. var logProvider = loggerFactory.CreateLogger<ThisAddIn />(); logProvider.LogDebug("debiggung"); Logger = logProvider; Configure PostSharp Logging to use Serilog LoggingServices.DefaultBackend = new MicrosoftLoggingBackend(loggerFactory); Globals.ThisAddIn.ApiUrl = Settings.Default?.ApiUrl; Globals.ThisAddIn.ApiUsername = Settings.Default?.ApiUsername; this.Application.WorkbookActivate += Application_WorkbookActivate; this.Application.WorkbookOpen += Application_WorkbookOpen; \} There is one important line in the logging setup which differs to the PostSharp Logging documentation at the time of writing, LoggingServices.DefaultBackend = new MicrosoftLoggingBackend(loggerFactory); which needs using PostSharp.Patterns.Diagnostics.Backends.Microsoft;. This is also useful for trace logging which I will explain further in the next section. Now decorate methods or entire classes with the [VstoUnhandledException] attribute and every exception will be handled by the OnException method. ## Bonus functionality in PostSharp community As I had logging working I also made use of the features of PostSharp Logging

First add a postsharp.config file, to comply with the license it is required to set the LoggingDeveloperMode to true. Be sure it has the Build Action set to Content so that it is copied to the output directory.

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
<Property Name="LoggingDeveloperMode" Value="True" />
</Project>

This still gives you 24 hours of trace logging after every publish of your VSTO Add-In and full tracing while debugging.

To control what is logged create a class called GlobalAspects, this is explained in the Adding logging to your projects section.

using PostSharp.Patterns.Diagnostics;
using PostSharp.Extensibility; [assembly: Log(AttributePriority = 1, AttributeTargetMemberAttributes = MulticastAttributes.Protected | MulticastAttributes.Internal | MulticastAttributes.Public)]
[assembly: Log(AttributePriority = 2, AttributeExclude = true, AttributeTargetMembers = "get_*")]
``` Now the logging will contain all the entry and exits from methods along with the parameters which were passed, providing valuable debug information for free for a whole day after pushing out and update to the Add-In.

KimaiDotNet based Office Add-ins

· 2 min read
Mark Burton
Software Engineer & Technical Writer

As of v1.0.0.0 released on 09032023 the VSTO is signed with an open source code signing cert from Certum this should mean the installation is automatically trusted. If you installed a version before v1.0.0.0 you must uninstall it before installing v1.0.0.0 as the change in signing cert will prevent the upgrade working. Until I get a real certificate you will need to install this self-signed certificate as a trusted root certification authority to be able to install the add-in. Download Excel Add-in ## Create an API password in Kimai

warning

WARNING!!! If you try and login with your normal password it will fail!

Create an API Password in Kimai ## Set the API credentials in the Excel Add-in Set the API credentials in the Excel Add-in ## Save the API credentials to activate the sync Set the API credentials in the Excel Add-in ## Usage of the Add-in For now the Add-in only supports reading existing timesheets and adding new ones, and editing of timesheets will need to be done in Kimai. ## Questions and suggestions The GitHub repo can be found at the KimaiDotNet on GitHub. ### Milestones Follow the Milestones. ### Issues Create any bugs or suggestions on GitHub Issues. ### Discussions Start a discussion on the GitHub Discussions.

VSTO installs over HTTPS issues

· One min read
Mark Burton
Software Engineer & Technical Writer

Test with this setup.exe. ## The Problem Setup runs sucessfully over http, but switch to https by publishing again with the https url or by using setup.exe -url="https:/myurl.comMyAppFoldersetup.exe"

URLDownloadToCacheFile failed with HRESULT '-2146697208'
Error: An error occurred trying to download 'https:/myurl.comMyAppFoldersetup.exe'.
``` ## Other Examples
https:/stackoverflow.comquestions20244507download-clickonce-fails-from-setup-exe ## Solution As explained on technet [https:/social.technet.microsoft.comForumsieen-US3d443283-c251-44b8-99ab-7ee33c928eeegroup-policy-setting-blocking-downloads-from-https-even-on-trusted-sites?forum=ieitprocurrentver](Check the GPO) the GPO was under: User Configuration\Policies\Administrative Templates\Windows Components\Internet Explorer\Internet Control Panel\Security Pages\Advanced Page\Do not save encrypted pages to disk or User Configuration\Policies\Administrative Templates\Windows Components\Internet Explorer\Internet Control Panel\Advanced Page\Do not save encrypted pages to disk ![Group Policy Management Editor](/img/GPOIE.png) By default this value is not configured
![Do Not Save Encrypted File To Disc Dialog](/img/DoNotSaveEncryptedFileToDiscDialog.png) In Internet Explorer the associated configuration option is found in Internet Options on the Advanced tab.
![Do Not Save Encrypted File To Disc Dialog](/img/IEOptions.png)