Hello,
Welcome to Microsoft Q&A!
MS Word documents are different from regular text files, so the Storage apis can't directly read or edit the text for you. You could try to use the Open XML SDK to achieve it, before using it, you need to install the DocumentFormat.OpenXml nuget package. Then using WordprocessingDocument.Open(Stream stream, bool isEditable) method to create a instance of WordprocessingDocument and edit the text in it. Here are some scenarios for [Word Document] (https://learn.microsoft.com/en-us/office/open-xml/word-processing?redirectedfrom=MSDN) you can check it. I will take an example of opening a document and adding new text as an example:
private async void Button_Click(object sender, RoutedEventArgs e)
{
StorageFolder f = KnownFolders.PicturesLibrary;
StorageFile sampleFile = await f.GetFileAsync("New Microsoft Word Document.docx");
string strTxt = "Append text in body - OpenAndAddTextToWordDocument";
Stream randomAccessStream = await sampleFile.OpenStreamForWriteAsync();
OpenAndAddTextToWordDocument(randomAccessStream, strTxt);
}
public static void OpenAndAddTextToWordDocument(Stream stream, string txt)
{
// Open a WordprocessingDocument for editing using the stream.
WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(stream, true);
// Assign a reference to the existing document body.
Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
// Add new text.
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text(txt));
// Close the handle explicitly.
wordprocessingDocument.Close();
}
Thanks.