+ Reply to Thread
Results 1 to 7 of 7

time scheduler and server execution error

  1. #1
    Registered User
    Join Date
    12-13-2021
    Location
    ISRAEL
    MS-Off Ver
    MS 2016
    Posts
    6

    time scheduler and server execution error

    Hi,
    I'm using a time scheduler system to open excels.
    when the excels opens it does several things. at he ends it will finish by sending an email with and attachment and a range.
    when I'm running the excel - the macros work fine but when Im using the trigger it gives me the server execution failed.
    this is my macro :
    Sub Mail_ActiveSheetTest()

    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim OutApp As Outlook.Application
    Dim OutMail As Outlook.MailItem
    Dim rng As Range


    Set rng = Nothing
    On Error Resume Next

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

    Set Sourcewb = ActiveWorkbook
    ActiveWorkbook.ShowPivotTableFieldList = False
    'Copy the ActiveSheet to a new workbook
    ActiveSheet.Copy
    Set Destwb = ActiveWorkbook


    With Destwb
    If Val(Application.Version) < 12 Then

    FileExtStr = ".xls": FileFormatNum = -4143
    Else

    Select Case Sourcewb.FileFormat
    Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
    Case 52:
    If .HasVBProject Then
    FileExtStr = ".xlsm": FileFormatNum = 52
    Else
    FileExtStr = ".xlsx": FileFormatNum = 51
    End If
    Case 56: FileExtStr = ".xls": FileFormatNum = 56
    Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
    End Select
    End If
    End With
    Set rng = ActiveSheet.Range("c121160").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

    'Save the new workbook/Mail it/Delete it
    TempFilePath = Environ$("temp") & "\"
    TempFileName = "DailySetReport" & " " & Format(Now, "dd-mmm-yy")

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

    With Destwb
    .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
    On Error Resume Next
    With OutMail
    .To = ""
    .CC = ""
    .BCC = ""
    .Subject = "DailySetsReport " & Format(Now, "dd-mmm-yy")
    .Attachments.Add Destwb.FullName
    .HTMLBody = RangetoHTML(rng)
    .Send 'or use .Display
    'You can add other files also like this
    '.Attachments.Add ("C:\test.txt")
    .Send 'or use .Display
    End With
    On Error GoTo 0
    .Close savechanges:=False
    End With

    'Delete the file you have send
    Kill TempFilePath & TempFileName & FileExtStr

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
    .ScreenUpdating = True
    .EnableEvents = True
    End With
    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"

    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


    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


    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

  2. #2
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,303

    Re: time scheduler and server execution error

    very unusual to see an 'emoticon' in VBA code.
    place your code between code tags as required so that we can run it and debug it.
    Torachan,

    Mission statement; Promote the use of Tables, Outlaw the use of 'merged cells' and 'RowSource'.

  3. #3
    Registered User
    Join Date
    12-13-2021
    Location
    ISRAEL
    MS-Off Ver
    MS 2016
    Posts
    6

    Re: time scheduler and server execution error

    just a clarification - im opening the excel by a task scheduler:
    that's the code :
    thanks!

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    12-13-2021
    Location
    ISRAEL
    MS-Off Ver
    MS 2016
    Posts
    6

    Re: time scheduler and server execution error

    Quote Originally Posted by torachan View Post
    very unusual to see an 'emoticon' in VBA code.
    place your code between code tags as required so that we can run it and debug it.
    Please Login or Register  to view this content.

  5. #5
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,303

    Re: time scheduler and server execution error

    are you sure about this range ????
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    12-13-2021
    Location
    ISRAEL
    MS-Off Ver
    MS 2016
    Posts
    6

    Re: time scheduler and server execution error

    sorry, that's the right code:



    sub mail()

    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String

    Set rng = Nothing
    On Error Resume Next



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

    Set Sourcewb = ActiveWorkbook
    ActiveWorkbook.ShowPivotTableFieldList = False


    ActiveSheet.Copy
    Set Destwb = ActiveWorkbook



    Set rng = ActiveSheet.Range("c12:o1160").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 Application
    .EnableEvents = False
    .ScreenUpdating = False
    End With
    TempFilePath = Environ$("temp") & "\"
    TempFileName = "DailySetReport"
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    With Destwb
    .SaveAs TempFilePath & TempFileName & "xlsm", FileFormat:=FileFormatNum

    On Error Resume Next
    With OutMail
    .To = ""
    .CC = ""
    .BCC = ""
    .Subject = "DailySetsReport "
    .Attachments.Add Destwb.FullName
    .HTMLBody = RangetoHTML(rng)
    .Send 'or use .Display

    End With
    On Error GoTo 0
    .Close SaveChanges:=False
    End With

    'Delete the file you have send
    Kill TempFilePath & TempFileName & FileExtStr

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
    .ScreenUpdating = True
    .EnableEvents = True
    End With
    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"


    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


    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


    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=")


    TempWB.Close SaveChanges:=False

    Kill TempFile

    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
    End Function

  7. #7
    Registered User
    Join Date
    12-13-2021
    Location
    ISRAEL
    MS-Off Ver
    MS 2016
    Posts
    6

    Re: time scheduler and server execution error

    can anyone help?

+ 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. run-time error '.2147217843 (80040e4d)' when connecting to SQL server
    By Lucho Pedano in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-13-2022, 02:50 AM
  2. VBA in task scheduler closes other opened excel tabs after the execution
    By Manikanta9333 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-20-2021, 08:47 AM
  3. Macro to email workbook -run time error "server execution failed"
    By Howardc1001 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-19-2021, 01:44 PM
  4. VBA Hiding rows multi criterias code time execution issue + Copy sheets VBA error
    By hunker in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-17-2020, 12:44 AM
  5. [SOLVED] Run-time error 462: remote server machine does not exist
    By mrbarba in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-18-2020, 11:34 AM
  6. Code Execution Hangs When Run by the Task Scheduler
    By dflak in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-27-2019, 10:21 AM
  7. Replies: 4
    Last Post: 03-16-2006, 05:25 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