Free Support Forum -

Validating the image stream retreived during an IMAGE: merge field?


I’m hoping you might have a suggestion as to the best way to proceed on a small problem.

I have a merge template that works very well and includes a merge field with the IMAGE: tag, e.g.:

The problem I’m experiencing is that some records in the data table have no value for the PHOTO_URL column. Apparently if that column is empty, Words still attempts to insert an Image field and it causes Words to fail with:

exception ‘cannot load image
from field ‘property_image_url2’. the field contains data in
unsupported format. empty path name is not legal.’ occured in
aspose.words for .net
for free technical support, please post this error and the file in the
aspose.words forums

The other related issue is that if the PHOTO_URL has a bad URL (file missing, etc), I get the same error. My question: how would you suggest I handle these two situations? I see that there’s an MergeImageFieldEventHandler, and I could use it to check to see if e.FieldValue is null or empty, and that might take care of an empty URL.

But in the case of an invalid URL, do you have any other suggestions as to how I can validate the imagestream as to whether it really is an image, and to prevent Words from attempting to create an image if the validation fails?

You guys always have such great ideas. Many thanks for your suggestions.



Thanks for your request. You was absolutely right; you should use the MergeImageField event. See the following code example.

public void TestMailMerge_99610()


//Create dataTable

DataTable table = new DataTable("table");


DataRow row = table.NewRow();

row[0] = ""; //empty data


DataRow row1 = table.NewRow();

row1[0] = ""; //bad url


DataRow row2 = table.NewRow();

row2[0] = ""; //valid data


//Open template

Document doc = new Document(@"256_99610_DenverMike\in.doc");

//add event handler

doc.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MailMerge_MergeImageField_99610);

//execute mail merge


//save document



void MailMerge_MergeImageField_99610(object sender, MergeImageFieldEventArgs e)


if (!String.IsNullOrEmpty(e.FieldValue.ToString()))




//get response stream from url

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(e.FieldValue.ToString());

request.Method = "GET";

request.ContentType = "multipart/form-data";

request.UserAgent = "Mozilla/4.0+(compatible;+MSIE+5.01;+Windows+NT+5.0";

request.Proxy.Credentials = CredentialCache.DefaultCredentials;

request.Credentials = CredentialCache.DefaultCredentials;

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Stream resStream = response.GetResponseStream();

//create image from stream

Image img = Image.FromStream(resStream);

//insert image into the document

e.Image = img;




//remove field.

//Also you can insert the default image here.






//remove field.

//Also you can insert the default image here.




I hope that this code will help you to solve your problem.

Best regards.

Thank you, Alexey! That was exactly what I needed… it’s working great!