agentkramr
Board Regular
- Joined
- Dec 27, 2021
- Messages
- 98
- Platform
- Windows
I have a macro that gets launched by a vbs script that i have going so the macro is
i keep getting a random "error 1004" on line
it happens intermittently , it will work fine once or twice then do it again ...
the vbs i have just opens the workbook runs this macro and then closes the workbook
which i am also trying to figure out how to add save the workbook to
Any help is greatly appreciated , i have been trying to wrap up this project for weeks but the random error is killing me
VBA Code:
Sub CB_Hourly()
Dim oApp As Object, oMail As Object, FileStr As String
Dim NewWb As Workbook, cnt As Integer
Dim FileName As String, MailSub As String, MailTxt As String
Dim tmpImageName As String
'************************************************* ********
'Set email details; Comment out if not required
Const MailTo = "test@test.com"
'Const MailCC = "[EMAIL]some2@someone.com[/EMAIL]"
'Const MailBCC = "[EMAIL]some3@someone.com[/EMAIL]"
MailSub = "Test"
MailTxt = ""
Workbooks("Test Hourly Counts Auto").RefreshAll
'Turns off screen updating
Application.ScreenUpdating = False
Sheets("1 Hour Counts").Unprotect "Test"
'define a temp path for your image
tmpImageName = Environ$("temp") & "" & "TempChart.jpg"
'create image file
Call CreateJpg("1 Hour Counts", Sheets("1 Hour Counts").Range("A1:T50"))
'copy range to new wb/remove formulas
Set NewWb = Workbooks.Add
'copy all sheets
For cnt = 1 To ThisWorkbook.Sheets.Count
ThisWorkbook.Sheets(cnt).Copy NewWb.Sheets(cnt)
Next cnt
NewWb.Sheets("1 Hour Counts").Range("A1:T50").Copy
NewWb.Worksheets("1 Hour Counts").Range("A1").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
'NewWb.Worksheets("1 Hour Counts").Shapes("Rectangle: Rounded Corners 1").Delete
'NewWb.Worksheets("1 Hour Counts").Shapes("Rectangle: Rounded Corners 2").Delete
NewWb.Worksheets("1 Hour Counts").Activate
NewWb.Worksheets("1 Hour Counts").Range("A1").Select
Application.DisplayAlerts = False
NewWb.Worksheets("Sheet1").Delete
NewWb.SaveAs FileName:=Left(ThisWorkbook.FullName, Len(ThisWorkbook.FullName) - 5) & ".xlsx", _
FileFormat:=xlOpenXMLWorkbook
Application.DisplayAlerts = True
FileStr = NewWb.FullName
NewWb.Close
Sheets("1 Hour Counts").Protect "Test"
'Creates and shows the outlook mail item
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(0)
With oMail
.To = MailTo
'.Cc = MailCC
'.Bcc = MailBCC
.Subject = MailSub
.HTMLBody = "<body><img src=" & "'" & tmpImageName & "'/></body>"
.Attachments.Add FileStr
.Display
.Send
End With
'Deletes the temporary file
'WB.ChangeFileAccess Mode:=xlReadOnly
Kill (Environ$("temp") & "" & "TempChart.jpg")
Kill FileStr
'Restores screen updating and release Outlook
Application.ScreenUpdating = True
Set oMail = Nothing
Set oApp = Nothing
'Save Workbook
'ThisWorkbook.Save
End Sub
Public Sub CreateJpg(SheetName As String, xRgAddrss As Range)
'creates temp JPG file of range (xRgAddrss) by creating temp chart
'uses current wb sheet (sheetname) to locate temp chart
Dim xRgPic As Range
Worksheets(SheetName).Activate
Set xRgPic = xRgAddrss
xRgPic.CopyPicture Appearance:=xlScreen, Format:=xlPicture
With ThisWorkbook.Worksheets(SheetName).ChartObjects.Add(xRgPic.Left, xRgPic.Top, _
xRgPic.Width, xRgPic.Height)
.Activate
.Chart.Paste
.Chart.Export Environ$("temp") & "" & "TempChart.jpg", "JPG"
End With
Worksheets(SheetName).ChartObjects(Worksheets(SheetName).ChartObjects.Count).Delete
End Sub
i keep getting a random "error 1004" on line
VBA Code:
xRgPic.CopyPicture Appearance:=xlScreen, Format:=xlPicture
it happens intermittently , it will work fine once or twice then do it again ...
the vbs i have just opens the workbook runs this macro and then closes the workbook
Code:
dim EXL
Set EXL = CreateObject("Excel.Application")
'not required
EXL.Visible = true
'File and Macro Call
EXL.Workbooks.open "C:\Users\automatedreports\Desktop\Automated Reports\Hourly\CB Hourly Counts Auto"
EXL.Run "CB_Hourly"
'Close Application
EXL.Quit
Set EXL = Nothing
which i am also trying to figure out how to add save the workbook to
Any help is greatly appreciated , i have been trying to wrap up this project for weeks but the random error is killing me