• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

User-defined type not defined

Hello Friends,

I am trying to copy a range of cells and paste it in lotus notes email. I am using the code listed in URL http://www.rondebruin.nl/win/s1/notes/notes5.htm . I am using excel 2010 version..

I get the above error for the line "Dim Data As DataObject". Please help me out..

Second part is that in the active sheet I have a picture inserted "Picture 1" and I want that picture to be pasted in the email.

I am using the below vba code
Code:
Public FolderName As String
Public WhereToSave As String
Public FilterSheet As Worksheet, OutPutSheet As Worksheet, OutPutWorkbook As Workbook, FilterWorkbook As Workbook, RowsOfDataSelected As Long
 
Public Sub FilterResultsAndSaveAsJPEG()
 
Dim TempFilePath  As String
TempFilePath = Environ$("temp") & "\"
Dim TempFileName As String
Dim FileExtStr As String
 
Set FilterSheet = ActiveSheet
Set FilterWorkbook = ThisWorkbook
 
Workbooks.Add
 
Set OutPutWorkbook = ActiveWorkbook
Set OutPutSheet = ActiveSheet
 
FilterSheet.Range("S4").CurrentRegion.Copy
 
Range("A1").Select
With Selection
    .PasteSpecial Paste:=xlPasteFormats
    .PasteSpecial Paste:=xlPasteColumnWidths
    .PasteSpecial Paste:=xlPasteValues
    .Font.Size = 9
    .Name = "Calibri"
End With
 
Application.CutCopyMode = False
 
With OutPutSheet
    .Activate
    ActiveWindow.Zoom = 74
    RowsOfDataSelected = .Range("E4", .Range("E10000").End(xlUp)).Rows.Count
    .Range("E4", .Range("E10000").End(xlUp)).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range( _
        "K1"), Unique:=True
 
End With
 
Dim HowManyIndustries As Excel.Range, Cell As Variant, FilterRange As Excel.Range, FieldNum As Integer, rng As Range
Dim NewWs As Worksheet
Dim noSession As Object, noDatabase As Object, noDocument As Object
Dim vaRecipient As Variant
Dim rnBody As Range
Dim Data As DataObject
 
Set HowManyIndustries = OutPutSheet.Range("K2", Range("K2").End(xlDown))
Set FilterRange = OutPutSheet.Range("A4:I" & (RowsOfDataSelected + 5))
FieldNum = 5
 
For Each Cell In HowManyIndustries
   
    OutPutSheet.Activate
    FilterRange.AutoFilter Field:=FieldNum, Criteria1:=Cell.Value
   
    With Selection
        On Error Resume Next
        Set rng = .SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
    End With
   
    Set NewWs = Sheets.Add
    NewWs.Name = Cell.Value
   
    rng.Copy
    With Selection
        .PasteSpecial Paste:=xlPasteFormats
        .PasteSpecial Paste:=xlPasteColumnWidths
        .PasteSpecial Paste:=xlPasteValues
        .Font.Size = 9
        .Name = "Calibri"
        On Error Resume Next
        Set rnBody = Selection
        Application.CutCopyMode = False
        ActiveWindow.Zoom = 74
    End With
   
'    FolderName = Format(Now, "yyyy-mm-dd h-mm-ss")
'    If Len(Dir(TempFilePath, vbDirectory)) = 0 Then
'      MkDir TempFilePath
'    End If
'
'    If Len(Dir(TempFilePath & FolderName, vbDirectory)) = 0 Then
'      MkDir TempFilePath & FolderName
'    End If
'
'    WhereToSave = TempFilePath & FolderName
 
  Const stSubject As String = "LS Enterprise LT$10M signings optimization"
  Dim stMsg As String
 
  stMsg = FilterSheet.Range("AC5").Value
  'This is one technique to send an e-mail to many recipients but for larger
  'number of recipients it's more convenient to read the recipient-list from
  'a range in the workbook.
  vaRecipient = FilterSheet.Range("AC3").Value
  On Error GoTo 0
  'Instantiate Lotus Notes COM's objects.
  Set noSession = CreateObject("Notes.NotesSession")
  Set noDatabase = noSession.GETDATABASE("", "")
  'Make sure Lotus Notes is open and available.
  If noDatabase.IsOpen = False Then noDatabase.OPENMAIL
  'Create the document for the e-mail.
  Set noDocument = noDatabase.CreateDocument
  'Copy the selected range into memory.
  rnBody.Copy
  'Retrieve the data from then copied range.
  Set Data = New DataObject
  Data.GetFromClipboard
  'Add data to the mainproperties of the e-mail's document.
  With noDocument
      .Form = "Memo"
      .SendTo = vaRecipient
      .Subject = stSubject
      'Retrieve the data from the clipboard.
      .Body = Data.GetText & " " & stMsg
      .SaveMessageOnSend = True
  End With
  'Send the e-mail.
  With noDocument
      .PostedDate = Now()
      .Send 0, vaRecipient
  End With
  'Release objects from memory.
  Set noDocument = Nothing
  Set noDatabase = Nothing
  Set noSession = Nothing
  'Activate Excel for the user.
  AppActivate "Microsoft Excel"
  'Empty the clipboard.
  Application.CutCopyMode = False
   
Next Cell
 
End Sub
 

Attachments

  • Test .xlsm
    395.9 KB · Views: 1
For working with Windows Clipboard you need DataObject, the object in MSFormslibrary. It provides support for text-string.
For that you must add the reference“Microsoft Forms 2.0 Object Library”
 
Hello Deepak,

I was not able to locate the Microsoft Forms 2.0 Object Library in my system so I asked you for the late binding technique. I have not tested your code but, I inserted a dummy user form in the vba project which resolved my issue.

I have used alternative code to fix the issue.
Code:
Public FolderName As String
Const EMBED_ATTACHMENT As Long = 1454
Public WhereToSave As String
Public FilterSheet As Worksheet, OutPutSheet As Worksheet, OutPutWorkbook As Workbook, FilterWorkbook As Workbook, RowsOfDataSelected As Long
 
Public Sub FilterResultsAndSaveAsJPEG()
 
Dim TempFilePath  As String
TempFilePath = Environ$("temp") & "\"
Dim TempFileName As String
 
'Setting the active sheet as the raw data sheet which will be used for filtering the data
Set FilterSheet = ActiveSheet
Set FilterWorkbook = ThisWorkbook
 
Application.ScreenUpdating = False
 
'Adding the temporary workbook to build the range which has to be pasted in the email trail
Workbooks.Add
 
'Setting the name for the temporary workbook
Set OutPutWorkbook = ActiveWorkbook
Set OutPutSheet = ActiveSheet
 
'Copying the raw data from the source file
FilterSheet.Range("S4").CurrentRegion.Copy
 
'pasting in the temporary file to build the range
Range("A1").Select
With Selection
    .PasteSpecial Paste:=xlPasteFormats
    .PasteSpecial Paste:=xlPasteColumnWidths
    .PasteSpecial Paste:=xlPasteValues
    .Font.Size = 9
    .Name = "Calibri"
End With
 
Application.CutCopyMode = False
 
'categorizing the data into several groups, each group will have the data in separate sheet
With OutPutSheet
    .Activate
    ActiveWindow.Zoom = 74
    .Range("A4").End(xlToRight).Offset(, 1).Value = "Group"  'Applying header to the new column
   
    Dim GroupCell As Variant, GroupSelection As Excel.Range
    Set GroupSelection = .Range("J5", .Range("E10000").End(xlUp).Offset(, 5))
   
    For Each GroupCell In GroupSelection
       
        GroupCell.Value = IndustyGroupingName(GroupCell.Offset(, -5))
   
    Next GroupCell
   
    RowsOfDataSelected = .Range("E4", .Range("E10000").End(xlUp)).Rows.Count
    .Range("J4", .Range("J10000").End(xlUp)).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range( _
        "L1"), Unique:=True
 
End With
 
Dim HowManyIndustries As Excel.Range, Cell As Variant, FilterRange As Excel.Range, FieldNum As Integer, rng As Range
Dim NewWs As Worksheet
Dim NSession As Object
Dim NDatabase As Object
Dim NUIWorkSpace As Object
Dim NDoc As Object
Dim NUIdoc As Object
Dim WordApp As Object
Dim WordDoc As Object
Dim vaRecipient As Variant
Dim rnBody As Range
Dim noEmbedObject As Object
Dim noAttachment As Object
Dim stAttachment As String
 
Set HowManyIndustries = OutPutSheet.Range("L2", Range("L100").End(xlUp))
Set FilterRange = OutPutSheet.Range("A4:J" & (RowsOfDataSelected + 5))
FieldNum = 10
 
For Each Cell In HowManyIndustries
   
    OutPutSheet.Activate
    FilterRange.AutoFilter Field:=FieldNum, Criteria1:=Cell.Value
   
    With Selection
        On Error Resume Next
        Set rng = .SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
    End With
   
    Set NewWs = Sheets.Add
      NewWs.Name = Cell.Value
   
    rng.Copy
    Dim AnotherTempWrokbook As Workbook
    Set AnotherTempWrokbook = ActiveWorkbook
    With Selection
        .PasteSpecial Paste:=xlPasteFormats
        .PasteSpecial Paste:=xlPasteColumnWidths
        .PasteSpecial Paste:=xlPasteValues
        .Font.Size = 9
        .Name = "Calibri"
        On Error Resume Next
        Set rnBody = Selection
        Application.CutCopyMode = False
        ActiveWindow.DisplayGridlines = False
        ActiveWindow.Zoom = 74
    End With
   
'    FolderName = Format(Now, "yyyy-mm-dd h-mm-ss")
'    If Len(Dir(TempFilePath, vbDirectory)) = 0 Then
'      MkDir TempFilePath
'    End If
'
'    If Len(Dir(TempFilePath & FolderName, vbDirectory)) = 0 Then
'      MkDir TempFilePath & FolderName
'    End If
'
'    WhereToSave = TempFilePath & FolderName
'    'Copy the active sheet to a new temporarily workbook.
'    With ActiveSheet
'      .Copy
'      TempFileName = Cell.Value
'    End With
   
    stAttachment = ThisWorkbook.FullName
   
'    'Save and close the temporarily workbook.
'    With ActiveWorkbook
'      .SaveAs stAttachment
'      .Close
'    End With
 
  Const stSubject As String = "LS Enterprise LT$10M signings optimization"
  Dim stMsg As String
 
  stMsg = Sheet13.Range("BodyOfEmail").Value
 
  Select Case Cell.Value
        Case "CS _ CP _ WD&S _ PS"
        vaRecipient = Sheet13.Range("B4").Value
       
        Case "Life Sciences"
        vaRecipient = Sheet13.Range("B5").Value
       
        Case "Retail"
        vaRecipient = Sheet13.Range("B6").Value
       
        Case "Travel and Transportation"
        vaRecipient = Sheet13.Range("B7").Value
       
        Case "C&P _ Industrial Products"
        vaRecipient = Sheet13.Range("B8").Value
       
        Case "Electronics"
        vaRecipient = Sheet13.Range("B9").Value
       
        Case "Automotive _ A&D"
        vaRecipient = Sheet13.Range("B10").Value
   
        Case "Energy & Utilities"
        vaRecipient = Sheet13.Range("B11").Value
       
        Case "Media & Entertainment"
        vaRecipient = Sheet13.Range("B12").Value
       
        Case "Telecommunications"
        vaRecipient = Sheet13.Range("B13").Value
       
        Case "Insurance"
        vaRecipient = Sheet13.Range("B14").Value
       
        Case "Financial Markets _ Banking"
        vaRecipient = Sheet13.Range("B15").Value
           
        Case "Government _ Education"
        vaRecipient = Sheet13.Range("B16").Value
       
        Case "Health"
        vaRecipient = Sheet13.Range("B17").Value
       
  End Select
 
  On Error GoTo 0
  'Instantiate Lotus Notes COM's objects.
  Set NSession = CreateObject("Notes.NotesSession")
  Set NUIWorkSpace = CreateObject("Notes.NotesUIWorkspace")
  Set NDatabase = NSession.GETDATABASE("", "")
 
  'Make sure Lotus Notes is open and available.
  If NDatabase.IsOpen = False Then NDatabase.OPENMAIL
 
  'Create the document for the e-mail.
  Set NDoc = NDatabase.CreateDocument
 
  Set noAttachment = NDoc.CreateRichTextItem("stAttachment")
  Set noEmbedObject = noAttachment.EmbedObject(EMBED_ATTACHMENT, "", stAttachment)
  'Add data to the mainproperties of the e-mail's document.
  With NDoc
      .SendTo = vaRecipient
      .CopyTo = ""
      .Subject = stSubject
      .Body = "Hello, " & vbNewLine & vbNewLine & stMsg & vbNewLine & vbNewLine & "**PASTE EXCEL CELLS HERE**" & vbNewLine & "**PASTE Chart HERE**"
      .Save True, False
  End With
 
    'Edit the just-created document to copy and paste the Excel cells into it
   
    Set NUIdoc = NUIWorkSpace.EDITDocument(True, NDoc)
    With NUIdoc
        'Find the marker text in the Body item
 
        .GotoField ("Body")
        .FINDSTRING "**PASTE EXCEL CELLS HERE**"
        '.DESELECTALL 'Uncomment to leave the marker text in place (cells are inserted immediately before)
 
        'Replace it with the Excel cells
        rnBody.Copy
       
            'Create a temporary Word Document
       
            Set WordApp = CreateObject("Word.Application")
            WordApp.Visible = False                                'True to aid debugging
            WordApp.Documents.Add
           
            'Paste into Word document and copy to clipboard
           
            With WordApp.Selection
                .PasteSpecial DataType:=1      'Enum WdPasteDataType: 10 = HTML; 2 = Text; 1 = RTF
                .WholeStory
                .Copy
            End With
 
        'Paste from clipboard (Word) to Lotus Notes document
           
        .Paste
       
        'Activate Excel for the user.
        AppActivate "Microsoft Excel"
        FilterWorkbook.Activate
       
        FilterSheet.Shapes.Range(Array("Picture 1")).Select
       
        Selection.Copy
       
        .FINDSTRING "**PASTE Chart HERE**"
       
        .Paste
       
        Application.CutCopyMode = False
 
        WordApp.Quit SaveChanges:=False
        Set WordApp = Nothing
 
        '.Send
        '.Close
    End With
   
    Set NSession = Nothing
    Set NDatabase = Nothing
    Set NUIWorkSpace = Nothing
    Set NDoc = Nothing
    Set NUIdoc = Nothing
   
Next Cell
Application.ScreenUpdating = True
 
OutPutWorkbook.Close SaveChanges:=False
'AnotherTempWrokbook.Close SaveChanges:=False
 
MsgBox "Emails have been drafted accordingly."
 
End Sub
 
Public Function IndustyGroupingName(IndName As Range) As String
 
Select Case IndName.Value
    Case "Computer Services", "Consumer Products", "Wholesale Distribution & Services", "Professional Services"
    IndustyGroupingName = "CS _ CP _ WD&S _ PS"
 
    Case "Life Sciences"
    IndustyGroupingName = "Life Sciences"
   
    Case "Retail"
    IndustyGroupingName = "Retail"
   
    Case "Travel and Transportation"
    IndustyGroupingName = "Travel and Transportation"
   
    Case "Chemicals & Petroleum", "Industrial Products"
    IndustyGroupingName = "C&P _ Industrial Products"
   
    Case "Electronics"
    IndustyGroupingName = "Electronics"
   
    Case "Automotive", "Aerospace & Defense"
    IndustyGroupingName = "Automotive _ A&D"
 
    Case "Energy & Utilities"
    IndustyGroupingName = "Energy & Utilities"
   
    Case "Media & Entertainment"
    IndustyGroupingName = "Media & Entertainment"
   
    Case "Telecommunications"
    IndustyGroupingName = "Telecommunications"
   
    Case "Insurance"
    IndustyGroupingName = "Insurance"
   
    Case "Finance Markets", "Banking"
    IndustyGroupingName = "Financial Markets _ Banking"
       
    Case "Government,State/Provincial/Local", "Education"
    IndustyGroupingName = "Government _ Education"
   
    Case "Health"
    IndustyGroupingName = "Health"
   
    Case Else
    IndustyGroupingName = Empty
   
End Select
End Function
 
Back
Top