I wrote a VBA code to convert TIF to JPG, but it does not work. I get run time error 5 when I run my code. Can someone help me? I'm using Microstation v8i. My code is below one.
RasterManager.ExportRasterFile sourcefilename:=fpath & "\" & fname, destinationfilename:=fpath & "\" & fso.GetBaseName(fname) & ".jpg", NewFormatSpecifier:=RasterManager.RasterFormats.Find("JPEG(J)")
hirotaka nisimura said:I get run time error 5
Add some Debug statements to help find the problem.
Debug
Const Backslash As String = "\"
Dim source As String source = fpath & Backslash & fName Debug.Print "Source=" & source
Dim raster As String raster = fpath & Backslash & fso.GetBaseName(fname) & ".jpg" Debug.Print "Raster=" & raster
The Debug.Print statement prints to the VBA Immediate window. Are those valid file names?
Debug.Print
The RasterFormats class is not well-documented. It's not clear whether JPEG(J) is a valid search criterion. The sole example I can find is...
RasterFormats
JPEG(J)
Set oFormat = RasterManager.RasterFormats.Find("TI~FF")
This always fails...
Sub TestRaster() Dim rm As RasterManager Set rm = RasterManager Dim oFormat As raster Dim formatName As String formatName = "JPEG(J)" Set oFormat = rm.RasterFormats.Find(formatName) If oFormat Is Nothing Then Debug.Print "Invalid format name " & formatName Else Debug.Print "Format name " & formatName & " is valid" End If End Sub
However, "JPEG" is valid.
Here's how to list all raster formats...
Sub TraceRasterFormats() Dim oFormat As RasterFormat Dim oFormats As RasterFormats Dim nFormat As Long nFormat = 1 Dim msg As String Set oFormats = RasterManager.RasterFormats For Each oFormat In oFormats msg = "Raster Format [" & CStr(nFormat) & "] = '" & oFormat.ToString() & "'" ShowMessage msg, msg, msdMessageCenterPriorityDebug Debug.Print msg nFormat = 1 + nFormat Next End Sub
Regards, Jon Summers LA Solutions
Thank you Jon. I checked the filename. In the case that I set 'JPEG' for Raster, the new file was saved. But it was TIFF, not JPEG format.
And I chekced RasterFormats class using your code. As the result, The string of RasterFormat for JPEG is 'JPEG(J)' so I tried again. But it don't work.