How can i add Microsoft.Office.Interop.Word to setup file? - c#

I'm creating a WPF application in Visual Studio 2015 that uses Microsoft.Office.Interop.Word for create Word document.When I create setup file for my project and run it in my PC everything is good but when I install and run it on a different PC it doesn't work.
How can i add Microsoft.Office.Interop.Word to setup file?

This interop is registered in the GAC after office / msword install on the PC. (which means you don't need to distribute the dll)
Check here for more info about PIAs. It states the following:
To perform certain development tasks, the PIAs must be installed and
registered in the global assembly cache on the development computer.
Typically, the PIAs are installed automatically when you install
Office on the development computer. For more information, see
Configuring a Computer to Develop Office Solutions.
The Office PIAs are not required on end-user computers to run Office
solutions. For more information, see Designing and Creating Office
Solutions.
Best option is to use some alternative of MsWord (check comments) or you can check on startup in the registry for the office install if available and if not you need to notify the user which functionalities won't work (or disallow usage of the application if more appropriate).

You don't need to explicitly include PIA as part of your set up (from C#4 onward)
In your local - right click assembly properties and make Embed Interop Type to true. Relevant types required to communicate to office com type will get embedded to your assembly.

Related

Cannot add reference to Outlook 2016 (Office 365) Interop (16.0.0.0)

I'm trying to add a reference to 'Microsoft Outlook 16.0 Object Library' in a C# .NET 4.6.1 WPF project, because I have office 2016 installed. Previous versions of the Object Library are incompatible with the 2016 version of office.
If I use Excel's VBA editor, 'Microsoft Outlook 16.0 Object Library' is listed, and exists in C:\Program Files\WindowsApps\Microsoft.Office.Desktop_16010.9126.2116.0_x86_8wekyb3d8bbwe\VFS\ProgramFilesCommonX86\Microsoft Shared\OFFICE16\MSOUTL.OLB, as you can see below:
However, when I use Visual Studio, the COM tab does not list 'Microsoft Office 16.0 Object Library', and when I try to browse to C:\Program Files\WindowsApps, i get 'You dont currently have permission to access this folder', and clicking 'Continue' (to get access) results in 'You have been denied permission to access this folder'.
So basically, the Office 2016 dlls seem to have been tucked away in a folder that is inaccessible to man, dog, and local administrator.
All I'm trying to do is connect to the open Outlook application, and then send an email with an attachment, so perhaps another question to ask is, is there some new fangled way to communicate with Outlook 2016 that hasn't cropped up in my Googleathon?
Also worth noting is that my version of office is installed as a 'Windows Store Application' and therefore does not appear on the usual Add/Remove programs list, so I can't find any 'repair' options for the installation.
Thanks.
Further investigation reveals that I am doing the right thing - trying to add a COM reference to 'Microsoft Outlook 16.0 Object Library' is now the correct way to target Outlook ... https://stackoverflow.com/a/21018418/5040941.
I've checked the GAC, and the assemblies aren't registered. I've done a repair via the 'Apps and Features' right-click start menu option, and they still aren't appearing - it's as if the Windows Store apps just shove their dlls into the Program Files\WindowsApps\* folder, and don't bother to register them in the GAC.
In answer to the much appreciated comments!
Using the following code
Outlook.Application application = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application");
If I use the nuget Microsoft.Office.Interop.Outlook package, i get a System.Runtime.InteropServices.COMException: 'Invalid class string (Exception from HRESULT: 0x800401F3 (CO_E_CLASSSTRING))' exception.
If I try using Microsoft Outlook 15.0 Object Library or Microsoft Outlook 14.0 Object Library references via the Assemblies/Extensions tab of Add Reference, I get a System.Runtime.InteropServices.COMException: 'Invalid class string (Exception from HRESULT: 0x800401F3 (CO_E_CLASSSTRING))' exception.
I don't think accessing the WindowsApps folder is the way to go.
Firstly, it's locked down for some reason which typically indicates the potential for trouble if i start digging around and changing security permissions, not to mention the fact that maintaining a copy of the latest dll in my solution will undoubtedly add unnecessary complexity to my project.
Secondly, but more importantly, if I have office installed, and I can access 'Microsoft Outlook 16.0 Object Library' from Excel Vba, then why can't I access it from Visual Studio?
There is obviously some issue with the installation not registering the dlls properly, only Excel has some trick it uses to make the reference available anyway.
Has anyone reading this managed to install Office 365/2016 Windows Store and found a reference to 'Microsoft Outlook 16.0 Object Library' in Visual Studio?
Am I the only with this problem?! Because it affects multiple computers in my office running Windows 10 and Office 365 ...
In answer to RogerN (thanks), please see the image below - I don't have Microsoft.Office.Interop.Outlook version 16 listed, and Microsoft.Office.Core isn't listed either.
If I try to use the v15 library, I get the following error:
System.Runtime.InteropServices.COMException
HResult=0x80040154
Message=Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
Source=mscorlib
*AND THE ANSWER IS *
The two computers I bought from Dell are 64 bit, but they come preinstalled with 32 bit, windows store versions of MS Office 2016/365.
In order to fix the problem, I had to:
uninstall the store app from Settings > Apps > Apps&Features > Microsoft Office Desktop Apps > Uninstall
log into portal.office.com
select install office apps, advanced, choosing the 64-bit version
Quite why Dell think we want 32 bit versions installed on 64 bit machines, or why someone from Microsoft hasn't posted a solution to this problem, is beyond my understanding, but nevertheless, RogerN was correct. Thanks to everyone for taking time out their days to help me out.
Elaborating on my earlier comment: From what you've described, it sounds like you've installed a 32-bit version of Microsoft Office on a 64-bit machine. Only 32-bit COM objects would have been registered, and therefore a 64-bit .NET application would be unable to find them. If you search your registry, you ought to find that the 0006F03A-0000-0000-C000-000000000046 class ID is only registered under the Wow6432Node.
To resolve the issue, you could either install the 64-bit version of Office or force your .NET application to target the x86 platform.

Does Primary Interop Assemblies ( PIA ) require Microsoft Office to be installed in the machine to work

Does Primary Interop Assemblies ( PIA ) require Microsoft Office to be installed in the machine to work ?
I have gone through so many confusing post/sites and unable arrivie at a conclusion.
I have a machine where office is not installed and I am using VS2010 . As Microsoft.Office.Interop.Excel assembly was not avaialble in COM section in referance . I add it through thought .Net section in referances . Still it throws the below error .
Retrieving the COM class factory for component with CLSID failed due
to the following error: 80040154 Class not registered (Exception from
HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
This is statement from Mircosoft website , The Office PIAs are not required on end-user computers to run Office solutions . What does this mean exactly ?
Does it mean without Office installation we can work with interop assemblies ?
If not why we require PIA when office needs to be installed to get things work ?
This is statement from Mircosoft website , The Office PIAs are not required on end-user computers to run Office solutions . What does this mean exactly ?
That's not the current quote I find. The one I find is:
The PIAs must also be installed on end-user computers to run Office solutions that target the .NET Framework 3.5. However, the Office PIAs are not required on end-user computers to run Office solutions that target the .NET Framework 4. For more information, see Designing and Creating Office Solutions. (https://msdn.microsoft.com/en-us/library/vstudio/hy7c6z9k(v=vs.100).aspx)
It means (for net 4+) that when you create an Office Solution in VS usint PIAs, you do not need to install those PIAs on the target computer to be able to run the solution. (the reason is because, if you've targeted Net4, if the Embed Interop Types property of each Office PIA reference in the project is set to True (this is the default value), the type information for the PIA types that are used by your solution is embedded into the solution assembly when you build the project. At run time, the embedded type information is used instead of the PIAs to call into the Office application's COM-based object model. For more information about how types from PIAs are embedded into your solution (https://msdn.microsoft.com/en-us/library/vstudio/3295w01c(v=vs.100).aspx)
For example: I want to write a program in VB to doSomething on an XLSX sheet.
I have to install Excel, PIA, and VS on my development machine or I will not be able to write it.
Once this program is finished, I want Bob in accounting to run it. So I publish the program and it gets installed on Bob's machine. Bob will not need to have installed PIAs on his local machine to run the program. He will need office.
Does it mean without Office installation we can work with interop assemblies ?
The other way around. It means, with Net 4+ applications using PIA (EIT), your application can work without PIAs installed on the client machine (the needed pieces will be part of your deployment).
Does Primary Interop Assemblies ( PIA ) require Microsoft Office to be installed in the machine to work ?
Yes.
The PIA are simply assemblies that allow you to call the Office COM object model from .NET. The COM objects which are hosted by the Office applications still need to be present for this to work. The Class not registered error you get is because the COM object is missing on the computer. To fix that you need to install the version of Office your application is designed to work with.
Your question really has two parts:
What does a developer need to develop an application that makes use of Office components?
What does an end user need to run that application?
On MSDN, the article Office Solutions Development Overview (Visual Studio 2013) provides concise guidance for these questions. From the section "Automating Office Applications by Using Primary Interop Assemblies":
You must have the Office PIAs installed and registered in the global
assembly cache on your development computer to perform most
development tasks. For more information, see Configuring a Computer
to Develop Office Solutions. The Office PIAs are not required on
end-user computers to run Office solutions. For more information, see
Designing and Creating Office Solutions.
The first article makes it clear that the developer must install Microsoft Office:
To create add-ins and customizations for Microsoft Office, install a
supported version of Visual Studio, the .NET Framework, and Microsoft
Office.
And from the latter article in the section "Understanding When the Office PIAs Are Required on End User Computers":
By default, Office primary interop assemblies (PIAs) do not need to be
installed on end user computers if the Embed Interop Types
property of each Office PIA reference in the project is set to
True, which is the default value. In this scenario, the type information for the PIA types that are used by your solution is
embedded into the solution assembly when you build the project. At run
time, the embedded type information is used instead of the PIAs to
call into the Office application's COM-based object model. For more
information about how types from PIAs are embedded into your solution,
see Type Equivalence and Embedded Interop Types.

deploy a windows form application which links to two excel files created on visual studio 2010 on another system

I have created a windows form application in visual studio 2010 which has 4 forms in it. 3 of these files are connected to 2 different excel files. Now, I want this application to run on another system which has the same excel files. I have just made the application but I am clueless on what to do in order to run it as an application on the other system.
I understand that you are relying on "Interop" to communicate with Excel. In that case, you have to make sure that Microsoft Office is installed on the target computer. In order to avoid problems, confirm that the version is equal to the one you used to create the application.
If the target version of Office is 2007 or older, the Redistributable Primary Interop Assemblies have to be installed on the given computer (link to the 2007 version).

Deploying a .NET C# Console App

All,
I created a C# console app in VS2010 (.NET4). that hits a database and sends out emails.
It works fine when I run it from VS but deploying the app to a remote server has me befuddled. All I need to do is install this app on (1) remote windows server.
Should be easy, right?
Looking at the publish settings, I don't see anything that will just build it locally without creating an installer (From a CD-Rom or DVD-ROM) and the other 2 options really don't apply either, at least by their descriptions.
So here is what I did so far:
The VS2010 publishing options that I am given are as follows:
Step #1 Picked option 3
Step #2 Place generated files on remote server
Step #3 Ran the setup installer
Step #4 Get this error
Question
Am I approaching this correctly?
If not, what do I need to do?
Thanks
Notice the error:
The application requires that assembly office Version 12.0.0.0 be installed in the Global Assembly Cache (GAC) first.
Refer to this answer and this MSDN question which contains the answer I have quoted below:
We solved it by going into the Applications Files dialog under the publish tab of the Project's properties and changing the office.dll assembly to Include. It had previously been set to prerequisite (auto). --Dave3182
It looks like your application is leveraging Microsoft Office (2003, I think) COM objects. This will require the same version of Office to be installed on your server.
If you are leveraging Office format files (.doc, .docx, .xls, .xlsx for example), you can look into 3rd party libraries that allow you to create the formatted files without Office installed. If you are just using Outlook to send the emails, you should re-implement the feature using the .NET libraries instead.

VSTO application add-in won't load - says "The Managed Add-in Loader failed to initialize"

I have a PowerPoint 2010 application-level add-in developed using VS2012 and VSTO. It works with PowerPoint 2007 too. I built an installer for it using the (terrible) InstallShield LE crippleware provided with VS2012. I've been able to install it on various test machines, but now that I'm trying to deploy it to a client, they can't install it on some machines.
One user at the client's site can install it, but that user works in IT and has admin rights on his machine. However, when the IT admins try to install it on a "normal" user's machine, the install seems to go OK but the add-in is not loaded by PowerPoint.
The "COM add-ins" dialog shows the message:
Load behavior: Not loaded. The Managed Add-in Loader failed to initialize.
[In case it's relevant, this client uses Office 2007. Note however that it works OK on Office 2007 both on my test hardware and for that one user at the client.]
Is there anything I can do to figure out why? Is there anything obvious that I should be doing as part of the installation? Clearly the registry entries are being set, because the add-in is listed in the COM Add-ins dialog. And I assume that all the required files are installed, because the installer works fine on other machines.
I guess it may be a security-related issue. I should note that I'm not doing anything security-wise, and I don't think the InstallShield thing is doing anything helpful in this regard. I don't know whether it's necessary to digitally sign my code (I haven't), or somehow set some security policy stuff (I wouldn't know how). I've seen some stuff on-line about using CasPol (?) to apply security settings to add-ins, but that applied to older versions of VS and/or ClickOnce, and I wasn't sure whether I needed any of that.
It turns out that the VSTO runtime is not installed with Office 2007 (it is installed with Office 2010). The IT user who could load the add-in already had it because he'd installed something else that included it (I guess). The other users did not have it.
So, I updated my installer to include it, and it now works.

Categories

Resources