0

I'm trying to create a DLL file that will help me to access and read PDF files to Excel. As can be seen in the attached photo I've added the .tlb file to the reference library in Excel but I'm not able to run it without an error (Error 438).

I've created a Main Method in VisualStudio to test PdfExtractor class with good results. So my question is how do I initialize an object from my DLL file and pass arguments to it?

VBA References

My VBA code:

Option Explicit

Sub PdfToExcel()

Dim vbObj As New PdfParser.PdfExtractor
Dim words As String

With vbObj
    'Error 438
    .PdfExtractor ("C:\Users\user\Downloads\54.pdf")
    words = .readPDF
End With

End Sub

I have selected "Make assembly COM-Visible" and "Register for COM interop". My C# code (VB - Class Library (.NET Framework):

using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using System;
using System.Runtime.InteropServices;
using System.Text;


namespace PdfParser
{
    [ComVisible(true)]

    public class PdfExtractor
    {
        private string url = string.Empty;
        private string path = string.Empty;
        private string getPageFromPdf = string.Empty;
        private PdfReader pdfReader;

        public PdfExtractor(string address)
        {
            try
            {
                if (address.Contains("http")){
                    this.url = address;
                    this.pdfReader = new PdfReader(new Uri(url));
                }
                else
                {
                    this.path = address;
                    this.pdfReader = new PdfReader(path);
                }
            }
            catch
            {

            }
        }
        public PdfExtractor(){}

        [ComVisible(true)]
        public string Url { get { return url; } set { url = value; } }
        [ComVisible(true)]
        public string Path { get { return path; } set { path = value; } }

        [ComVisible(true)]
        public void setPdfReader()
        {
            if (path != string.Empty) this.pdfReader = new PdfReader(path); else this.pdfReader = new PdfReader(new Uri(url));
        }

        [ComVisible(true)]
        public string readPDF()
        {
            return parsePage();
        }
        private string parsePage()
        {
            try
            {
                for (int i = 1; i <= pdfReader.NumberOfPages; i++)
                {
                    ITextExtractionStrategy its = new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy();
                    string stringToConvert = iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(pdfReader, i, its);

                    stringToConvert = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(stringToConvert)));
                    getPageFromPdf = getPageFromPdf + stringToConvert;
                }
                pdfReader.Close();
                return getPageFromPdf;
            }
            catch (Exception e)
            {
                return e.Message;
            }

        }
    }
}
  • Looks like you have declared PdfExtractor(string... as s constructor which is not valid for VBA. Might be better to change that to 'public void Init(string... – freeflow Apr 04 '22 at 22:38
  • 1
    I haven't written a COM DLL in a while but I'm pretty sure you're missing some of the attributes, such as the interface GUIDs. See [link](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/interop/example-com-class) – John Wu Apr 04 '22 at 23:03
  • Be aware it may be expecting either 32 or 64 bit and you have the wrong bitness: https://stackoverflow.com/a/3534693. It also helps to search Google on the exact error message you are getting because someone almost certainly had the same problem and solved it already. – NightOwl888 Apr 05 '22 at 06:45
  • @JohnWu Do you know where I can retrieve the GUID id? –  Apr 05 '22 at 08:32
  • @Banke I don't think you retrieve them, you generate them (e.g. use a [tool](https://www.guidgenerator.com/)) and add them to your classes and interfaces. – John Wu Apr 05 '22 at 12:16

0 Answers0