I am using API calls but the errors do not derive from the module that contains them. It seems that (at least some of) the errors are stemming from the following subroutine:
Sub LoadImage(ByVal imgType$, imgname$, ByRef uForm As UserForm, Optional ByVal ctlName$)
On Error GoTo errorhandler
If ctlName = "" Then ctlName = imgname
Dim addr$, ratio#, PicHeight#, PicWidth#
addr = ThisWorkbook.Path & "\" & "~TempImg." & imgType
If FileExists(addr) Then Kill addr
PicHeight = uForm.Controls.Item(ctlName).Height
PicWidth = uForm.Controls.Item(ctlName).Width
With ThisWorkbook.Sheets("Images")
.Shapes(imgname).Select
ratio = PicHeight / .Shapes(imgname).Height
.Shapes(imgname).Height = PicHeight
.Shapes(imgname).Width = .Shapes(imgname).Width * ratio
.ChartObjects(imgname).Chart.Export Filename:=addr, filtername:=imgType
End With
With uForm.Controls.Item(ctlName)
.Picture = LoadPicture(addr)
If InStr(ctlName, "Start") Or InStr(ctlName, "Stop") Then
.Visible = False
Else
.Visible = True
End If
End With
Kill addr
ThisWorkbook.Sheets("Images").Visible = xlSheetVeryHidden
Exit Sub
errorhandler:
ErrorTrap "#070001", Err
End Sub
Essentially (all the fiddly details aside) the routine takes an image control in a userform and loads a picture to it which is saved in a worksheet, embedded in a chart frame; the chart frame is first re-sized to match the dimensions of the control (this had to be done to accommodate different local resolution settings). The image in the worksheet is then exported to a static image file ("~TempImg.xxx"), which is loaded to the control and then the temporary file is deleted. All pretty standard and straight forward and not a problem in Excel 2010 and 2013 but it 2007 it falls down.
It seems to fall down specifically when it is being called multiple times in quick succession - the same routine is used without a problem to load images at other points in the code. My suspicion is that the code is running faster than the excel 2007 system can export the image file, load it and delete it. I have tried using a sleep procedure to add a pause in the code to allow the system to "catch up", I have tried adding DoEvents (which, incidentally, I have never found to make a difference in any scenario) but to no avail.
The most common errors are 70 (permission denied) and 75 (path/file access error), which I think lends weight to my suspicion. Any thoughts?
Bookmarks