We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Conditionally insert image

’m completeting a merge with regions. I would like to insert an image if the value of the field SaleTypeTxt is equal to Hart Exclusive agency. How would I modify my below code to do so? Right now it inserts the image no matter what, but I need it to be conditionally done. I attached my template with the image field in the far column…

Dim doc As New Document(“d:\hart11\templates\listingsummary.doc”)

Dim adapterTemplate1 As New SQLDataAdapter(“SELECT Custom2RefTxt, TransListingStatusTxt, AddressCalc, CASE WHEN CityTxt > ‘’ THEN CHAR(10) + CityTxt ELSE ‘’ END AS CityTxt, StateCode, PostalCodeTxt, REPLACE(CONVERT(varchar, CAST(CASE WHEN SFInt > 0 THEN SFInt ELSE NULL END AS money), 1),’.00’,’’) AS SFInt, CASE WHEN CeilingHeightMinCalc1 > ‘0’ THEN CeilingHeightMinCalc1 ELSE NULL END AS CeilingHeightMinCalc1, CASE WHEN CeilingHeightMinCalc2 > ‘0’ THEN CeilingHeightMinCalc2 ELSE NULL END AS CeilingHeightMinCalc2, CASE WHEN CeilingHeightMaxCalc1 > ‘0’ THEN CeilingHeightMaxCalc1 ELSE NULL END AS CeilingHeightMaxCalc1, CASE WHEN CeilingHeightMaxCalc2 > ‘0’ THEN CeilingHeightMaxCalc2 ELSE NULL END AS CeilingHeightMaxCalc2, ACTxt, RailInstalledYNTxt, ISNULL(CONVERT (varchar, CAST(CASE WHEN SaleAskingPriceInt > 0 THEN SaleAskingPriceInt ELSE NULL END AS money), 1), ‘0’) AS SaleAskingPriceInt, CASE WHEN LeaseAskingRateDec > ‘0’ THEN LeaseAskingRateDec ELSE NULL END AS LeaseAskingRateDec, LeaseAskingTypeTxt, LandAcresDec, CASE WHEN ExtraLandAcresDec > ‘0’ THEN ExtraLandAcresDec ELSE NULL END AS ExtraLandAcresDec FROM ViewListing INNER JOIN ExportDetail ON ExportDetail.ExportPrimaryRecordID = ViewListing.TransID WHERE ExportDetail.ExportID=’” & Request.QueryString(“ID”) & “’”, myConnection)

Dim dtTemplate1 As New DataTable()

adapterTemplate1.Fill(dtTemplate1)

Dim recordCount As Integer

recordCount = dtTemplate1.Rows.Count

dtTemplate1.TableName = “Summary”

doc.MailMerge.ExecuteWithRegions(dtTemplate1)

doc.MailMerge.FieldMergingCallback = New HandleMergeImageFieldFromBlobListingSummary()

doc.MailMerge.Execute(New String() {“RecordCount”, “PreparedBy”, “PreparedFor”}, New Object() {recordCount, rcbPreparedBy.Text, rcbPreparedFor.Text})

doc.MailMerge.DeleteFields()

If qExportTemplateID = “33” Then 'Save As PDF

doc.Save(REPLACE(SavePath,“doc”,“pdf”))

Else

doc.Save(SavePath)

End If

Private Class HandleMergeImageFieldFromBlobListingSummary

Implements IFieldMergingCallback

Private Sub IFieldMergingCallback_FieldMerging(ByVal args As FieldMergingArgs) Implements IFieldMergingCallback.FieldMerging

End Sub

Private Sub ImageFieldMerging(ByVal e As ImageFieldMergingArgs) Implements IFieldMergingCallback.ImageFieldMerging

e.ImageFileName = System.IO.Path.Combine(“d:\hart11\images”, “hartlogoxsmall.gif”)

End Sub

End Class

Hi Ryan,

Thanks for your inquiry.

Please try the code below which should achieve what you are looking for.

Private Class HandleMergeImageFieldFromBlobListingSummary Implements IFieldMergingCallback

Dim mIsMergeImage As Boolean = False

Private Sub IFieldMergingCallback_FieldMerging(ByVal args As FieldMergingArgs) Implements IFieldMergingCallback.FieldMerging

' This field is SaleTypeTxt
If (args.FieldName = "SaleTypeTxt") Then
' If this is the correct FieldValue then merge the image
If (args.FieldValue = "Hart Exclusive Agency") Then
mIsMergeImage = True

Else
' Don't merge the image
mIsMergeImage = False
End If
End If
End Sub


Private Sub ImageFieldMerging(ByVal e As ImageFieldMergingArgs) Implements IFieldMergingCallback.ImageFieldMerging

' Check should merge image
If (mIsMergeImage) Then
e.ImageFileName = System.IO.Path.Combine("d:\hart11\images\", "hartlogoxsmall.gif")
End If

End Sub
End Class

Thanks,

No values seems to be getting passed to the fieldmergingcallback. No image is inserted, unless I remove all logic and just leave mIsMergeField = True. This does insert the image so I assume my template is fine. Am I missing something for passing the SaleTypeTxt string to the handler?

Dim DocumentString As String

If qExportTemplateID = “32” OR qExportTemplateID = “33” Then

DocumentString = “d:\hart11\templates\listingsummary.doc”

Else If qExportTemplateID = “34” OR qExportTemplateID = “35” Then

DocumentString = “d:\hart11\templates\listingsummary_mom.doc”

Else If qExportTemplateID = “36” OR qExportTemplateID = “37” Then

DocumentString = “d:\hart11\templates\soldsummary.doc”

End If

Dim doc As New Document(DocumentString)

Dim adapterTemplate1 As New SQLDataAdapter(“SELECT Custom2RefTxt, TransListingStatusTxt, AddressCalc, CASE WHEN CityTxt > ‘’ THEN CHAR(10) + CityTxt ELSE ‘’ END AS CityTxt, StateCode, PostalCodeTxt, REPLACE(CONVERT(varchar, CAST(CASE WHEN SFInt > 0 THEN SFInt ELSE NULL END AS money), 1),’.00’,’’) AS SFInt, CASE WHEN CeilingHeightMinCalc1 > ‘0’ THEN CeilingHeightMinCalc1 ELSE NULL END AS CeilingHeightMinCalc1, CASE WHEN CeilingHeightMinCalc2 > ‘0’ THEN CeilingHeightMinCalc2 ELSE NULL END AS CeilingHeightMinCalc2, CASE WHEN CeilingHeightMaxCalc1 > ‘0’ THEN CeilingHeightMaxCalc1 ELSE NULL END AS CeilingHeightMaxCalc1, CASE WHEN CeilingHeightMaxCalc2 > ‘0’ THEN CeilingHeightMaxCalc2 ELSE NULL END AS CeilingHeightMaxCalc2, ACTxt, RailInstalledYNTxt, CONVERT (varchar, CAST(CASE WHEN SaleAskingPriceInt > 0 THEN SaleAskingPriceInt ELSE NULL END AS money), 1) AS SaleAskingPriceInt, CONVERT (varchar, CAST(CASE WHEN SaleActualPriceInt > 0 THEN SaleActualPriceInt ELSE NULL END AS money), 1) AS SaleActualPriceInt, CASE WHEN AskingPricePerSFCalc > ‘0.00’ THEN AskingPricePerSFCalc ELSE NULL END AS AskingPricePerSFCalc, CASE WHEN PricePerSFCalc > ‘0.00’ THEN PricePerSFCalc ELSE NULL END AS PricePerSFCalc, CASE WHEN LeaseAskingRateDec > ‘0’ THEN LeaseAskingRateDec ELSE NULL END AS LeaseAskingRateDec, LeaseAskingTypeTxt, CASE WHEN LeaseActualRateDec > ‘0’ THEN LeaseActualRateDec ELSE NULL END AS LeaseActualRateDec, LeaseActualTypeTxt, LandAcresDec, CASE WHEN ExtraLandAcresDec > ‘0’ THEN ExtraLandAcresDec ELSE NULL END AS ExtraLandAcresDec, CASE WHEN SaleTypeTxt = ‘Hart Exclusive Agency’ THEN ‘H’ ELSE NULL END AS H, SaleTypeTxt, CASE WHEN TransMOM > ‘’ THEN TransMOM ELSE NULL END AS TransMOM FROM ViewListing INNER JOIN ExportDetail ON ExportDetail.ExportPrimaryRecordID = ViewListing.TransID WHERE ExportDetail.ExportID=’” & Request.QueryString(“ID”) & “’ ORDER BY StateCode ASC, SFInt DESC, CityTxt ASC, Custom2RefTxt ASC”, myConnection)

Dim dtTemplate1 As New DataTable()

adapterTemplate1.Fill(dtTemplate1)

Dim recordCount As Integer

recordCount = dtTemplate1.Rows.Count

dtTemplate1.TableName = “Summary”

doc.MailMerge.FieldMergingCallback = New HandleMergeImageFieldFromBlobListingSummary()

doc.MailMerge.ExecuteWithRegions(dtTemplate1)

doc.MailMerge.Execute(New String() {“RecordCount”, “PreparedBy”, “PreparedFor”}, New Object() {recordCount, rcbPreparedBy.Text, rcbPreparedFor.Text})

doc.MailMerge.DeleteFields()

If qExportTemplateID = “33” OR qExportTemplateID = “35” OR qExportTemplateID = “37” Then 'Save As PDF

doc.Save(REPLACE(SavePath,“doc”,“pdf”))

Else

doc.Save(SavePath)

End If

I think I found my issue. I guess I have to physically show the SaleTypeTxt value for each record in the merge template itself? I added it to the template and now the merge works… I guess I thought it would work off the SQL query and then update the template, not reference the template itself…

Hi Ryan,

Yes the values for that field need to be passed in the DataTable to the merge engine for the code to work. It’s great that it’s working as expected now.

If you need any further help please feel free to ask.

Thanks,

The field is in my datatable. Is there anyway to not have to have a mergefield with the saletypetxt in the template for this to work? I dont want to show the SaleTypeTxt, but it wont merge the image unless I have a merge field named SaleTypeTxt in the document.

Hi

Thanks for your inquiry. In your case, mergefield is like placeholder where image will be inserted. If there will be no placeholder, how will you determine place where the image should be inserted?

Of course, if it is acceptable for you, you can just insert an image at another mergefield, for example, you can use DocumentBuilder.InsertImage in IFieldMergingCallback.

Best regards,

No, I have my ImageField merge object and above it I have a normal mergefield titled SaleTypeTxt. If I remove the saletypetxt, the image field is not populated. The image shows based upon what is in that saletypetxt. I would like to not have to show the saletypetxt in the actual word document.

Also, is there anyway aspose.words can capture an image from a webservice? I’m trying to find a way to insert a bing map into my word document and its accessible as an image from a web service.

Hello

Thank you for additional information. Could you please attach simple template and code which will allow me to reproduce the problem on my side? I will investigate them and provide you more information.

Regarding the second question, please try using the following code and the attached template to achieve what you need:

Document doc = new Document("in.doc");
doc.MailMerge.Execute(new string[] { "url" }, new object[] { "http://www.bing.com/local/GetMap.ashx?ppl=34,,40.1640379563293,-75.0469702264019&b=r,stl.h,mkt.en-us&rf=o&rp=n&z=16&c=40.164038,-75.04697&w=659&h=602" });

doc.Save("out.doc");

Best regards,

That does indeed load the map which is really nice. I would like to customize it though. I have geocodeLongitude and geocodeLatitude fields on my table. How do I add that string to a handler and process it there? This doesnt look right to me…

Dim Query As String = “SELECT ROW_NUMBER() OVER(ORDER BY ViewContact.ContactID ASC) AS ‘Record’, (SELECT Count(ContactID) FROM ViewContact INNER JOIN ExportDetail ON ExportDetail.ExportPrimaryRecordID = ViewContact.ContactID WHERE ExportDetail.ExportID=’” & Request.QueryString(“ID”) & “’) AS RecordCount, ViewContact.ContactID, geocodeLatitude, geocodeLongitude, CASE WHEN ContactTxt = ‘’ THEN ‘-’ ELSE ContactTxt END AS ContactTxt, CASE WHEN TitleTxt = ‘’ THEN ‘-’ ELSE TitleTxt END AS TitleTxt, CASE WHEN CompanyTxt = ‘’ THEN ‘-’ ELSE CompanyTxt END AS CompanyTxt, CASE WHEN ContactKeyYNTxt = ‘’ THEN ‘-’ ELSE ContactKeyYNTxt END AS ContactKeyYNTxt, CASE WHEN ContactCategoryTxt = ‘’ THEN ‘-’ ELSE ContactCategoryTxt END AS ContactCategoryTxt, case when len(ltrim(rtrim(REPLACE(REPLACE(PhoneDirectTxt,’-’,’’),’.’,’’))))=‘10’ then ‘’+SUBSTRING(REPLACE(REPLACE(PhoneDirectTxt,’-’,’’),’.’,’’),1,3)+’.’+’’+SUBSTRING(REPLACE(REPLACE(PhoneDirectTxt,’-’,’’),’.’,’’),4,3)+’.’+SUBSTRING(REPLACE(REPLACE(PhoneDirectTxt,’-’,’’),’.’,’’),7,4) else CASE WHEN PhoneDirectTxt = ‘’ THEN ‘-’ ELSE PhoneDirectTxt END end AS PhoneDirectTxt, PhoneDirectExtTxt, case when len(ltrim(rtrim(REPLACE(REPLACE(FaxDirectTxt,’-’,’’),’.’,’’))))=‘10’ then ‘’+SUBSTRING(REPLACE(REPLACE(FaxDirectTxt,’-’,’’),’.’,’’),1,3)+’.’+’’+SUBSTRING(REPLACE(REPLACE(FaxDirectTxt,’-’,’’),’.’,’’),4,3)+’.’+SUBSTRING(REPLACE(REPLACE(FaxDirectTxt,’-’,’’),’.’,’’),7,4) else CASE WHEN FaxDirectTxt = ‘’ THEN ‘-’ ELSE FaxDirectTxt END end AS FaxDirectTxt, case when len(ltrim(rtrim(REPLACE(REPLACE(PhoneGeneralTxt,’-’,’’),’.’,’’))))=‘10’ then ‘’+SUBSTRING(REPLACE(REPLACE(PhoneGeneralTxt,’-’,’’),’.’,’’),1,3)+’.’+’’+SUBSTRING(REPLACE(REPLACE(PhoneGeneralTxt,’-’,’’),’.’,’’),4,3)+’.’+SUBSTRING(REPLACE(REPLACE(PhoneGeneralTxt,’-’,’’),’.’,’’),7,4) else CASE WHEN PhoneGeneralTxt = ‘’ THEN ‘-’ ELSE PhoneGeneralTxt END end AS PhoneGeneralTxt, case when len(ltrim(rtrim(REPLACE(REPLACE(FaxGeneralTxt,’-’,’’),’.’,’’))))=‘10’ then ‘’+SUBSTRING(REPLACE(REPLACE(FaxGeneralTxt,’-’,’’),’.’,’’),1,3)+’.’+’’+SUBSTRING(REPLACE(REPLACE(FaxGeneralTxt,’-’,’’),’.’,’’),4,3)+’.’+SUBSTRING(REPLACE(REPLACE(FaxGeneralTxt,’-’,’’),’.’,’’),7,4) else CASE WHEN FaxGeneralTxt = ‘’ THEN ‘-’ ELSE FaxGeneralTxt END end AS FaxGeneralTxt, case when len(ltrim(rtrim(REPLACE(REPLACE(PhoneMobileTxt,’-’,’’),’.’,’’))))=‘10’ then ‘’+SUBSTRING(REPLACE(REPLACE(PhoneMobileTxt,’-’,’’),’.’,’’),1,3)+’.’+’’+SUBSTRING(REPLACE(REPLACE(PhoneMobileTxt,’-’,’’),’.’,’’),4,3)+’.’+SUBSTRING(REPLACE(REPLACE(PhoneMobileTxt,’-’,’’),’.’,’’),7,4) else CASE WHEN PhoneMobileTxt = ‘’ THEN ‘-’ ELSE PhoneMobileTxt END end AS PhoneMobileTxt, case when len(ltrim(rtrim(REPLACE(REPLACE(PhoneHomeTxt,’-’,’’),’.’,’’))))=‘10’ then ‘’+SUBSTRING(REPLACE(REPLACE(PhoneHomeTxt,’-’,’’),’.’,’’),1,3)+’.’+’’+SUBSTRING(REPLACE(REPLACE(PhoneHomeTxt,’-’,’’),’.’,’’),4,3)+’.’+SUBSTRING(REPLACE(REPLACE(PhoneHomeTxt,’-’,’’),’.’,’’),7,4) else CASE WHEN PhoneHomeTxt = ‘’ THEN ‘-’ ELSE PhoneHomeTxt END end AS PhoneHomeTxt, case when len(ltrim(rtrim(REPLACE(REPLACE(FaxHomeTxt,’-’,’’),’.’,’’))))=‘10’ then ‘’+SUBSTRING(REPLACE(REPLACE(FaxHomeTxt,’-’,’’),’.’,’’),1,3)+’.’+’’+SUBSTRING(REPLACE(REPLACE(FaxHomeTxt,’-’,’’),’.’,’’),4,3)+’.’+SUBSTRING(REPLACE(REPLACE(FaxHomeTxt,’-’,’’),’.’,’’),7,4) else CASE WHEN FaxHomeTxt = ‘’ THEN ‘-’ ELSE FaxHomeTxt END end AS FaxHomeTxt, CASE WHEN EmailURL = ‘’ THEN ‘-’ ELSE EmailURL END AS EmailURL, CASE WHEN REPLACE(WebURL,‘http://’,’’) = ‘’ THEN ‘-’ ELSE REPLACE(WebURL,‘http://’,’’) END AS WebURL, CASE WHEN Address1Txt > ‘’ THEN Address1Txt ELSE ‘-’ END AS Address1Txt, CASE WHEN Address2Txt > ‘’ THEN CHAR(10) + Address2Txt ELSE ‘’ END AS Address2Txt, CASE WHEN CityTxt > ‘’ THEN CHAR(10) + CityTxt ELSE ‘’ END AS CityTxt, CASE WHEN StateCode > ‘’ THEN StateCode ELSE ’ ’ END AS StateCode, CASE WHEN PostalCodeTxt > ‘’ THEN PostalCodeTxt ELSE ’ ’ END AS PostalCodeTxt, CASE WHEN CountryCode > ‘’ THEN CHAR(10) + CountryCode ELSE ‘’ END AS CountryCode, CASE WHEN ShippingAddress1Txt > ‘’ THEN ShippingAddress1Txt ELSE ‘-’ END AS ShippingAddress1Txt, CASE WHEN ShippingAddress2Txt > ‘’ THEN CHAR(10) + ShippingAddress2Txt ELSE ’ ’ END AS ShippingAddress2Txt, CASE WHEN ShippingCityTxt > ‘’ THEN CHAR(10) + ShippingCityTxt ELSE ’ ’ END AS ShippingCityTxt, CASE WHEN ShippingStateCode > ‘’ THEN ShippingStateCode ELSE ’ ’ END AS ShippingStateCode, CASE WHEN ShippingPostalCodeTxt > ‘’ THEN ShippingPostalCodeTxt ELSE ’ ’ END AS ShippingPostalCodeTxt, CASE WHEN ShippingCountryCode > ‘’ THEN CHAR(10) + ShippingCountryCode ELSE ’ ’ END AS ShippingCountryCode, ContactProfileMemo FROM ViewContact LEFT OUTER JOIN ContactGeoCode ON ContactGeoCode.ContactID = ViewContact.ContactID INNER JOIN ExportDetail ON ExportDetail.ExportPrimaryRecordID = ViewContact.ContactID WHERE ExportDetail.ExportID=’” & Request.QueryString(“ID”) & “’”

If qExportTemplateID = “24” Then

Dim doc As New Document(“d:\hart11\templates\contactbasicprofile.doc”)

Dim adapterTemplate1 As New SQLDataAdapter(Query, myConnection)

Dim dtTemplate1 As New DataTable()

adapterTemplate1.Fill(dtTemplate1)

Dim recordCount As Integer

recordCount = dtTemplate1.Rows.Count

doc.MailMerge.FieldMergingCallback = New HandleMergeFieldContactMap()

doc.MailMerge.Execute(dtTemplate1)

doc.Save(SavePath)

Private Class HandleMergeFieldContactMap

Implements IFieldMergingCallback

Dim mIsMergeImage As Boolean = False

Private Sub FieldMerging(args As FieldMergingArgs) Implements IFieldMergingCallback.FieldMerging

doc.MailMerge.Execute(New String() {“url”}, New Object() {“http://www.bing.com/local/GetMap.ashx?ppl=34,,40.1640379563293,-75.0469702264019&b=r,stl.h,mkt.en-us&rf=o&rp=n&z=16&c=40.164038,-75.04697&w=659&h=602”})

End Sub

Private Sub ImageFieldMerging(e As ImageFieldMergingArgs) Implements IFieldMergingCallback.ImageFieldMerging

End Sub

End Class

I got this working by adding it to my query but I think a handler is the proper way to do this?

‘http://www.bing.com/local/GetMap.ashx?ppl=34,,’ + geocodeLatitude + ‘,’ + geocodeLongitude + ‘&b=r,stl.h,mkt.en-us&rf=o&rp=n&z=16&c=’ + geocodeLatitude + ‘,’ + geocodeLongitude + ‘&w=659&h=602’ AS url

Hi Ryan,

Thanks for this additional information.

It sounds like including those varaibles in your query before merging or during merging are both good methods to achieve what you are looking for. If it is working as expected right now then it should be fine.

Regarding your question about the SaleTypeTxt field. You need this field in to signal to the merging engine if the image is to be inserted or not. However if you don’t want it to appear in the generated document, you can remove it during merging. Please see the sample code below which builds off the previous code.

Private Class HandleMergeImageFieldFromBlobListingSummary Implements IFieldMergingCallback

Dim mIsMergeImage As Boolean = False

Private Sub IFieldMergingCallback_FieldMerging(ByVal args As FieldMergingArgs) Implements IFieldMergingCallback.FieldMerging

' This field is SaleTypeTxt

If (args.FieldName = "SaleTypeTxt") Then

' If this is the correct FieldValue then merge the image

If (args.FieldValue = "Hart Exclusive Agency") Then

mIsMergeImage = True

Else

' Don't merge the image

mIsMergeImage = False

End If


' Set text of the field to empty

args.Text = ""

Dim para As Paragraph = args.Field.Start.ParentParagraph


' If the paragraph where this field is located is empty then remove it

If String.IsNullOrEmpty(para.ToTxt()) Then

para.Remove()

End If


End If

End Sub


Private Sub ImageFieldMerging(ByVal e As ImageFieldMergingArgs) Implements IFieldMergingCallback.ImageFieldMerging


' Check should merge image

If (mIsMergeImage) Then

e.ImageFileName = System.IO.Path.Combine("d:\hart11\images\", "hartlogoxsmall.gif")

End If


End Sub

End Class

If you have any further queries, please feel free to ask.

Thanks,

Getting an error when I try and load an image from the query. I attached my template which has an imagefield named profile… The file does exist but not at the path aspose is looking for it at…

Dim Query As String = “SELECT ROW_NUMBER() OVER(ORDER BY ViewContact.ContactID ASC) AS ‘Record’, (SELECT Count(ContactID) FROM ViewContact INNER JOIN ExportDetail ON ExportDetail.ExportPrimaryRecordID = ViewContact.ContactID WHERE ExportDetail.ExportID=’” & Request.QueryString(“ID”) & “’) AS RecordCount, ViewContact.ContactID, ‘http://www.bing.com/local/GetMap.ashx?ppl=34,,’ + geocodeLatitude + ‘,’ + geocodeLongitude + ‘&b=r,stl.h,mkt.en-us&rf=o&rp=n&z=12&c=’ + geocodeLatitude + ‘,’ + geocodeLongitude + ‘&w=720&h=300’ AS url, **CASE WHEN ImageURL > ‘’ THEN ‘http://connect.hartcorp.com/hart11/’ + ImageURL ELSE NULL END AS profile**, ContactProfileMemo FROM ViewContact LEFT OUTER JOIN ContactGeoCode ON ContactGeoCode.ContactID = ViewContact.ContactID INNER JOIN ExportDetail ON ExportDetail.ExportPrimaryRecordID = ViewContact.ContactID WHERE ExportDetail.ExportID=’” & Request.QueryString(“ID”) & “’”

If qExportTemplateID = “24” OR qExportTemplateID = “4” Then

Dim doc As New Document(“d:\hart11\templates\contactbasicprofile.doc”)

Dim adapterTemplate1 As New SQLDataAdapter(Query, myConnection)

Dim dtTemplate1 As New DataTable()

adapterTemplate1.Fill(dtTemplate1)

Dim recordCount As Integer

recordCount = dtTemplate1.Rows.Count

doc.MailMerge.Execute(dtTemplate1)

If qExportTemplateID = “4” Then 'Save As PDF

doc.Save(REPLACE(SavePath,“doc”,“pdf”))

Else

doc.Save(SavePath)

End If

Error:

Cannot load image from field ‘profile’. The field contains data in unsupported format. Could not find a part of the path ‘c:\windows\system32\inetsrv\filecabinet\Contact\180492\danbotts.jpg’.

Hi

Thanks for your inquiry. Please make sure that the image exists in the specified location. Also, make sure that image is readable and is valid.

Best regards,

I got this working by using the physical path instead of web address. How can I make sure the image appears in word as a fixed size? Some images are huge when the merge is done, vs others which are to small.

CASE WHEN PropertyImageURL > ‘’ THEN ‘D:\hart11’ + REPLACE(REPLACE(PropertyImageURL,’/’,’’),‘Properties’,‘Property’) ELSE NULL END AS profile

Hi Ryan,

You can code like this below to resize your images during mail merge.

Private Sub ImageFieldMerging(ByVal e As ImageFieldMergingArgs) Implements IFieldMergingCallback.ImageFieldMerging


' Check should merge image

If (mIsMergeImage) Then

Dim image As Image = Image.FromFile(CStr(e.FieldValue))

image = image.GetThumbnailImage(400, 300, Nothing, IntPtr.Zero)


e.Image = image

End If

If you have any further queries, please feel free to ask.

Thanks,

One thing I noticed is I get a nasty error if for some reason the image cannot be located. Is there anyway to test if the image exists before merging and skip it, or load a default image if for some reason the image cannot be opened or doesnt exist?

Thanks for your request.

Sure, please look into the File.Exists method detailed here. Including this within the logic for ImageMerging should help achieve what you are looking for.

Thanks

How do I specify what image merge field to use when inserting an image. I have my map image merge field but when I add the following, it replaces the map merge field as well. Can I specify which merge field should be used for the image?

Private Class HandleMergeFieldProfile1

Implements IFieldMergingCallback

Dim mIsMergeImage As Boolean = False

Private Sub FieldMerging(args As FieldMergingArgs) Implements IFieldMergingCallback.FieldMerging

'If args.FieldName = “SaleTypeTxt” Then

'If args.FieldValue = “Hart Exclusive Agency” Then

mIsMergeImage = True

’ Else

’ mIsMergeImage = False

'End If

'End If

End Sub

Private Sub ImageFieldMerging(e As ImageFieldMergingArgs) Implements IFieldMergingCallback.ImageFieldMerging

If (mIsMergeImage) Then

e.ImageFileName = System.IO.Path.Combine(“d:\hart11\images”, “hartlogoxsmall.gif”)

End If

End Sub

End Class

I moved to using the documentbuilder insert image method. Is this ok to do? It seems to be inserting the images how I want now and only affects that one field which is what I want. I still need to figure out resizing as not all images scale properly.

Private Class HandleMergeFieldProfile1

Implements IFieldMergingCallback

Private Sub FieldMerging(args As FieldMergingArgs) Implements IFieldMergingCallback.FieldMerging

Dim builder As New DocumentBuilder(args.Document)

If args.FieldName = “P1” Then

Dim Profile1s As String = args.FieldValue.ToString()

Dim img As System.Drawing.Image

builder.MoveToField(args.Field, True)

img = System.Drawing.Image.FromFile(Profile1s)

builder.InsertImage(img, 275, 125)

End If

End Sub

Private Sub ImageFieldMerging(e As ImageFieldMergingArgs) Implements IFieldMergingCallback.ImageFieldMerging

’ Do nothing.

End Sub

End Class