Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programming MS office with C# - is it possible?

MS office apps like Excel can be programmed with Visual Basic for Applications.Is it possible to program MS office with C# for advanced tasks?

like image 708
Fsalad Avatar asked Mar 23 '16 08:03

Fsalad


People also ask

What programming language is used for Microsoft Office?

Visual Basic for Applications (VBA) is an implementation of Microsoft's Event-Driven Programming language Visual Basic 6.0 built into most desktop Microsoft Office applications.

Is Microsoft Word written in C?

Microsoft products like Word, Excel, Powerpoint have been written in both C and C++.

Does Microsoft use C language?

Microsoft's Windows kernel is developed mostly in C, with some parts in assembly language. For decades, the world's most used operating system, with about 90 percent of the market share, has been powered by a kernel written in C.

Is MS Office required for coding?

Only as far as automation via VBA. Many businesses have no idea how much time can be saved through VBA coding. I use it almost every day at work. If you or your company (or whoever) does not work with ms office, there is no reason for it.


2 Answers

Yes, it is. You can use Visual Studio Tools for Office, which allows add-ins to be written in C#, VB.NET, etc. It uses COM interop to communicate with Office.

Also, nowadays you have Office apps, which are a hybrid between JavaScript and possibly a server endpoint, which can be coded in C# too.

like image 121
Patrick Hofman Avatar answered Oct 13 '22 06:10

Patrick Hofman


Definitely. You can install Visual Studio Community 2015 for free. Then look into this folder (path may vary):

C:\Program Files (x86)\Microsoft Visual Studio 12.0\Visual Studio Tools for Office\PIA\Office15

From here you can find dll for each Office app.

Taking Microsoft.Office.Interop.Excel.dll for example, start a new C# project, add that dll into project reference.

using Excel = Microsoft.Office.Interop.Excel;

Then you can initialize your excel app like this:

Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:\path\file.xlsx");
Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[1];

There is a abundance of these tutorials online. DoNotPerls is a good place to start with basic:

  1. Excel
  2. Word
like image 5
Liren Yeo Avatar answered Oct 13 '22 05:10

Liren Yeo