Ok, so I need to loop through the columns to calculate the total width.
But then, when I try to set the Left property of the Picture, I sometime receive this exception:
The left value must be less than the upper left column width.
I found that this error occurs when the first column is narrower then the left property.
So I have written that code to loop through the column width a second time to place the picture in the correct cell.
'------------------------------------------------------------------
'--- Center the image in the middle of the merge cells
'------------------------------------------------------------------
'Find the width (in pixels) of the merge cells
Dim intWidth As Integer = 0
For intI As Integer = 0 To 4
intWidth += objSheet.Cells.GetColumnWidthPixel(intI)
Next
'Calculate the LEFT position of the image
Dim objPicture As Picture = objSheet.Pictures(intPictureHeader)
Dim intLeft As Integer
If intWidth > objPicture.Width Then
intLeft = (intWidth - objPicture.Width) \ 2
Else
intLeft = 0
End If
'The Left property must abolutely be related to the UpperLeftColumn
For intI As Integer = 0 To 4
If objSheet.Cells.GetColumnWidthPixel(intI) > intLeft Then
objPicture.Left = intLeft
Exit For
Else
intLeft -= objSheet.Cells.GetColumnWidthPixel(intI)
objPicture.UpperLeftColumn += 1
End If
Next
Is there an easier way?