I already have a C#-based Outlook Addin application which may or may not be installed on my clients versions of Outlook. Is it possible to determin whether the addin is installed and enabled from an external C# application, running on the same client's machine? And if so, how?
Many thanks in advance!
John
If you are installing via MSI, you can check if it has been installed with the Windows Installer API (see MSDN for more, P/Invoke.net has a C# example).
In the end, the following code solved my problem:
using System.Reflection;
using System.Runtime.InteropServices;
using Outlook = Microsoft.Office.Interop.Outlook;
using Microsoft.Office.Core;
...
public static bool IsOutlookAddinEnabled(string addinName)
{
bool isEnabled = false;
Outlook.Application outlookApp = null;
if (System.Diagnostics.Process.GetProcessesByName("OUTLOOK").Length > 0)
{
outlookApp = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
}
else
{
outlookApp = new Outlook.Application();
Outlook.NameSpace nameSpace = outlookApp.GetNamespace("MAPI");
nameSpace.Logon("", "", Missing.Value, Missing.Value);
nameSpace = null;
}
try
{
COMAddIn addin = outlookApp.COMAddIns.Item(addinName);
isEnabled = addin.Connect;
}
catch { }
return isEnabled;
}
Many thanks to Mitch for his quick response.
Related
Using: Visual Studio 2017 (Language: C#)
I have a similar function written below in PowerShell script, but I need the C# version of it to execute on the click of a button within Visual Studio:
Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
dir “$env:userprofile\Documents\Outlook Files\*.pst” | % { $namespace.AddStore($_.FullName) }
Any insight or examples of code would be much appreciated.
You can do that the following way:
In your project, right click on "References" and add a reference to the assembly "Microsoft.Office.Interop.Outlook".
Then you can use the following code:
/// <summary>
/// Get a reference to an already running or a newly started Outlook instance
/// </summary>
Microsoft.Office.Interop.Outlook.Application GetOutlookApp()
{
Microsoft.Office.Interop.Outlook.Application app = null;
// Try to get running instance
try
{
app = Marshal.GetActiveObject("Outlook.Application") as Microsoft.Office.Interop.Outlook.Application;
}
catch(Exception)
{
// Ignore exception when Outlook is not running
}
// When outlook was not running, try to start it
if(app == null)
{
app = new Microsoft.Office.Interop.Outlook.Application();
}
return app;
}
private void button1_Click(object sender, EventArgs e)
{
const string fileName = #"D:\MyDings.pst";
var app = GetOutlookApp();
var nameSpace = app.GetNamespace("MAPI");
nameSpace.AddStore(fileName);
MessageBox.Show("Done");
}
I'm not 100% sure if it is possible without extra packages. So I would just do a shell command executing the powershell script, since you already have that. Bit of a hack but seems the easiest option.
using System.Diagnostics;
Process.Start("powershell.exe " + scriptLocation);
I am trying to automate Outlook using COM, and we are having terrible problems with Outlook crashing at arbitrary points when trying to use MAPI methods (eg Outlook.Recipient r = MAPI.CreateRecipient("me#there.com")).
One solution that has been suggested is to use Marshal.GetActiveObject() instead of new Outlook.Application().
This appears to work fine, however I have run into a very strange issue - the code fires up a copy of outlook and tries to get the application object, but calls to Marshal.GetActiveObject throw a System.Runtime.InteropServices.COMException exception while Outlook is the active application (Has the focus).
If I run the below code, the try keeps failing over and over.
However, if I hit ALT-TAB or click anywhere such that Outlook is no longer the active application, the code INSTANTLY succeeds.
Any ideas why? Failing that, does anyone have code that will de-focus outlook, so the code succeeds?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Threading;
using System.Reflection;
using System.Runtime.InteropServices;
namespace MAPI_Repro
{
class FakeTest
{
public Outlook.Application oApp;
public Outlook.NameSpace MAPI;
public void DoTest(){
if (Process.GetProcessesByName("OUTLOOK").Count() == 0)
{
var process = Process.Start(new ProcessStartInfo("outlook.exe"));
}
while (Process.GetProcessesByName("OUTLOOK").Count() == 0)
{
Thread.Sleep(100);
}
bool success = false;
while (!success)
{
Debug.WriteLine("Waiting for Marshal.GetActiveObject");
try
{
// This FAILS while Outlook is the active application.
// As soon as you hit ALT-TAB or click another app, this succeeds.
oApp = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
success = true;
Debug.WriteLine("SUCCESS");
}
catch (System.Runtime.InteropServices.COMException exp)
{
Debug.WriteLine("FAILED " + exp);
}
Thread.Sleep(100);
}
MAPI = oApp.GetNamespace("MAPI");
MAPI.Logon("", "", Missing.Value, Missing.Value);
}
}
}
I have tried to read Microsoft Outlook contact using Microsoft Outlook 15.0 object library DLL, it works locally; when it comes to client, we do not know what version of Outlook is the client using. How to read if each client having different versions of Outlook?
I want to read the contact with any version of Microsoft Outlook Version using C#.
If you have any open source code, it helps a lot.
Please look at my code and help me where am doing wrong.
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Runtime.InteropServices;
using MsOutlook = Microsoft.Office.Interop.Outlook;
namespace Test
{
public class OutlookMailManager : IDisposable
{
public OutlookMailManager() { }
/// <summary>
/// Get MailContacts From Google (Gmail) using the provided username and password.
/// </summary>
/// <param name="maxEnries">Total number of entries to return</param>
/// <returns>The addressbook entries</returns>
public string GetOutlookMailContacts(int maxEnries)
{
MsOutlook.ApplicationClass OutlookApplication = new MsOutlook.ApplicationClass();
MsOutlook.NameSpace outlookNameSpace = OutlookApplication.GetNamespace("MAPI");
MsOutlook.MAPIFolder contactsCollection = outlookNameSpace.GetDefaultFolder(MsOutlook.OlDefaultFolders.olFolderContacts);
Microsoft.Office.Interop.Outlook.Items folderItems = contactsCollection.Items;
string rtnStr = "";
if (folderItems.Count > 0)
{
for (int i = 1; folderItems.Count >= i; i++)
{
object contactObj = folderItems[i];
if (contactObj is MsOutlook.ContactItem)
{
MsOutlook.ContactItem contact = (MsOutlook.ContactItem)contactObj;
rtnStr += contact.FullName + " (" + contact.BusinessTelephoneNumber + ")\n";
}
Marshal.ReleaseComObject(contactObj);
if (i == maxEnries) break;
}
}
Marshal.ReleaseComObject(folderItems);
Marshal.ReleaseComObject(contactsCollection);
Marshal.ReleaseComObject(outlookNameSpace);
return rtnStr;
}
}
}
You just need to use the PIAs corresponding the lowest Outlook version you need to support. Thus, you will be sure that only properties and methods existing in all Outlook versions are used. See C# app automates Outlook (CSAutomateOutlook) for the sample project.
Currently its working fine with my Outlook 2003 Version
CheckOut this code but i havent tested with different outlook version.but
Add Microsoft.Office.Interop.Outlook dll in reference
Microsoft.Office.Interop.Outlook.Items OutlookItems;
Microsoft.Office.Interop.Outlook.Application outlookObj;
MAPIFolder Folder_Contacts;
private void Form1_Load(object sender, EventArgs e)
{
outlookObj = new Microsoft.Office.Interop.Outlook.Application();
Folder_Contacts = (MAPIFolder)outlookObj.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
OutlookItems = Folder_Contacts.Items;
for (int i = 0; i < OutlookItems.Count; i++)
{
Microsoft.Office.Interop.Outlook.ContactItem contact = (Microsoft.Office.Interop.Outlook.ContactItem)OutlookItems[i + 1];
MessageBox.Show("FirstName:"contact.FirstName +" "+"LastName:"+contact.LastName +" "+"Emailid:"+contact.Email1Address);
}
}
I'm trying to connect to a Public Folder in Outlook 2010 with C# (Visual Studio 2010).
I copied following code from a Microsoft Website:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// TODO: Add code here to start the application.
Outlook._Application olApp = new Outlook.ApplicationClass();
Outlook._NameSpace olNS = olApp.GetNamespace("MAPI"); Outlook._Folders oFolders;
oFolders = olNS.Folders;
Outlook.MAPIFolder oPublicFolder = oFolders["Public Folders"];
oFolders = oPublicFolder.Folders;
Outlook.MAPIFolder oAllPFolder = oFolders["All Public Folders"];
oFolders = oAllPFolder.Folders;
Outlook.MAPIFolder oMyFolder = oFolders["My Public Folder"];
Console.Write(oMyFolder.Name);
}
}
}
My problem is that "ApplicationClass" is redlined and I don't know what I've forgotten or done wrong.
Here's a screenshot with the error message.
You need to use interface
Microsoft.Office.Interop.Outlook.Application outlook = new Microsoft.Office.Interop.Outlook.Application()
or disable embedding of Interop types for this assembly (References -> Microsoft.Office.Interop.Outlook (right click) -> Properties -> Set 'Embed Interop Types' to False)
Change the line
Outlook._Application olApp = new Outlook.ApplicationClass();
to
Outlook._Application olApp = new Outlook._Application();
I've been browsing for a good hour and have yet to find something that would help with this. I'm working on opening AutoCAD from the .NET API in VS2013 using C#, but for some reason, I can never get AutoCAD to actually launch. I'm using the following code:
using System;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
namespace IOAutoCADHandler
{
public static class ACADDocumentManagement
{
[CommandMethod("ConnectToAcad")]
public static void ConnectToAcad()
{
AcadApplication acAppComObj = null;
// no version number so it will run with any version
const string strProgId = "AutoCAD.Application";
// Get a running instance of AutoCAD
try
{
acAppComObj = (AcadApplication)Marshal.GetActiveObject(strProgId);
}
catch // An error occurs if no instance is running
{
try
{
// Create a new instance of AutoCAD
acAppComObj = (AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID(strProgId), true);
}
catch //// STOPS HERE
{
// If an instance of AutoCAD is not created then message and exit
// NOTE: always shows this box and never opens AutoCAD
System.Windows.Forms.MessageBox.Show("Instance of 'AutoCAD.Application'" +
" could not be created.");
return;
}
}
// Display the application and return the name and version
acAppComObj.Visible = true;
System.Windows.Forms.MessageBox.Show("Now running " + acAppComObj.Name +
" version " + acAppComObj.Version);
// Get the active document
AcadDocument acDocComObj;
acDocComObj = acAppComObj.ActiveDocument;
// Optionally, load your assembly and start your command or if your assembly
// is demandloaded, simply start the command of your in-process assembly.
acDocComObj.SendCommand("(command " + (char)34 + "NETLOAD" + (char)34 + " " +
(char)34 + #"C:\Users\Administrator\Documents\All Code\main-libraries\IOAutoCADHandler\bin\Debug\IOAutoCADHandler.dll" + (char)34 + ") ");
acDocComObj.SendCommand("DRAWCOMPONENT");
}
}
Unfortunately, it always stops at the nested catch statement and always displays the popup box without opening AutoCAD. Any suggestions on how to at least make AutoCAD open for me?
EDIT: Error message
The issue is you're coding (correctly) to the AutoCAD interop interface. I recommend against that (due to potential version changes).
The other issue is that the documentation for AutoCAD plugins using the newer .net api is for plugins when AutoCAD is already running.
Final issue could be that the program Id of AutCAD is a mystery. I have resorted to making that a configurable setting, but default to "AutoCAD.Application", which will take the currently registered AutoCAD.Application on the production machine. If there are multiple versions installed on the machine and you want to be specific, then you could append the version number (which you'll need to research) to the ProgID like: "AutoCAD.Application.19", or "AutoCAD.Application.20" for 2015.
For the first issue, one technique is to use dynamics for the autoCad objects, particularly for creating instances. I have used the ObjectARX api for creating my application in a dummy project, and then switching to dynamics when I'm happy with the properties and method names.
In a standalone .Net application that starts AutoCAD you could use something like:
// I comment these out in production
//using Autodesk.AutoCAD.Interop;
//using Autodesk.AutoCAD.Interop.Common;
//...
//private static AcadApplication _application;
private static dynamic _application;
static string _autocadClassId = "AutoCAD.Application";
private static void GetAutoCAD()
{
_application = Marshal.GetActiveObject(_autocadClassId);
}
private static void StartAutoCad()
{
var t = Type.GetTypeFromProgID(_autocadClassId, true);
// Create a new instance Autocad.
var obj = Activator.CreateInstance(t, true);
// No need for casting with dynamics
_application = obj;
}
public static void EnsureAutoCadIsRunning(string classId)
{
if (!string.IsNullOrEmpty(classId) && classId != _autocadClassId)
_autocadClassId = classId;
Log.Activity("Loading Autocad: {0}", _autocadClassId);
if (_application == null)
{
try
{
GetAutoCAD();
}
catch (COMException ex)
{
try
{
StartAutoCad();
}
catch (Exception e2x)
{
Log.Error(e2x);
ThrowComException(ex);
}
}
catch (Exception ex)
{
ThrowComException(ex);
}
}
}
When there are several versions of AutoCAD installed on a computer, creating an instance with the ProgID "AutoCAD.Application" will run the latest version started on this computer by the current user. If the version of the Interop assemblies used does not match the version that is starting, you'll get a System.InvalidCastException with an HRESULT 0x80004002 (E_NOINTERFACE).
In your specific case, the {070AA05D-DFC1-4E64-8379-432269B48B07} IID in your error message is the GUID for the AcadApplicationinterface in R19 64-bit (AutoCAD 2013 & 2014). So there is an AutoCAD 2013 or 2014 that is starting, and you cannot cast this COM object to a 2015 type because 2015 is R20 (not binary compatible).
To avoid that, you can add a specific version to your ProgID (like "AutoCAD.Application.20" for AutoCAD 2015 (R20.0) to 2016 (R20.1)) to start the version matching your Interop assemblies or you can use late binding (eg. remove your references to Autodesk.AutoCAD.Interop* and use the dynamic keyword instead of the AutoCAD types).
In the last case, you will lost autocompletion, but your program will work with all the versions of AutoCAD.
Check also 32-bit vs 64-bit because TypeLib/Interop assemblies are not the same.
I open the application in a much straight-forward way. First, be sure to reference the correct type library. The one I am using is AutoCAD 2014 Type Library, located at:
c:\program files\common files\autodesk shared\acax19enu.tlb
To initialize the application:
using AutoCAD;
namespace test
{
class Program
{
static void Main(string[] args)
{
AutoCAD.AcadApplication app;
app = new AcadApplication();
app.Visible = true;
Console.Read();
}
}
}
Try this:
"sourcefile" is the original file
"newfile" is the new file
[CommandMethod("ModifyAndSaveas", CommandFlags.Redraw | CommandFlags.Session)]
public void ModifyAndSaveAs()
{
Document acDoc = Application.DocumentManager.Open(sourcefile);
Database acDB = acDoc.Database;
Transaction AcTran = acDoc.Database.TransactionManager.StartTransaction();
using (DocumentLock acLckDoc = acDoc.LockDocument())
{
using (AcTran)
{
BlockTable acBLT = (BlockTable)AcTran.GetObject(acDB.BlockTableId, OpenMode.ForRead);
BlockTableRecord acBLTR = (BlockTableRecord)AcTran.GetObject(acBLT[BlockTableRecord.ModelSpace], OpenMode.ForRead);
var editor = acDoc.Editor;
var SelectionSet = editor.SelectAll().Value;
foreach (ObjectId id in SelectionSet.GetObjectIds())
{
Entity ent = AcTran.GetObject(id, OpenMode.ForRead) as Entity;
//modify entities
}
AcTran.Commit();
}
}
acDB.SaveAs(newfile, DwgVersion.AC1021);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Tekkit
{
class Program
{
static void Main(string[] args)
{
//make sure to add last 2 using statements
ProcessStartInfo start = new ProcessStartInfo("calc.exe");
Process.Start(start);//starts the process
}
}
}