Function in class library throwing run-time error '91'

mwinnow

New member
Joined
Jun 10, 2022
Messages
1
Programming Experience
1-3
Hi guys, I am trying to get the thumbnails of .dwg's, in order to show them in a file organizer I'm making in VBA Forms. From what I read, this isn't possible from VBA directly, but they are accessible by using VB.NET. What I did was take some code from another forum and attempt to integrate that with my code. I have a class library written in VB.NET Framework. It is COM visible, registered with regasm, and referenced in the VBA Forms code. Here is the VB.NET class Library:

BitmapV3:
Imports System.IO
Imports System.Drawing

' 2011 Copyright (C) jgr=&jgr, via http://www.theswamp.org
' 2012 (me): Added code to read PNG Thumbnails from DWG (2013 file format)


'Friend NotInheritable Class ThumbnailReader
Public Class ThumbnailReader

    Public Sub New()
    End Sub
    'Friend Shared Function GetBitmap(fileName As String) As Bitmap

    Public Function GetBitmap(fileName As String) As Bitmap
        Using fs As New FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
            Using br As New BinaryReader(fs)
                fs.Seek(&HD, SeekOrigin.Begin)
                fs.Seek(&H14 + br.ReadInt32(), SeekOrigin.Begin)
                Dim bytCnt As Byte = br.ReadByte()
                If bytCnt <= 1 Then
                    Return Nothing
                End If
                Dim imageHeaderStart As Integer
                Dim imageHeaderSize As Integer
                Dim imageCode As Byte
                For i As Short = 1 To bytCnt
                    imageCode = br.ReadByte()
                    imageHeaderStart = br.ReadInt32()
                    imageHeaderSize = br.ReadInt32()
                    If imageCode = 2 Then ' BMP Preview (2012 file format)
                        ' BITMAPINFOHEADER (40 bytes)
                        br.ReadBytes(&HE)
                        'biSize, biWidth, biHeight, biPlanes
                        Dim biBitCount As UShort = br.ReadUInt16()
                        br.ReadBytes(4)
                        'biCompression
                        Dim biSizeImage As UInteger = br.ReadUInt32()
                        'br.ReadBytes(0x10); //biXPelsPerMeter, biYPelsPerMeter, biClrUsed, biClrImportant
                        '-----------------------------------------------------
                        fs.Seek(imageHeaderStart, SeekOrigin.Begin)
                        Dim bitmapBuffer As Byte() = br.ReadBytes(imageHeaderSize)
                        Dim colorTableSize As UInteger = CUInt(Math.Truncate(If((biBitCount < 9), 4 * Math.Pow(2, biBitCount), 0)))
                        Using ms As New MemoryStream()
                            Using bw As New BinaryWriter(ms)
                                bw.Write(CUShort(&H4D42))
                                bw.Write(54UI + colorTableSize + biSizeImage)
                                bw.Write(New UShort())
                                bw.Write(New UShort())
                                bw.Write(54UI + colorTableSize)
                                bw.Write(bitmapBuffer)
                                Return New Bitmap(ms)
                            End Using
                        End Using
                    ElseIf imageCode = 6 Then ' PNG Preview (2013 file format)
                        fs.Seek(imageHeaderStart, SeekOrigin.Begin)
                        Using ms As New MemoryStream
                            fs.CopyTo(ms, imageHeaderStart)
                            Dim img = Image.FromStream(ms)
                            Return img
                        End Using
                    ElseIf imageCode = 3 Then
                        Return Nothing
                    End If
                Next
            End Using
        End Using
        Return Nothing
    End Function
End Class

Note that this code is slightly changed from the original - the commented function and class declarations (highlighted) are the originals, but these allowed no outside access, so I changed them.

I then attempt to reference that library in my VBA Forms. Because that code was not working when I tried to reference the "GetBitmap" function, I temporarily added another public function into the class, and that little function worked fine (I removed it after briefly testing). Here is the VBA code I'm using to attempt to access the class library above:

VB.NET:
Private Sub Get_Preview()

    Dim fileName As String
    fileName = "drawingFile.dwg"
   
    Dim thumbnailObject As BitmapV3.ThumbnailReader
    Set thumbnailObject = New BitmapV3.ThumbnailReader
   
    Dim thumbnailImage As image
    thumbnailImage = thumbnailObject.GetBitmap(fileName)
   
End Sub

On line 10, this code throws "Error '91': Object variable or With block variable not set. " The research I've done suggests that that error means I'm referencing an object that doesn't exist, but my Locals window shows thumbnailObject as an object as type ThumbnailReader, which is as expected.

I'm an absolute beginner in both VBA and VB.NET, so I apologize if it's something obvious. I did go through a whole bunch of forums before posting my own question. Thank you for taking the time to look, and any assistance would be greatly appreciated.
 
Last edited:
You should add some exception handling and logging to your .NET code. That will allow you to determine what's going on insider that code. If that code is doing what it's supposed to then this isn't a VB.NET issue. If it's not then you need to find out what it is doing to be able to address it.
 
Back
Top