+ Reply to Thread
Results 1 to 7 of 7

Email Selection From Excel, No Selection Copied

  1. #1
    Registered User
    Join Date
    03-27-2014
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    4

    Email Selection From Excel, No Selection Copied

    Hi Guys,

    Hoping you can help as I'm really struggling with this.

    I'm trying to email a range from excel into the body of an email, all using Access VBA.

    I've used Ron De Bruin's excellent VBA code to do this, however, I keep getting the error that 'The selection is not a range or the sheet is protected".

    The sheet is not protected and stepping through the code I can see that the range is being selected (highlighted) so I'm stumped as to why I'm receiving this error.

    If I comment out the error capture code, the procedure continues to run through but nothing is pasted into the mail body in Outlook.

    Below is the code I'm using:

    Sub Mail_Selection_Range_Outlook_Body()
    'Working in Excel 2000-2013
    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object

    Set rng = Nothing
    On Error Resume Next
    'Only the visible cells in the selection
    'Set rng = Selection.SpecialCells(xlCellTypeVisible)
    'You can also use a fixed range if you want
    Set rng = excel.Sheets("Sheet1").Range("A1:F35").Select.SpecialCells(xlCellTypeVisible)
    On Error GoTo 0

    If rng Is Nothing Then
    MsgBox "The selection is not a range or the sheet is protected" & _
    vbNewLine & "please correct and try again.", vbOKOnly
    Exit Sub
    End If

    With excel.Application
    .EnableEvents = False
    .ScreenUpdating = False
    End With

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
    .To = "me@me.com"
    .CC = ""
    .BCC = ""
    .Subject = "This is the Subject line"
    .HTMLBody = RangetoHTML(rng)
    .Display
    End With
    On Error GoTo 0

    With excel.Application
    .EnableEvents = True
    .ScreenUpdating = True
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
    End Sub

  2. #2
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    883

    Re: Email Selection From Excel, No Selection Copied

    Try taking the select out of the middle of the Set rng line i.e.

    Please Login or Register  to view this content.
    and make sue that you are not in R1C1 reference style i.e. you have column letters and not numbers showing.

    and please use code tags!!!!!!!
    Last edited by WasWodge; 03-27-2014 at 11:36 PM.
    If my solution worked (or not) please let me know. If your question is answered then please remember to mark it solved

    Computers are like air conditioners. They work fine until you start opening windows. ~Author Unknown

  3. #3
    Registered User
    Join Date
    03-27-2014
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Email Selection From Excel, No Selection Copied

    Thanks for the response and apologies for not putting code tags in.

    I've tried replacing .select with the above but the same issue occurs.

    I can see that the range is being 'selected' but for some reason it appears that it isn't recognising that there are entries in the cells, hence I'm getting the error capture or if removing that just nothing pasted into the Email?

  4. #4
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    883

    Re: Email Selection From Excel, No Selection Copied

    The code below works fine for me (as long as the sheet is in A1 reference style) and obviously using Outlook

    by the way
    I can see that the range is being 'selected'
    how can you see it being selected if you actually removed the select as I posted????

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    03-27-2014
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Email Selection From Excel, No Selection Copied

    Thanks for the reply.

    When I say that I can see the range is being selected, I mean that with screenupdating turned off you can see the range of cells become highlighted with the select statement.

    I can get the code to work in Excel, just not when referencing Excel from Access.

  6. #6
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    883

    Re: Email Selection From Excel, No Selection Copied

    I haven't got access in front of me until I knock off and so I won't be able to look at it until then.
    Hopefully someone might jump in before then.

  7. #7
    Registered User
    Join Date
    03-27-2014
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Email Selection From Excel, No Selection Copied

    Thanks for looking.

+ 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. Replies: 2
    Last Post: 10-07-2013, 11:06 AM
  2. email selection not working
    By djblois1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-13-2012, 09:47 AM
  3. Save selection as CSV and email
    By Milet in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-17-2011, 06:07 PM
  4. Email cell selection
    By jmcall10 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-21-2010, 09:15 AM
  5. Saving copied info while running a Selection Change Macro
    By J Streger in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-04-2005, 08:06 PM

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