With Visual Studio 2013 I target the .NET Framework 4, and created a single add-in that targets both Office 2007 and Office 2010. I choose a 2010 Addin but it should work in 2007 accordind to this link:
https://blogs.msdn.microsoft.com/vsto/2010/06/04/creating-an-add-in-for-office-2007-and-office-2010-that-lights-up-on-office-2010-mclean-schofield/
I used the ribbon designer (not the ribbon xml) In my AddInin the startup method I have the following:
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
((Word.ApplicationEvents4_Event)this.Application).NewDocument += new Microsoft.Office.Interop.Word.ApplicationEvents4_NewDocumentEventHandler(Application_NewDocument);
this.Application.DocumentBeforeClose += new Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(Application_DocumentBeforeClose);
//THIS LINE FAILS IN Word 2007 but not in Word 2010
this.Application.ActiveDocument.Saved = false;
}
Basically I'm capturing the close event and I do some custom code of my own in that event. This works perfectly in 2010. In 2007 the ribbon installs and some functionality works however when I close the document my 'Application_DocumentBeforeClose' close event dosnt get called in Word 2007. Any suggestions?
UPDATE: I changed my code as suggested however the line below fails in Word 2007 when I open a document with error - 'This command is not available because no document is open.'
this.Application.ActiveDocument.Saved = false;
Take a look at the similar forum thread - Word DocumentBeforeClose not firing. Here is what it states:
If we open a document, Word will detect whether the new document is modifed. If it is the blank document from scratch, Word uses it to host the target document directly. So in this case, it is not considered a Close action. Hence, the DocumentBeforeClose does not fire.
Related
Long time ago I made small AddIn for Excel to upload small trunks off data to SQL server. Quite simple form is called from context menu on right btn mouse click. It worked well on Win7 + Excel 2010 (don't know if OS is important). Recently I moved to Win10 + Excel 2013. And the context menu won't show up anymore... Well actually I can make it show but only once. My ThisAddIn_Startup
private void ThisAddIn_Startup(object sender, EventArgs e)
{
logger.Info("Started");
CreateContextMenu();
Application.SheetBeforeRightClick += Application_SheetBeforeRightClick;
}
And here's CreateContextMenu
private void CreateContextMenu()
{
try
{
Office.MsoControlType menuItem = Office.MsoControlType.msoControlButton;
uploadToDatabase = (Office.CommandBarButton)Application.CommandBars["Cell"].
Controls.Add(menuItem, missing, missing, 1, true);
uploadToDatabase.Style = Office.MsoButtonStyle.msoButtonCaption;
uploadToDatabase.Caption = "Upload to database";
uploadToDatabase.Tag = "0";
//MessageBox.Show("MF");
logger.Info("Added context menu");
}
catch (Exception ex)
{
logger.Error(ex);
}
}
In log file I see both records - "Started" and "Added context menu", but context menu is still not customized.
But if I uncomment the line MessageBox.Show("MF"); then it will show up for the first start of excel and then again dissappear if I close a workbook and open any other.
Does anyone have an idea why?
Appreciate for any hints.
The fact is that Command bars are not used any longer (were deprecated). The only possible way to customize the context menus in Office 2013 and later versions is to use the Fluent UI (aka Ribbon UI). You can read more about that in the following articles in MSDN:
Customizing Context Menus in Office 2010
Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)
I am tring to call with =CALLTHIS("pp",) and with =CALLTHIS("ThisDocument.pp",) from shape event dblClick. I´m using C# Visio 2010 Add-in in Visual Studio 2015 without success.
The method is:
public static void pp(Visio.Shape shpObj) {
MessageBox.Show("My id is: " + shpObj.ID);
}
To call a managed C# (VSTO/COM) add-in from shape sheet formula cell, such as "dblClick" event, use QUEUEMARKEREVENT function. CALLTHIS only works for VBA functions. RUNADDON/RUNADDONWARGS only works for unmanaged (VSL) addons.
Here you can find a step-by-step guide how to make it happen:
https://blogs.msdn.microsoft.com/chcast/2004/11/03/calling-com-add-ins-from-the-shapesheet/
I have a word document, downloaded from the internet, that I would like to perform some operations on in an Application-level Add-In. These operations (document search, unprotecting the document, etc) require that the document be in edit mode when it is opened. Here is some sample code that illustrates my needs:
private void ThisAddIn_Startup(object sender, EventArgs e)
{
Application.DocumentOpen += application_DocumentOpen;
}
private void application_DocumentOpen(Document doc)
{
if (doc.ProtectionType != WdProtectionType.wdNoProtection)
{
// this throws a COMException if the document is opened in read-only mode
doc.Unprotect("password");
}
// ...
}
Since this add-in will be distributed to multiple users, I can't assume that the user will set any application properties, such as opening downloaded documents in edit mode by default, so doing it in code would be ideal. Is there some means with VSTO or the interop library to accomplish this, given my constraints? Thanks for any help.
EDIT: My application-level add-in was tested as installed on Word 2013, and was created with VS 2013, VSTO 4.0.
A sample document that displays the requisite characteristics can be found here. The document is protected with WdProtectionType.wdAllowOnlyFormFields, and the password is "password".
What version of Word/VSTO are you using.
I tried this using Word 2013 ( 64-bit ) and VSTO 4.0/Visual Studio 2013 and do not get any Exception in the even handler function "application_DocumentOpen". For both read-only and protected docs.
EDIT:
Try to change the view to Print View before unprotecting the document.
if (doc.ProtectionType != Word.WdProtectionType.wdNoProtection)
{
doc.ActiveWindow.View.Type = Word.WdViewType.wdPrintView;
doc.Unprotect("password");
}
Reference : http://answers.microsoft.com/en-us/office/forum/office_2013_release-word/not-available-for-reading-error-on-unprotecting-a/a888701b-d70a-4dbc-a1ec-68b8bad80848
VSTO
VS2008 SP1
.NET 3.5
Excel 2007
I am a .net noob. I am trying to load an automation addin that is an excel application/automation addin (it is a dll not xla or xll) from within a vsto addin in the ThisAddIn_Startup() method of the vsto addin. From google I got the below solution which is not working.
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Application excel = Globals.ThisAddIn.Application;
//Also tried without display alerts being set to false
excel.DisplayAlerts = false;
foreach (AddIn addin in excel.AddIns)
{
if (addin.progID.Equals("MY_ADDIN_PROG_ID"))
{
Debug.WriteLine("Addin installed is " + addin.Installed);
addin.Installed = false;
Debug.WriteLine("Addin is: " + addin.FullName + ", " + addin.progID);
Debug.WriteLine("Addin installed is " + addin.Installed);
}
}
AddIn addIn = excel.AddIns.Add("MY_ADDIN_PROG_ID", false);
addIn.Installed = true;
excel.DisplayAlerts = true;
Debug.WriteLine("Addin is: " + addIn.FullName + ", " + addIn.progID);
Debug.WriteLine("Addin installed is " + addIn.Installed);
excel.DisplayAlerts = false;
//OTHER STARTUP CODE
Debug.WriteLine("Starting up addin!");
}
Note, I can see the addin.installed is being set to false and back to true on startup but when I try to populate worksheet with udfs from the addin I tried to load in a later button_click method, I get #NAME? error. I am at my wits end. Any help will be greatly appreciated.
If I first try to call the udf in excel by typing it in a cell by hand before I call my button click method, the worksheet population works and the udfs get evaluted as expected but this is not ideal.
Also setting installed property to true does not seem to be doing anything as i can still see the udf addin as inactive in excel, it is only if I type it into a cell that it gets activated. Is there anything else I need to do to activate the automation addin in my vsto startup?
Thanks!
I'm not sure you want to do this in the startup event. I have done something similar but not quite the same before which may be applicable. I exposed some COM visible functions to VBA in a different event handler:
protected override object RequestComAddInAutomationService()
{
// return something com-visible
}
So maybe you can try to load your automation dll this way? This happens before the startup event fires... Excel might be doing something like locking its list of addins while a startup event is being handled - who knows? If it were possible to know Excel programming would be less tedious.
It is harder than it seems to combine VSTO and Automation in Excel. You may find my blog post helpful:
Communicating Between VSTO and UDF's in Excel
Just need to add String Value to the following registry key and you are good.
For Office 2007
Find regkey, HKEY_CURRENT_USER\SOftware\Microsoft\Office\12.0\Excel\Options, then create string value, where name = OPEN, value = /A "YOUR ADDIN NAME HERE" (quotes need to be included as well.)
Note that for the first addin, value name should be called OPEN, for the second one and onwards, use OPEN1, OPEN2, ... etc.
For Office 2010
Just replace 12.0 with 14.0 in the above regkey path, the rest are all the same.
Check out below article on MSDN, which will also help you a lot.
http://support.microsoft.com/kb/291392
Looks like this is a bug specific to VSTO. I converted my addin to a COM addin and was able to use the automation addin from code after that. My team has sent the issue to microsoft so we'll see what they say.
I had my first outlook add-in developed,
I can see that debugging the add-in opens the outlook automatically, the issue i noticed that outlook takes about 20 sec to open when my add-in attached (as new menu with one button).
I thought it might be caused by the fact the im debugging my project!,
I published my add-in to my localhost, and then installed it using the click once thing, but still hangs on load
the outlookAddIn2.vsto file is used by outlook as my custom add-in, but when i saw the other add-ins all of them was dlls not vsto plus they dont hang up the outlook on start
What should I do to deploy my project as dll and yet not to freeze my outlook on startup?
Thank you in advance.
p.s.: eventually the add-in will be implemented in our intranet employees outlook accounts
EDIT:
namespace OutlookAddIn2
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
MyToolBar();
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
Office.CommandBar mainMenuBar;
Office.CommandBarPopup oldMenuBar;
Office.CommandBarPopup myMenuBar;
Office.CommandBarButton myButton;
private void MyToolBar()
{
try
{
mainMenuBar = this.Application.ActiveExplorer().CommandBars.ActiveMenuBar;
oldMenuBar = (Office.CommandBarPopup)this.Application.ActiveExplorer().CommandBars.ActiveMenuBar.FindControl
(
Office.MsoControlType.msoControlPopup, missing, "Katakit", true,true
);
if (oldMenuBar != null)
oldMenuBar.Delete(true);
myMenuBar = (Office.CommandBarPopup)mainMenuBar.Controls.Add(
Office.MsoControlType.msoControlPopup,
missing, missing, missing, false);
if (myMenuBar != null)
{
// Add a button to the new toolbar.
myMenuBar.Caption = "Katakit";
myMenuBar.Visible = true;
myMenuBar.Tag = "Katakit";
myButton = (Office.CommandBarButton)myMenuBar.Controls.Add
(Office.MsoControlType.msoControlButton, missing, missing, missing, true);
myButton.Caption = "Pending Summary 2";
myButton.FaceId = 500;
myButton.Tag = "btnPendingSummary";
myButton.Visible = true;
}
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show("Error: " + ex.Message.ToString()
, "Error Message");
}
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}
Probably you run into the "Check for publishers certificate revocation" bottleneck. It has nothing to do with Outlook, but with .net-assemblies running in an environment without proper internet access. See this entry in the Add-in Express forum, with a reference to this discussion. Either you can disable an IE setting, or try to verify the Internet access.
I are always running myself into this problem when my VMWare development machine thinks it has network access, but the host's network is switched off, e.g. the VM is bridged to the host, but the network cable of the host is not plugged in, or if the VMWare guest is part of a domain with a domain controller running (=> network available), but this network has no Internet access and no proper Certificate Authority. In this case, slow startup time. If the host has Internet access, no startup delay.