Find Image in Word DOCX Document and Replace this Shape with another Image using C# .NET

Error in C# code that finds and replaces an image with another image
On 2 servers it works, on the 3rd it doesn’t. “Value cannot be null. Parameter name: node.”
Code, paths, files and permissions are the same.
DocumentBuilder builder = new DocumentBuilder(asposeDoc);
Shape shape = (Shape)asposeDoc.GetChild(NodeType.Shape, 0, true);
builder.MoveTo(shape);
shape.Remove(); //This is the cause

@DGeorge,

Please ZIP and attach the following resources here for testing:

  • Your simplified input Word document you are getting this problem with
  • Please also create a standalone simple console application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words DLL files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start investigation into your scenario and provide you more information. Thanks for your cooperation.

Perhaps a better course of action, would be for you to create your own simplified document and use the C# above.

If I create new document and a new application to send, what are we really testing (I already know the code and/or my document errors)? It’s more direct if you can recreate the error independently.

If you can’t reproduce the error using the code above against the same DLL version then we know the code/dll version is solid and can look into the document.

@DGeorge,

In your case, if the shape object is null and still moving cursor to a null Shape by using builder.MoveTo(shape); will cause the following exception.

System.ArgumentNullException
  HResult=0x80004003
  Message=Value cannot be null.
Parameter name: node

And your code will find a null Shape if there is no Shape node in source Word document. You can make the following improvement in your code to avoid this problem:

Document doc = new Document("E:\\Temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = (Shape)doc.GetChild(NodeType.Shape, 0, true);
if (shape != null)
{
    builder.MoveTo(shape);
    shape.Remove(); //This is the cause
}
else
{
    Console.WriteLine("There is no Shape in source Word document");
}
doc.Save("E:\\Temp\\20.3.docx");

Hope, this helps.

Thank you but error handling is not the answer.

The document has a shape (image) in it or the code wouldn’t be searching for it. This still leaves the question(s) does that code work with version 14.11 and if it does, what could be the issues with the shape (image) in our file.

With your access to multiple dll versions you have the ability to confirm this.

If that code does not work with 14.11, can you create code that does?

@DGeorge,

Please note that we do not provide support for older released versions of Aspose.Words. We also do not provide any fixes or patches for old versions of Aspose APIs. All fixes and new features are always added into new versions of our APIs. So, we suggest you to please upgrade to the latest version of Aspose.Words for .NET i.e. 20.3. Hope, this helps.

In case the problem still remains, please provide the resources requested here to be able to reproduce the exact problem with 20.3 version on our end. Thanks for your cooperation.