Results 1 to 4 of 4

Macro not working

Threaded View

  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.

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