Cannot change Word.Application options from C# Installer Class - c#

I am developing MS-Word Add-In using Visual Studio 2019 (with C#). I am trying to make my add-in change some Word options, like this:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
// Set numeral to "Hindi"
Application.Options.ArabicNumeral = WdArabicNumeral.wdNumeralHindi;
}
Although it works, it is not efficient because this code will be executed every time the Word application is opened. I need to execute it only once when my add-in is being installed, so I tried to create an Installer Class and do it like this:
public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);
Word.Application app_ = new Word.Application();
// Set numeral to "Hindi"
app_.Options.ArabicNumeral = WdArabicNumeral.wdNumeralHindi;
}
However, the second method doesn't change numeral to "Hindi" .. Any help ?
Note: To make sure that the Install function gets executed, I tried to divide by zero to make sure it will give runtime error.

Related

How to register a callback for an Activity Result with Xamarin Android

Background
I am developing an application that needs to browse a file manager , pick an image file and display that in an Android ImageView object. The code that starts the image file picker is this one below.
private void TextView_Click(object sender, EventArgs e)
{
//show the file manager with extensions
Intent image_intent= new Intent(Intent.ActionOpenDocument);
image_intent.AddCategory(Intent.CategoryOpenable);
//set the type for image files
image_intent.SetType("image/*");
StartActivityForResult(image_intent, 899);
}
Problem
However when I try to override the OnActivityResult() method so that I can process the image data from the resulting Intent data, Visual Studio IDE does not list the method OnActivityResult, it displays the output below.
What I tried
I tried to check if there is an existing override of this method but there was none.
I also tried to change the return type of the method to see if its been embedded in another return type but couldn't find it.
Why am I unable to override the method and how can I resolve this?
Looks like I was using the wrong access specifier to access the method, am supposed to use the keyword protected override void and not public override void. I had to check with the documentation on Microsoft Docs.

VSTO Excel template are not loaded when other EXCEL files are open

There is a problem. I have the vsto Excel template (c#). This one works perfectly, but if with it works other file excel then happens something mysterious. After exit from add-in i cant enter to it again until ill close other Excel files. I thought that it was depended on wrong way to exit from add-in, but i tryed the following ways which didn`t decide my problem:
private void ThisWorkbook_Shutdown(object sender, System.EventArgs e)
{
Application.Quit();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
I believe you have created a document-level customization. This means you will see your buttons/code running just for the one workbook.
If you really wants to have your Ribbon available just for some types of your workbooks then create a workbook template xltx. (not xlsx, xlsb or xlsm)
All workbooks created from your template will have your custom functionality. Please check this guide from Microsoft - Get started programming document-level customizations for Excel
The concept is like this.
You create a VSTO Template customization in Visual Studio
Make your Ribbon, write your code and build your project
In your Release folder you will get your Excel template, like ExcelTemplate.xltx
Distribute this file to your clients
Every time someone creates a new workbook from your template, it will have your customization but standard Excel files will not
To be honest with you I think that over years I've created just one project like this (usually I do VSTO add-ins). I don't know how you will manage updates/bug-fixes while you or your clients may already create thousands of files based on one version of your template => think in advance to have some update logic/versioning system.
I seem to remember that the one project I did, had some issue with running the code on non developer machine. I'd say I had to manually tweak registry to get it running but it may change it was really 5-6 years ago (maybe even more)
You can also consider the VSTO add-in and set the visibility (Ribbon callbacks) of your Ribbon based on some document property etc. So you will have your add-in that will load every time Excel loads but based on some internal checks it will make the Ribbon visible or hidden.
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Globals.ThisAddIn.Application.WorkbookOpen += Application_WorkbookOpen;
Globals.ThisAddIn.Application.WorkbookActivate += Application_WorkbookActivate;
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
Globals.ThisAddIn.Application.WorkbookOpen -= Application_WorkbookOpen;
Globals.ThisAddIn.Application.WorkbookActivate -= Application_WorkbookActivate;
}
private void Application_WorkbookActivate(Excel.Workbook Wb)
{
var passThisValueToRibbonCallback = IsSupportedDocument(Wb);
}
private void Application_WorkbookOpen(Excel.Workbook Wb)
{
var passThisValueToRibbonCallback = IsSupportedDocument(Wb);
}
/// <summary>
/// An example how to check if opened/activated document is 'your' document
/// </summary>
/// <param name="workbook"></param>
/// <returns></returns>
private bool IsSupportedDocument(Excel.Workbook workbook)
{
var props = workbook.CustomDocumentProperties;
try
{
var myCustomProperty = props.Item["myPropertyThatWillBeJustInMyWorkbooks"];
return true;
}
catch (Exception)
{
return false;
}
}
Please also check this Features available by Office application and project type

Executing NUnit tests through a Windows Form application with NUnit Engine

I'm trying to execute NUnit tests through a Windows Form application with NUnit Engine, but I don't understand how to set the path for the DLL where my tests are (I have already included the DLL in the references). When I click a button, I want the tests to start; however, NUnit opens and then immediately closes without doing anything. Here's what I have:
namespace ATF.GUI
{
public partial class ATF_Main : Form
{
TestPackage package;
ITestEngine engine;
public ATF_Main()
{
InitializeComponent();
}
private void ATF_Main_Load(object sender, EventArgs e)
{
string path = Assembly.GetExecutingAssembly().Location;
package = new TestPackage(path);
package.AddSetting("Working Directory", Environment.CurrentDirectory);
// Prepare the engine
engine = TestEngineActivator.CreateInstance();
}
private void btnStartTests_Click(object sender, EventArgs e)
{
using (ITestRunner runner = engine.GetRunner(package))
{
// Execute the tests
XmlNode result = runner.Run(null, TestFilter.Empty);
}
}
I never got a real answer to this but I figured it out myself.
private void ATF_Main_Load(object sender, EventArgs e)
{
// Add reference to tests DLL and load it here by name
Assembly testAssembly = Assembly.Load("Program.Tests");
package = new TestPackage(testAssembly.Location);
package.AddSetting("Working Directory", Environment.CurrentDirectory);
}
You could also add the assembly locations to a List if you have multiple test assemblies.
You are setting the test assembly to be your gui assembly. Since it has no tests, NUnit finds nothing to do. I imagine it returns an error in the result.
Somehow, your application has to be given the path of the test assembly. This can be through a command-line or a dialog of some kind. You can look at the code for nunit3-console or nunit-gui to see how it is done.
Your idea of using a referenced assembly seems a bit odd for a packaged application. Your users will need to have the source and rebuild it each time, referencing the desired test assembly. Do you really want that?
In case you do, you will need to find some way to get at that reference. Hard to do if there is nothing constant that will always be in it.

Launching a Console Applications from a MenuStrip (Visual C#)

I want to have a Windows Form Application use a menustrip with three options to launch a console application. The console application is a .exe file built in C# in Visual Studio with some basic code for as school project. The console application does not need to return any values, it only needs to run and allow the user to use it. This is what the form will look like: Menu Application
I have tried importing the System.Diagnostics.Process.Start namespace with Process.Start#("Path of file") in my menu item click event method to launch my C# console application but have not been successful. I am getting a "Win32Exception was unhandled: An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll. Additional information: The system cannot find the file specified"
Here is the code in the menu item click event:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void lesson13LabCToolStripMenuItem_Click(object sender, EventArgs e)
{
Process.Start(#"\C:\Users\Sam\Documents\Visual Studio 2015\Projects\LabMenu\LabMenu\Lesson13LabC.exe");
}
}
Any ideas on what I am doing wrong?
The error is clear The system cannot find the file specified. Check the path of file.
Also remove the starting \ in the path
Remove the backslash at the beginning of your path (before the drive letter).

ILNumerics and Visual Studio Tools for Office (VSTO)

I've been experiemnting with the community version of ILNumerics 3.2.1.0 with .Net 4.0 in Visual Studio 2010 pro on Windows 7, and going through the documentation I succesfully get a windows form project to display a chart, using the code below.
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void ilPanel1_Load(object sender, EventArgs e)
{
ILSurface mySurface = new ILSurface(ILSpecialData.sincf(100, 200));
ILPlotCube myCube = new ILPlotCube(twoDMode: false);
myCube.Add(mySurface);
ilPanel1.Scene.Add(myCube);
}
}
If I try exactly the same code but from inside a VSTO Excel 2010 application all that is displayed in the form is the designer view of the ILPanel, blue circle on white background. I don't get any error messages. Am I missing something obvious? or does anyone have a solution of how to get the chart to display in VSTO?
Update
Thanks to Philliproso for pointing out the IsDesignMode() method. As pointed out in various places, including this question, Detecting design mode from a Control's constructor , the following method is not ideal, but for me is has provided a quick fix to allow me to evaluate ILNumerics.
public static bool IsDesignMode() {
if (System.Windows.Forms.Application.ExecutablePath.IndexOf("devenv.exe", StringComparison.OrdinalIgnoreCase) > -1)
{
return true;
}
return false;
}
This is the same issue as here:
Ilnumerics Ilpanel not activating when compiled in a winform into a dll when loaded into matlab
in-a-winform-into-a-dll-when-loa
Using VSTO as host for ILNumerics lets the panels assume, it was loaded in a designer. We are currently collecting possible workarounds and solutions. One solution might be to introduce a flag in the Settings of ILNumerics:
Hosted [default: false]
Your situation would require the flag to be enabled. In hosted mode, a blacklist of common designers could be checked at runtime and compared to the current entry assembly. Any other suggestions?

Categories