+ Reply to Thread
Results 1 to 4 of 4

Macro not working

Hybrid View

cafc_fuller Macro not working 07-31-2013, 07:13 AM
cafc_fuller Re: Macro not working 07-31-2013, 07:25 AM
blue.chio Re: Macro not working 07-31-2013, 08:14 AM
arlu1201 Re: Macro not working 08-02-2013, 01:49 PM
  1. #1
    Registered User
    Join Date
    01-04-2011
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    67

    Macro not working

    Hello,

    I have a sheet when a button is clicked to run the macro below, however I get the error
    "Cannot run the macro 'Despatch.xlsm"Tracking'. The macro may not be available in this workbook or all macros may be disabled."

    If i run the macro in VBA it works fine? Macros are enabelled - does anyone know why this is?
    Also is there a way when emailing the body of an email to keep the format of text etc?
    Last point for the to: i want the value to be that of Cell B12 (this is where the email address is)

    Thanks
    Darren


    Sub Mail_Sheet_Outlook_Body()
    
        Dim rng As Range
        Dim OutApp As Object
        Dim OutMail As Object
        
        With Application
            .EnableEvents = False
            .ScreenUpdating = False
        End With
    
        Set rng = Nothing
        Set rng = Sheets("Despatch").UsedRange
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
        On Error Resume Next
        With OutMail
            .To = ""
            .CC = ""
            .BCC = ""
            .Subject = "Despatch"
            .HTMLBody = RangetoHTML(rng)
            .display
        End With
        On Error GoTo 0
    
        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    End Sub
    
    
    Function RangetoHTML(rng As Range)
        Dim fso As Object
        Dim ts As Object
        Dim TempFile As String
        Dim TempWB As Workbook
    
        TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
    
        'Copy the range and create a new workbook to past the data in
        rng.Copy
        Set TempWB = Workbooks.Add(1)
        With TempWB.Sheets(1)
            .Cells(1).PasteSpecial Paste:=8
            .Cells(1).PasteSpecial xlPasteValues, , False, False
            .Cells(1).PasteSpecial xlPasteFormats, , False, False
            .Cells(1).Select
            Application.CutCopyMode = False
            On Error Resume Next
            .DrawingObjects.Visible = True
            .DrawingObjects.Delete
            On Error GoTo 0
        End With
    
        'Publish the sheet to a htm file
        With TempWB.PublishObjects.Add( _
             SourceType:=xlSourceRange, _
             Filename:=TempFile, _
             Sheet:=TempWB.Sheets(1).Name, _
             Source:=TempWB.Sheets(1).UsedRange.Address, _
             HtmlType:=xlHtmlStatic)
            .Publish (True)
        End With
    
        'Read all data from the htm file into RangetoHTML
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
        RangetoHTML = ts.readall
        ts.Close
        RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                              "align=left x:publishsource=")
    
        'Close TempWB
        TempWB.Close savechanges:=False
    
        'Delete the htm file we used in this function
        Kill TempFile
    
        Set ts = Nothing
        Set fso = Nothing
        Set TempWB = Nothing
    End Function
    Last edited by arlu1201; 08-02-2013 at 01:40 PM. Reason: Replaced quote tags with code tags.

  2. #2
    Registered User
    Join Date
    01-04-2011
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    67

    Re: Macro not working

    I have fixed the error why it is not working, but could someone please help me with the following:

    Also is there a way when emailing the body of an email to keep the format of text etc?
    Last point for the to: i want the value to be that of Cell B12 (this is where the email address is)

    Thanks
    Darren

  3. #3
    Forum Contributor
    Join Date
    03-14-2012
    Location
    location
    MS-Off Ver
    Excel 2007
    Posts
    170

    Re: Macro not working

    perhaps something like this
    '
    '
    With OutMail
    .To = Sheets("Despatch").Range("b12")
    .CC = ""
    .BCC = ""
    '
    '
    blue

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Macro not working

    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution.

    Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.

    To change a Title on your post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ 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. Converting workbook with Macro to a template causes the Macro to stop working
    By Skeeta in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-20-2012, 02:00 PM
  2. Macro Stopped Working and Macro Not Listed
    By XLevel in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-19-2011, 04:43 AM
  3. Macro was working but is not now
    By jeskit in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-13-2011, 05:52 AM
  4. Macro not working
    By smbutler in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-20-2007, 09:34 AM
  5. [SOLVED] Macro only working on 1 row
    By naiveprogrammer in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-17-2005, 05:05 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