2

I am trying to use an xml file to determine which outlook properties be should be included in a workflow executed via VSTO addin code.

Example

The xml might state that the Subject of the current Outlook.MailItem is required by the workflow. I haven't been able to use reflection to get the Subject property using its string name "Subject" because the MailItem is an interface and not a class.

I thought the solution might be to create and compile dynamic C# code that returns the required property by name...

Problem: I have been unable to work out how to find the location of the running Microsoft.Office.Interop.Outlook.dll so that I can add it as a reference to the dynamic compiler. I have tried a number of combinations, the last effort is shown below.

CodeDomProvider provider = new Microsoft.CSharp.CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
parameters.GenerateExecutable = false;
parameters.GenerateInMemory = true;
parameters.IncludeDebugInformation = false;
parameters.ReferencedAssemblies.Add(Assembly.GetAssembly(typeof(Outlook.MailItem)).Location);
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Visual Micro
  • 1,561
  • 13
  • 26

2 Answers2

1
parameters.ReferencedAssemblies.Add("Microsoft.Office.Interop.Outlook.dll");
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • That does appear to work with the microsoft interop dlls. The interop dlls are from the gac and not present in the folder where my program dlls reside. Maybe, my question should be how to get the gac folder path? – Visual Micro Sep 08 '12 at 19:41
  • @VisualMicro: You don't need a GAC path. Just add assembly by name, it will be resolved by compiler automatically. This code is equal to run `csc /r:YourAssembly.dll` which is your goal, isn't it? – abatishchev Sep 08 '12 at 19:47
  • Yes but this gives compile error "metadata file not found" `parameters.ReferencedAssemblies.Add("Microsoft.Office.Interop.Outlook.dll");` – Visual Micro Sep 08 '12 at 19:53
  • And a bit more info that might help. The `MailItem` is an `interface` and not a `class`. If I simply add a reference to "MyAssembly.dll" I get the following error. `Cannot find the interop type that matches the embedded interop type 'Microsoft.Office.Interop.Outlook.MailItem'. Are you missing an assembly reference?` – Visual Micro Sep 08 '12 at 20:14
  • @Visual: According to [MSDN](http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.mailitem.aspx) this interfere is located in 'Microsoft.Office.Interop.Outlook.dll' – abatishchev Sep 08 '12 at 20:23
  • I have tried to delete my answer because the problem is as result of using embedded interops in the addin. I don't see way around this problem at the moment because switching off embedded will cause future version conflicts/issues with ms office? Thanks for all your help. If you have any bright ideas I will be keen to hear them. I think your response above would be the answer if it wasn't for the way microsoft expect the interop references to be configured. – Visual Micro Sep 08 '12 at 22:12
0

I have the answer now. abatishchev gave me the confidence to know what should be working so I tried setting the compiler include path for the office dlls into the CompilerOptions.

I still need to work out how to get this path for the current version of outlook but that doesn't sound to hard or I might open a new question for it:)

Here is the code that works.

  parameters.ReferencedAssemblies.Add("Microsoft.Office.Interop.Outlook.dll"); 
  parameters.CompilerOptions = "/lib:\"C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\Visual Studio Tools for Office\\PIA\\Office14\"";`

Thanks abatishchev for all your help

UPDATE: Outlook addins use embedded interop assemblies which causes this problem. So it is not possible to determine office dll locations from loaded assemblies. The /lib path needs to be resolved by other means.

Visual Micro
  • 1,561
  • 13
  • 26