Error while running Code to add reference library

  • Hii..

    I am trying to draft mail via outlook using VBA.

    since we need to add outlook library/ scripting library (for using file system object), I incorporated vba code to add libraries.

    Code is working fine if the it only have the lines to add library. In this case it is adding the required library.

    But when i add outlook object declaration to the code, it is skipping the code lines which adds libraries and throwing 'compiler error : user defined type not defined'

    If the libraries are already added this is running fine. In any case it is skipping the lines highlighted in red.

    Please help


    Option Explicit

    Sub DraftMail()

    On Error Resume Next

    Application.VBE.ActiveVBProject.References.AddFromFile "C:\Program Files\Microsoft Office\root\Office16\MSOUTL.OLB" ' adding outlook library

    Application.VBE.ActiveVBProject.References.AddFromFile "C:\Windows\System32\scrrun.dll"

    On Error GoTo 0

    Dim MyOutlook As Outlook.Application

    Dim DraftMail As Outlook.MailItem

    Set MyOutlook = New Outlook.Application

    Set DraftMail = MyOutlook.CreateItem(olMailItem)

    With DraftMail

    .BodyFormat = olFormatHTML


    .HTMLBody = "Dear ABC" & vbNewLine & .HTMLBody

    .To = "[email protected]"

    .Subject = "xyz"

    .Attachments.Add "ref"

    End With

    End Sub


  • Late Binding works for me. I'm not sure what "ref" is though?

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!