How to Find and Replace a Text String in DOCX Using C#

Cloudmersive
2 min readJan 29, 2021

If you need to modify a specific text string in an online Word document, it can be a real pain to find each string and replace it with new content. Fortunately, we’ve got a great API that can cut down processing time and do the searching and replacing for you!

To begin using the API, our first step is to install the .NET Framework SDK:

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;
namespace Example
{
public class EditDocumentDocxReplaceExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
var apiInstance = new EditDocumentApi();
var reqConfig = new ReplaceStringRequest(); // ReplaceStringRequest | Document string replacement configuration input
try
{
// Replace string in Word DOCX document
byte[] result = apiInstance.EditDocumentDocxReplace(reqConfig);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling EditDocumentApi.EditDocumentDocxReplace: " + e.Message );
}
}
}
}

And that’s it! Now all occurrences of the target text string have been located and replaced with minimal effort. If you have multiple text strings you’d like to replace, we’ve got an API for that too. To retrieve your API key and gain access to 800 calls/month across our library of APIs, simply head to the Cloudmersive website and register for a free account.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.