Search and replace then compare error

Hi,

I’m trying to search and replace 2 docx documents and then comparing. The search and replace is done right because I see the 2 documents with the changes, but when the script tries to compare them it returns error. The problem is the target document after saving the changes, because if I change the target document one of the originals it works. So, it seems after saving the documents, the comparison can’t be done.

Here is the script:

builder.OpenFile("http://192.168.1.45:45455/EditorDocumento/GetFile?documentId=3&type=2")
var oDocument = Api.GetDocument();
oDocument.SearchAndReplace({"searchString": "%titulo%", "replaceString": "editar externa test"});
oDocument.SearchAndReplace({"searchString": "%area%", "replaceString": "Gestión de calidad"});
oDocument.SearchAndReplace({"searchString": "%fecha%", "replaceString": "02/01/2025"});

builder.SaveFile("docx", "C:\Users\info\AppData\Local\Temp\output1_.aaf9fb9b-8dcb-4a12-839e-4a65e7fc5ede.Resultado_sustitucion.docx")
builder.CloseFile()
builder.OpenFile("http://192.168.1.45:45455/EditorDocumento/GetFile?documentId=1004&type=2")
oDocument = Api.GetDocument();
oDocument.SearchAndReplace({"searchString": "%titulo%", "replaceString": "editar externa test"});
oDocument.SearchAndReplace({"searchString": "%area%", "replaceString": "Gestión de calidad"});
oDocument.SearchAndReplace({"searchString": "%fecha%", "replaceString": "01/11/2024"});

builder.SaveFile("docx", "C:\Users\info\AppData\Local\Temp\output2_.aaf9fb9b-8dcb-4a12-839e-4a65e7fc5ede.Resultado_sustitucion.docx")
builder.CloseFile()


builder.OpenFile("C:\Users\info\AppData\Local\Temp\output1_.aaf9fb9b-8dcb-4a12-839e-4a65e7fc5ede.Resultado_sustitucion.docx")
oDocument = Api.GetDocument();
const file = builderJS.OpenTmpFile("C:\Users\info\AppData\Local\Temp\output2_.aaf9fb9b-8dcb-4a12-839e-4a65e7fc5ede.Resultado_sustitucion.docx")
AscCommonWord.CompareDocuments(Api, file, null)
file.Close()

builder.SaveFile("docx", "C:\Users\info\AppData\Local\Temp\output.aaf9fb9b-8dcb-4a12-839e-4a65e7fc5ede.Resultado_comparacion.docx")
builder.CloseFile()

If I change the line

const file = builderJS.OpenTmpFile("C:\Users\info\AppData\Local\Temp\output2_.aaf9fb9b-8dcb-4a12-839e-4a65e7fc5ede.Resultado_sustitucion.docx")

with

const file = builderJS.OpenTmpFile("http://192.168.1.45:45455/EditorDocumento/GetFile?documentId=3&type=2")

It works, but is not ok because that is the document before the replace.

Here is the error:

TypeError: Cannot read property '5' of null
    at window.AscCommonWord.rEa.KGb (<anonymous>:22370:254)
    at window.AscCommonWord.rEa.Hq (<anonymous>:22371:195)
    at <anonymous>:22625:458
    at Object.X [as wf] (<anonymous>:12781:175)
    at O (<anonymous>:22625:342)
    at Object.window.AscCommonWord.CompareDocuments (<anonymous>:22714:1)
    at <anonymous>:3:15

Thanks

Also, I’ve tried to compare the replaced documents manually using Word (Office365) and it worked.

Hello @ruben.m :wave:

Thank you for your detailed explanation and the script example. We truly appreciate the effort you’ve put into describing the issue.
We need some time to investigate the problem you’ve reported.

In the meantime, could you please confirm the following:

  • Does manual document comparison in ONLYOFFICE work in this scenario?
  • Specifically, after the documents have been modified and saved via the builder, are you able to compare them manually within ONLYOFFICE?

This information will help us better understand the scope of the issue.
Thank you for your cooperation, and we’ll get back to you as soon as we have an update.

Hi,

The document comparison works without problem in the web editor of ONLYOFFICE community edition as you can see in this image:

I’ve uploaded the output1 file and them compared it with output2 using “Document from file” option.

Thanks

@Nikolas I can send you the output documents if you want. Just say where do you want me to upload them.

Hi @ruben.m :wave:
You can attach the files directly here, upload them to any cloud storage service (like Google Drive, Dropbox, or OneDrive), and share the link with us. Alternatively, if you prefer, you can send the files via a private message.

Hi, here are the documents:

output1_.aaf9fb9b-8dcb-4a12-839e-4a65e7fc5ede.Resultado_sustitucion.docx (25.6 KB)
output2_.aaf9fb9b-8dcb-4a12-839e-4a65e7fc5ede.Resultado_sustitucion.docx (25.7 KB)

Thanks

Hey @ruben.m

Apologies for the delay. I’ve received the files and added them to the analysis. Thank you for providing this information!
I will keep you updated as soon as there’s any progress or details to share.