How to edit a Docx. file without having Microsoft Office installed

Uzair Ghaffar 0 Reputation points
2023-06-25T19:51:27.6866667+00:00
Hi,
I was wondering how to edit - replace a text in a Word file, but without the Word application scope activity.
I cannot install or use Microsoft Office in the environment where I am executing the robot, so how can I replace a text in a Word file under these circunstamces?

I’ve been reading and you’re supposed to achieve it by using the documentformat.OpenXML package along with the Invoke code activity. However, I’ve been trying to do it without success.

This is the code inside of the Invoke code using VBNet:

I’m passing in 4 arguments.

Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Packaging
Imports <a href="
Format.OpenXml.Wordprocessing

Public Sub ReplaceTextAndCreateNewDocx(filePath As String, newFilePath As String, oldText As String, newText As String)
    ' Load the document
    Using wordprocessingDocument As WordprocessingDocument = WordprocessingDocument.Open(filePath, False)
        ' Copy the document to create a new one
        wordprocessingDocument.MainDocumentPart.Document.Save(newFilePath)
    End Using

    ' Load the new document
    Using wordprocessingDocument As WordprocessingDocument = WordprocessingDocument.Open(newFilePath, True)
        ' Get the main document part
        Dim mainPart As MainDocumentPart = wordprocessingDocument.MainDocumentPart
        ' Get the document text
        Dim docText As String = mainPart.Document.Body.InnerText
        ' Replace the text
        docText = docText.Replace(oldText, newText)
        ' Update the document text
        mainPart.Document.Body.InnerXml = docText
    End Using
End Sub
What am I doing wrong? Is there any other workaround?
Thanks!
Microsoft 365 and Office | Development | Other
{count} votes

1 answer

Sort by: Most helpful
  1. Vahid Ghafarpour 23,385 Reputation points Volunteer Moderator
    2023-06-25T20:16:58.3833333+00:00

    Hi,

    I think instead of assigning the updated docText value to mainPart.Document.Body.InnerXml, you should assign it to mainPart.Document.Body.InnerText.

    The InnerXml property returns the text with XML tags while InnerText returns only the text content.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.