Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
Microsoft created Word to serve as a word processor. Initially available under the name Multi-Tool Word for Xenix systems, it was introduced on October 25, 1983. Subsequent versions were developed for a wide range of operating systems, such as SCO Unix (1990), Microsoft Windows (1989), Atari ST (1988), OS/2 (1989), AT&T UNIX PC (1985), IBM PCs running DOS (1983), Apple Macintosh running the Classic macOS (1985), macOS (2001), Web browsers (2010), iOS (2014), and Android (2015). Wine can be used to run older versions of Microsoft Word on Linux.
Commercial Word versions can be licensed as a stand-alone application or as a component of Microsoft 365, which can be purchased as a perpetual license or as part of a Microsoft 365 subscription. In this article, we will manipulate Word documents using C# with the help of Microsoft Interop assemblies and explore how IronXL helps us to Edit Excel documents.
Programs written in C# or VB.NET can create or open Word documents (DOC, DOCX, and RTF) with Office Interoperability for Microsoft Word. However, it has a lot of drawbacks when used in projects.
We will discuss frequent issues that you may run across when using Microsoft Office Interop (Word Automation) from C# or VB.NET in this article.
For example:
It is necessary to launch Visual Studio and create a .NET project before using the Interop library. Visual Studio is compatible with any version, though the most recent is advised. Depending on your needs, you can either develop a project template or an application that resembles Windows Forms. I'll be using the Console Application in this case for simplicity's sake.
Next, provide the location and name of the project.
Using the Framework drop-down menu, you can choose a .NET Framework. The Dot.NET Framework 4.7 will be utilized for this project. The next action is to press the "Create" button.
After the application has generated the solution, you may input the code and build or run the program by accessing the Program.cs file.
Now that the Microsoft.Office.Interop.Word
library has been added, we can test the code.
The next repair requires installing the Interop library. Enter the following command in the NuGet Package Manager Console to accomplish this:
:InstallCmd: Install-Package Microsoft.Office.Interop.Word
Another way to find the package "Interop" is to use the NuGet Package Manager. Among all the NuGet packages related to Interop, we may select the required package to download from this list.
Once you have installed all the necessary libraries, you can then start to edit DOCX files.
To use Microsoft Word, you must first create an instance of Microsoft.Office.Interop.Word.Application
. The communication of Word documents would take place in this instance. The next step is to create a new Word document instance using the Documents
property of Microsoft.Office.Interop.Word.Application
instance we just created. As seen in the C# code excerpt below, this allows us to manipulate Word documents, programmatically
using System;
using System.Data;
using Microsoft.Office.Interop.Word;
try
{
var WordApp = new Microsoft.Office.Interop.Word.Application();
var WordDoc = WordApp.Documents.Open(@"d:/Demo.docx");
WordDoc.Paragraphs [1].Range.Text = "New text here...";
WordDoc.SaveAs(@"d:/NewDemo.docx");
WordDoc.Close();
WordApp.Quit();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
using System;
using System.Data;
using Microsoft.Office.Interop.Word;
try
{
var WordApp = new Microsoft.Office.Interop.Word.Application();
var WordDoc = WordApp.Documents.Open(@"d:/Demo.docx");
WordDoc.Paragraphs [1].Range.Text = "New text here...";
WordDoc.SaveAs(@"d:/NewDemo.docx");
WordDoc.Close();
WordApp.Quit();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Imports System
Imports System.Data
Imports Microsoft.Office.Interop.Word
Try
Dim WordApp = New Microsoft.Office.Interop.Word.Application()
Dim WordDoc = WordApp.Documents.Open("d:/Demo.docx")
WordDoc.Paragraphs (1).Range.Text = "New text here..."
WordDoc.SaveAs("d:/NewDemo.docx")
WordDoc.Close()
WordApp.Quit()
Catch ex As Exception
Console.WriteLine(ex.ToString())
End Try
In the above code, we are able to edit the Word document in C#. First, We're creating an item object for the word processor that Interop uses. With the help of the created object which has an open method allows us to pass the existing Word file name. It helps us to convert the file to a document object.
By using the document object we can get all the processes available related to the existing Word document. Then using the paragraphs object we get all the content available in the DOCX files. Using the array we can replace the text of each paragraph with the specific paragraphs by passing them in the array. Then, we can save them into new MS Word documents as a DOCX file. We can also create Word documents by using Interop.
IronXL is an alternative to Microsoft Interop that may be used in .NET programs to handle Excel files. While Microsoft Interop requires interacting with Excel through the Interop assemblies, IronXL offers a more straightforward, effective, and powerful method for programmatically manipulating Excel files in .NET contexts.
Utilizing IronXL instead of Microsoft Interop has several benefits, such as:
For .NET developers who must operate with Excel files programmatically, IronXL is frequently a better option because of its ease of use, speed, and decreased reliance on third-party software installations. The decision between IronXL and Microsoft Interop, however, could be influenced by the particulars of the project, the infrastructure that already exists, and the user's level of expertise with each library.
When deciding between these options, always keep your application's requirements in mind. Check visit this link to learn more about the IronXL library.
Since the IronXL library is needed for the upcoming patch, install it. To finish this, open the NuGet Package Manager Console and type the following command:
Install-Package IronWord
Searching for the package "IronXL" via the NuGet Package Manager is an additional choice. From this list of every NuGet package linked to IronXL, we can select the one we need to download.
Data can be exported to the.XLSX or.XLS formats with just a few lines of code. The following example of source code shows how data can be exported from an Excel file into a simple tabular table format:
var workbook = IronXL.WorkBook.LoadExcel("Demo file.xlsx");
WorkSheet ws = workbook.GetWorkSheet("Sheet1");
string address_val = ws ["A1"].ToString();
console.writeline(address_val);
ws ["A2"].Value = "Hello World";
workbook.SaveAs("export.xlsx");
//or
workbook.SaveAs("export.xls");
//or
workbook.WorkSheets [0].SaveAs("export.xls");
var workbook = IronXL.WorkBook.LoadExcel("Demo file.xlsx");
WorkSheet ws = workbook.GetWorkSheet("Sheet1");
string address_val = ws ["A1"].ToString();
console.writeline(address_val);
ws ["A2"].Value = "Hello World";
workbook.SaveAs("export.xlsx");
//or
workbook.SaveAs("export.xls");
//or
workbook.WorkSheets [0].SaveAs("export.xls");
Dim workbook = IronXL.WorkBook.LoadExcel("Demo file.xlsx")
Dim ws As WorkSheet = workbook.GetWorkSheet("Sheet1")
Dim address_val As String = ws ("A1").ToString()
console.writeline(address_val)
ws ("A2").Value = "Hello World"
workbook.SaveAs("export.xlsx")
'or
workbook.SaveAs("export.xls")
'or
workbook.WorkSheets (0).SaveAs("export.xls")
The previous example loads an Excel file that already exists by calling the LoadExcel
function, which takes an argument for the file path and name. Importing the file into the Workbook
object is now complete. The Excel worksheets are then loaded with the help of GetWorkSheet
, which allows us to load the worksheet using the sheet name. The Excel address was then used to read the value. To know more about reading Excel files click here.
We can alter the Excel sheet's values by utilizing the same Excel URL. The Excel document can be saved as an XLSX or XLS file by utilizing the SaveAs
function that is offered by the 'Workbook' object. Using this process, the entire file is saved in the chosen format.
Additionally, we can choose a certain Excel worksheet by utilizing its index value or by referring to it by name. Next, we may export the data from the Excel spreadsheet to a different file by using the SaveAs
option. Click this link to find out more about formatting and exporting Excel files.
One of the most popular add-ons for Excel is IronXL. It doesn't rely on any additional external libraries. It is not necessary to install Microsoft Excel because it is self-contained. It operates via a multitude of channels. This contrasts with the Interop library which has to parse the file using extra libraries to edit Word documents.
A complete solution for any programming process utilizing Microsoft Excel documents is IronXL. Calculations, sorting strings or numbers, pruning, adding, finding and replacing, merging and unmerging, and file storage are just a few of the many available operations. Not only can spreadsheet data be validated, but you can also construct new forms of cell data. It facilitates reading and writing files as well as handling Excel data.
When IronXL was first released, it cost $749. Alternatively, customers can opt to pay a one-year subscription fee to receive software updates and support. For a charge, IronXL provides security against unauthorized redistribution. Go to the IronXL licensing page. To know more about Iron Software products check here.
9 .NET API products for your office documents