+ Reply to Thread
Results 1 to 8 of 8

Creating email attachment out of excel - so close yet so far...

Hybrid View

  1. #1
    Registered User
    Join Date
    10-10-2013
    Location
    Amsterdam, the Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    4

    Creating email attachment out of excel - so close yet so far...

    Hi,

    I have the following sub which creates an email in Lotus Notes using the output of my query in excel.

    Sub Lotus()
    
            vaRecipients = VBA.Array("My Client")
            stSubject = "CREST ETI " & Date
    
            ActiveSheet.Copy
    
            ActiveSheet.Shapes("CommandButton1").Select
            Selection.Delete
            ActiveSheet.Shapes("CommandButton2").Select
            Selection.Delete
            Rows("1:4").Select
            Selection.Delete Shift:=xlToUp
            Columns("A:B").Select
            Selection.Delete Shift:=xlToLeft
            Range("A1:AC1", Selection.End(xlDown)).Select
            Selection.Copy
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    
            TempFilePath = Environ$("CREST ETI") & "\"
            TempFileName = ActiveWorkbook.Name
            FileExtStr = ".csv": FileFormatNum = 6
            
            ActiveWorkbook.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
            
            Set noSession = CreateObject("Notes.NotesSession")
            Set noDatabase = noSession.GETDATABASE("", "")
              
            If noDatabase.IsOpen = False Then noDatabase.OPENMAIL
            
            Set noDocument = noDatabase.CreateDocument
            Set noAttachment = noDocument.CreateRichTextItem(ActiveWorkbook.FullName)
            Set noEmbedObject = noAttachment.EmbedObject(1454, "", ActiveWorkbook.FullName)
            
            With noDocument
                  .Form = "Memo"
                  .SendTo = vaRecipients
                  .CopyTo = vaCopyTo
                  .Subject = stSubject
                  .PostedDate = Now()
            End With
            
            Set Workspace = CreateObject("Notes.NotesUIWorkspace")
            Call Workspace.EditDocument(True, noDocument)
               
            Dim UIdoc As Object
            Set UIdoc = Workspace.CURRENTDOCUMENT
            Call UIdoc.GotoField("Body")
            Call UIdoc.InsertText("Hello," & vbCrLf & vbCrLf & "Please find attached the CREST ETI file for" & Date & ". Thank you." & vbCrLf & vbCrLf)
        
            ActiveWorkbook.Close SaveChanges:=False
            Kill TempFilePath & TempFileName & FileExtStr
        
            MsgBox "You can now open Lotus and edit the mail"
            
    End Sub
    It works, the output is a .csv file with the range of rows that I have in excel except that it also includes the remaining 65000 empty lines as a string of commas (,,,,,,,,,,,,,,,,,,) underneath. I dont understand why since in the selection.copy I really only get the rows filled by my query.

    Anyone have an idea how to create a temp .csv file with only the rows that I select in the selection.copy part of the code? Any help will be greatly appreciated!

  2. #2
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Creating email attachment out of excel - so close yet so far...

    The following code may be causing this

    Range("A1:AC1", Selection.End(xlDown)).Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    The first range statement may not be doing what you expect. In particular the 'Selection.End(xlDown)' may yield strange results

    You could try the code below (note I have also set SkipBlanks to True as this only refers to empty cells at the end of the selection)

    Range("A1:AC1").Resize(ActiveSheet.UsedRange.Rows.Count).Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=True, Transpose:=False
    If you like my contribution click the star icon!

  3. #3
    Registered User
    Join Date
    10-10-2013
    Location
    Amsterdam, the Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Creating email attachment out of excel - so close yet so far...

    Hi OllieB,

    Thanks for the reply! I should clarify that I do not see a problem in the selection part of the code, I used your suggestion and the results do not vary, it really seems like I am missing something in the step between converting that selection into the .csv file.
    Attached Images Attached Images
    Last edited by malikazam; 10-10-2013 at 05:36 AM.

  4. #4
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Creating email attachment out of excel - so close yet so far...

    Another solution you could try is to copy & paste the range used onto a new empty worksheet and saving that new worksheet as a CSV. That would also save you the work of removing columns, rows etc, and I am pretty sure that will solve you problem altogether.

  5. #5
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Creating email attachment out of excel - so close yet so far...

    malikazam,

    under certain conditions Excels loses track of the last row actually used on a worksheet. You can check this by opening the worksheet manually, position the cursor on cell A1 and pressing {Ctrl}{Shift}{End} simultaneously. If all is well the selection shown should only encompass a range of cells that have values. If things are wrong it will select pretty much the entire worksheet. Can you check this for me.

  6. #6
    Registered User
    Join Date
    10-10-2013
    Location
    Amsterdam, the Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Creating email attachment out of excel - so close yet so far...

    Hi OllieB,

    {Ctrl}{Shift}{End} select A1:AE65536.

    Unfortunately, the user for this sheet is too lazy to agree with your alternate solution

  7. #7
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Creating email attachment out of excel - so close yet so far...

    You could try to use the below code to generate the CSV

    
    Public Sub tryUsingEmptyWorksheet()
    
    '#
    '# declare private variables
    '#
       Dim pvt_xls_Source As Excel.Worksheet
       Dim pvt_xls_Target As Excel.Worksheet
       
       Dim vaRecipients As Variant
       Dim stSubject As String
       Dim TempFilePath As String
       Dim TempFileName As String
       Dim FileExtStr As String
       
    '#
    '# initialise
    '#
       Set pvt_xls_Source = ActiveSheet
       Set pvt_xls_Target = ThisWorkbook.Worksheets.Add
       
       vaRecipients = VBA.Array("My Client")
       stSubject = "CREST ETI " & Date
       TempFilePath = Environ$("CREST ETI") & "\"
       TempFileName = ActiveWorkbook.Name
       FileExtStr = ".csv"
       
    '#
    '# copy the data to the newly created / pristine worksheet
    '#
       pvt_xls_Source.Range("C5:AE5", Selection.End(xlDown)).Select
       Selection.Copy
       pvt_xls_Target.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
       
    '#
    '# move the focus to the newly created worksheet and save the contents as a CSV file
    '#
       pvt_xls_Target.Activate
       ActiveWorkbook.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=xlCSV
            
    End Sub

  8. #8
    Registered User
    Join Date
    10-10-2013
    Location
    Amsterdam, the Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Creating email attachment out of excel - so close yet so far...

    Hi OllieB, once again many thanks for your help. I cannot seem to incorporate your code into my initial sub. In the meantime I have convinced my client to just copy and paste into a different sheet and save as .csv :D

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Export Email Attachment to Excel
    By dwhite30518 in forum Outlook Programming / VBA / Macros
    Replies: 0
    Last Post: 05-30-2012, 06:08 PM
  2. Creating email from excel list with attachment
    By Trassin in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-25-2011, 07:44 AM
  3. email excel file or worksheet as an attachment to multiple email addresses
    By jgeagle5 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-16-2009, 03:40 PM
  4. attachment - then email from excel!
    By dj_siek in forum Excel General
    Replies: 0
    Last Post: 02-14-2006, 10:10 PM
  5. [SOLVED] cant send email attachment from excel
    By DKT in forum Excel - New Users/Basics
    Replies: 8
    Last Post: 11-21-2005, 09:30 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1