I'm working on a VSTO add-in for Word 2010 (.NET Framework 4.6.1) in C#. This add-in is meant to produce Word documents from a template, using data from a few sources, a few of which (depending on the ) being one or more Excel workbooks.
I have been perusing the MSDN literature, but I feel as if I'm missing a crucial step.
Is it possible to access the Microsoft.Office.Tools.Word objects in the same add-in as Microsoft.Office.Tools.Excel objects? They each seem to rely on the Factory.GetVstoObject() method, which uses a factory object that is application-specific. I know I can use the normal Microsoft.Office.Interop.Word/Excel objects, but they aren't as useful.
I have been playing around with having two add-ins, one targeting Word and one targeting Excel, but I'm having a hard time getting them to see each other. If this isn't achievable as a VSTO add-in, can you reference multiple Office application object models in a standalone executable?
Is it possible to access the Microsoft.Office.Tools.Word objects in the same add-in as Microsoft.Office.Tools.Excel objects
Maybe not those exact types but you can find something similar. You can always drop down to pure COM/Ole Automation level completely by-passing VSTO APIs. Office is still exposing a COM API which VSTO I suspect is just wrapping into nicer c# classes.
e.g. from a c# Excel VSTO add-in you can fire up word document by:
try
{
var type = Type.GetTypeFromProgID("Word.Application");
dynamic app = Activator.CreateInstance(type);
app.Visible = true;
dynamic doc = app.Documents.Add();
doc.Content.InsertAfter("Hello world!");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
Note: I am using dynamic (one of the main purposes of dynamic in c# was to make COM easier) here so as to avoid dependence on any specific COM-type library. It's version neutral so it should work any Word version.
I know I can use the normal Microsoft.Office.Interop.Word/Excel objects, but they aren't as useful.
In what sense?
See also
Where to find OLE Automation's documentation, specifically this answer
Disclaimer I never have done any shared add-in I'm going to talk about here (#Cindy Meister would know more)
Till VS 2010 you could create a project called Office Shared add-in. Here is how it looked like in VS 2003
This would be, probably, something what you would need but as said it's no longer available and I seem to remember there were some issues with it anyway.
I found this post where the guy is trying to use something what would mimic the Office Shared add-in template. He claims it's working so that would be worth to try.
Otherwise you would have to use the Office PIA as #MickyD answered
Related
I would like to use Excel functionality from a browser. I currently have an excel based Microsoft office application. It adds an icon to the ribbon and does C# based custom application logic to communicate to a database server.
Since it requires application releases for any changes in the schema I am curious if I can convert this into an web application and still benefit from Excel's built in functionality.
I am expecting this to be possible since Microsoft Excel Online is run inside a browser. My question is, is it possible to add plugin to such online excel document? If yes, any example would be much appreciated.
You absolutely can, and depending on approach you can do so with nothing more than a VSTO project template. This is little more than a ClickOnce application. This can launch a browser within a window to perform the logic.
You may use the Office Interop assemblies to decouple your application from Office apps, yet still communicate easily. Make sure you set Embed Interop Assemblies to true once added as a reference.
For those using Office in the browser, it seems as though Microsoft is shifting into CSOM/JSON/REST, meaning this solution is coded entirely differently than COM. This should help you get started:
https://msdn.microsoft.com/EN-US/library/office/dn268594.aspx.
I am trying to write a C# add-in for PowerPoint with VS2013. But (after some years of absence in programming :) I am struggling with C#.
Task is to let the user choose one installed proofing language and set it for all slides.
First, it seems that PowerPoint doesn't have a function for fetching the installed/available proofing languages, but obviously Word has. So I started with Word and found:
Microsoft.Office.Interop.Word.Languages
But I have not managed to get a collection of the proofing languages; it's an IEnumerable interface so I can't instantiate it.
What is the correct code for this?
You should use the Languages property to get the collection of languages.
_Application.Languages Property
Returns a Languages collection that represents the proofing languages
listed in the Language dialog box.
Here _Application represents the Microsoft Office Word application.
I want to know if this could be done.
I am building a data dictionary for our software system (school project), and I'm thinking of an automated way to do this. Basically I don't use much of Microsoft Word (2007), I only use it in documenting schools stuff, etc. I want to know if its possible to create/edit a Word document programmatically from a template.
The idea is, I will create a page on Word that contains an empty form that will be repeated on every page. For every data that I will input to my program, it will update the corresponding field in the form and skips to the next form.
The purpose of this, is to eliminate copy-paste methods (my habit) and to speed things up when doing the documentation.
Word automation, as suggested by others, will lead you to a world of hurt for two major reasons:
Office is not intended to be run unattended, so it can pop up message boxes at any time, and
It is (probably) not licensed to enable office functionality for computers which don't have it. If you generate a Word document on a web site using automation, you have to make sure that this functionality cannot be reached by computers which don't have office installed (unless they changed this rule in the last years).
I have used Aspose.Words, it costs a little, but it works well and is intended for this.
Not exactly sure what you really want, but creating word documents with c# shouldn't be any problem:
http://support.microsoft.com/kb/316384
If i find out your purpose correctly you need to visit this microsoft msdn link
Manipulating Word 2007 Files with OpenXML
Definitely possible. A fairly easy way of doing it using Office Automation. See this KB article for a basic sample: How to automate Microsoft Word to create a new document by using Visual C#
I think the main difference to that sample will be that you'll open your template and do SaveAs instead of creating a new document, but I can't remember exactly.
However, depending on your exact requirements, there might be better alternatives. For example, it's not recommended to do Office Automation on servers (including on webservers), so if that's needed you might want to look at something else.
You can use com interop of .net framework.
Understanding the Word Object Model from a .NET Developer's Perspective
Building COM Objects in C#
Using COM programming is not the best way as mentioned by erikkallen, I suggest using OPEN XML. It is really easy to use and your document generation operation will be very fast.
http://blog.goyello.com/2009/08/21/how-to-generate-open-xml-file-in-c-in-4-minutes/
http://msdn.microsoft.com/en-us/library/aa338205(v=office.12).aspx
I am in the estimating phase of a project, and one requirement is that my application will create draft emails (with attachments) in MS-Outlook, which the user can then review and send. The app is written in WPF.
The clients will have either Outlook 2003 or Outlook 2007. The files that need to be attached will already exist on the file system when the drafts are generated.
I have done some initial research, but would like to get some opinions from people who have first-hand experience.
Questions:
What tool would you use to
accomplish this?
Will there need to be separate code for Outlook 2003 vs. 2007?
In general, using whatever tools are recommended, is this a relatively straightforward problem to solve?
Thanks for any insight.
a c# wpf application should be able to do the job just fine, you just need to add the outlook libraries in your references and you can work with outlook directly from your WPF app.
I only write for 2003, but I do know that 2003 and 2007 use different libraries. There's probably a clever way check what version of outlook is being and use the methods from the correct library, but it will take some work to figure out.
It's relatively straight forward except for the security prompt you'll get if you send the email. But I suppose if they are going to have them review it first, you should be able to generate the email, open it in an outlook window for them to review, and have them click the send button.
Would you be able to use WebDAV and then simply create the message and drop it in the users' Drafts folder?
Basically, you'll end up using something like this product (or you can roll your own) to create and save the message. You might be able to find an open source solution.
I think there are a couple tools you could use here:
Visual Studio Tools for Office (VSTO)
total control over Outlook
version of Outlook may matter
Assuming you have Exchange, you could use WebDAV, Exchange's XML methodology
version of Outlook wouldn't matter here
Use the built in mailto: functionality
lots of results for using this to include attachments - Google Results
would work for email apps other than Outlook
this probably would be quickest solution, but the least control over the output
An Outlook Add-in is probably a good way to go for this application. The tool set you need is Visual Studio Tools for Office (VSTO). With the possible exception of the WPF requirement, this is pretty straightforward.
Be aware that the API's and the VSTO tools evolved between 2003 and 2007. You can potentially have a single code base but you will need to write for the least common denominator, 2003.
2003 and 2007 also have different Primary Interop Assemblies, the components that bridge the gap between your .NET code and the native code COM interfaces that Outlook has. This can be a challenge when it comes to building and installer for your add-in. If you want your add-in to install the PIA's, you need to detect the version of Office and install the appropriate version, or, more commonly, just build two different installers.
I've never tried to do WPF inside Outlook 2003. There may be some issues with it but I don't know.
If i create a program, which in one small out of the way area, uses Excel automation:
will the application fail when Excel is needed
or will the application fail to start?
Update
Let me ask the same question, but in a more drawn out way:
Will the application be usable by
99.9% of the users who never use the feature that requires Excel
0% of the users, since Excel is not installed.
Let me ask the same question another way:
Will an application fail to initialize that references the COM interop dll's?
Let me ask the same question another way:
Will an application that doesn't use Excel, but references the COM interop DLL's fail to start?
Let me ask the same question another way:
Will an application that doesn't use Excel be usable if Excel is not installed, if that application has a dependancy on the Office Primary Interop dlls?
Let me ask the same question another way:
If my application doesn't use Excel, does the user have to have Excel installed?
The code will properly execute until it tries to make a call to the automation libraries, at that time it will generate an exception.
I have an app that uses Excel automation and I can definitively say that it will fail at runtime, not at load time. In fact we check to see if it's even installed and only show the "Show data in Excel" button if we find it (but the PIAs are deployed to all installs).
If you automate Office... you gotta have Office!
Having said that, the answer changes if you twist the question into: Can I create an Excel spreadsheet without having Excel on the client?
There are many third-party controls to do this; you can write directly to the MS SpreadML XML spec (http://msdn.microsoft.com/en-us/library/aa140066(office.10).aspx; or use third-party libraries that do (like http://www.carlosag.net/Tools/ExcelXmlWriter/).
I think it will fail at runtime, because the Interop Assemblies should be able to load without the COM-Components available on the clients system.
UPDATE: Runtime should mean when you need it!
Using COM in Win32, it will fail when needed.