+ Reply to Thread
Results 1 to 3 of 3

Macro to copy an image and create a file does not work when run automatically through VBS

Hybrid View

  1. #1
    Registered User
    Join Date
    04-14-2019
    Location
    Singapore
    MS-Off Ver
    Office 365
    Posts
    20

    Macro to copy an image and create a file does not work when run automatically through VBS

    Hi ,

    I have a macro to create an image file and then attach it to a mail in outlook . The macro runs fine when I run it manually . However I have a requirement to send the same on a mail at 4 am and for that I have created a VBS file to run the same at 4 AM through Windows scheduler. However when it is run automatically at 4 AM the macro does not wok and mail is sent without an image. First I thought it was a issue with my Mail macro but the issue I found that the range when copy and pasted as graph and then exported it to image file is not working when runs automatically . Looks like the VBS scheduler runs faster and does not either copy the range or paste or fails to export. I tried to put Application.Wait also after copy and after paste but the problem is still there. The image file is created blank.

    Can anyone help?

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Macro to copy an image and create a file does not work when run automatically through

    While vbscript is similar to VBA, this is a VBA macros forum. Why not use VBA? If this were, I would say post the code.

    For those sorts of things, I put a popup msgbox in the open event. If no response, it closes the Windows Scheduler workbook, runs the macro, and closes the workbook. If I respond to the msgbox, it skips the macro so I can edit the file.

    e.g. In ThisWorbook object:
    Private Sub Workbook_Open()
    'ozgrid.com #43022, Bob Phillips
      Dim cTime As Long
      Dim wsh As Object
       
      Set wsh = CreateObject("WScript.Shell")
      cTime = 15 ' 15 secs
      Select Case wsh.Popup("Test?", cTime, "Question - Times Out in 15s", vbOKCancel)
        Case vbOK
            MsgBox "You clicked OK"
        Case vbCancel
            MsgBox "You clicked Cancel"
        Case -1
            MsgBox "Timed out"
            'comment out above and add code here if you want to do stuff before close.
            ThisWorkbook.Close
        Case Else
      End Select
    End Sub
    Last edited by Kenneth Hobson; 09-08-2019 at 11:27 AM.

  3. #3
    Registered User
    Join Date
    04-14-2019
    Location
    Singapore
    MS-Off Ver
    Office 365
    Posts
    20
    Hi

    Thanks a lot !

    Let me send you the code I am using in vba . The problem is when this macro is called through VBS file the image is not created specially when run in night when the task is scheduled. The code is as follows
    Public Sub CreateScreenshot()
     
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    Dim ChartName As String
    Dim imgPath As String
    Dim NSheetName As String, RngFrm As String, RngTo As String
    Dim wb As Workbook
    Dim WB1 As Workbook
    Dim AttachScreenshot As String
    Dim ImgFilePath As String
    Dim AttachFile As String
    Set wb = ThisWorkbook
    AttachFile = wb.Worksheets("Mail Content").Range("D6").Value
    NSheetName = wb.Worksheets("Mail Content").Range("E10").Value
    RngFrm = wb.Worksheets("Mail Content").Range("F10").Value
    RngTo = wb.Worksheets("Mail Content").Range("G10").Value
    AttachScreenshot = wb.Worksheets("Mail Content").Range("H10").Value
    ImgFilePath = "C:\MailScheduler_Files\" & Worksheets("Mail Content").Range("D14").Value
    tmpImageName = ImgFilePath & "\ScreenShot.jpg"
    SaveExt = "." & Right(AttachFile, Len(AttachFile) - InStrRev(AttachFile, "."))
    AttachfileName = Right(AttachFile, Len(AttachFile) - InStrRev(AttachFile, "\"))
     
    'Clearing the Office Clipboard
        Dim oData   As New DataObject 'object to use the clipboard
        oData.Clear
        oData.SetText Text:=Empty 'Clear
    '    oData.PutInClipboard 'take in the clipboard to empty it
    ' Clear Clipboard
     
     
    'Check if Folder Exists - if not then create a folder and save image there.
    Folder = Dir(ImgFilePath, vbDirectory)
     
        If Folder = vbNullString Then
            VBA.FileSystem.MkDir (ImgFilePath)
        End If
       
        'Remove existing image to create new image
        If FileExists(tmpImageName) Then
            ' First remove readonly attribute, if set
            SetAttr tmpImageName, vbNormal
            ' Then delete the file
            Kill tmpImageName
        End If
     
    'Refresh  file and get new screenshot saved in the given path
    Set WB1 = Workbooks.Open(AttachFile)
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
     
    WB1.Unprotect
    WB1.RefreshAll
     
    'Range to save as an image
     
    On Error Resume Next
    Set rangetosend = WB1.Worksheets(NSheetName).Range(RngFrm & ":" & RngTo)
    Call Clear_Clipboard
    WB1.Worksheets(NSheetName).Range(RngFrm & ":" & RngTo).CopyPicture Appearance:=xlPrinter, Format:=xlPicture
      'wait until the clipboard gets a pic, but not over 9 seconds (avoid infinite loop)
                        T = Timer
                        Do
                              Waiting (10)
                        Loop Until Is_Pic_in_Clipboard Or Timer - T > 0.9
    ' Application.Wait (Now + TimeValue("0:00:10"))
        If Not GetWorksheet("TempImage") Is Nothing Then
            WB1.Worksheets("TempImage").Delete
        End If
    WB1.Worksheets.Add(After:=Sheets(Sheets.Count)).Name = "TempImage"
    Dim sht As Worksheet
    Set sht = WB1.Worksheets("TempImage")
    sht.Shapes.AddChart
    sht.Shapes.Item(1).Select
    Set objChart = ActiveChart
    'On Error Resume Next
    With objChart
        .ChartArea.Height = rangetosend.Height
        .ChartArea.Width = rangetosend.Width
        .ChartArea.Fill.Visible = msoFalse
        .ChartArea.Border.LineStyle = xlLineStyleNone
        .Paste
         Application.Wait (Now + TimeValue("0:00:10"))
         Application.CutCopyMode = False
    End With
     
    sht.Shapes("Chart 1").Name = "ScreenshotN"
    'sht.Shapes.Item(1).Select
    tmpImageName = ImgFilePath & "\ScreenShot.jpg"
    sht.Shapes("screenshotN").Select
    Set objChart = ActiveChart
    'On Error Resume Next
    With objChart
    .Export FileName:=tmpImageName, Filtername:="JPG"
    End With
    WB1.Worksheets("TempImage").Visible = xlSheetHidden
    WB1.Worksheets(NSheetName).Activate
    Range("A1").Select
    WB1.Save
    WB1.Close True
     
    End sub
    Last edited by davesexcel; 09-23-2021 at 05:20 AM.

+ 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. macro create file copy
    By max_max in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-19-2019, 12:09 PM
  2. Macro to Open File/s and copy data won't work
    By Howardc1001 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-20-2019, 01:16 AM
  3. macro to just automatically open the pdf file in a location and copy it and paste in XL
    By catchnanan in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-29-2018, 09:29 AM
  4. Macro to create a copy of XlSB file and save as XLSX file without any formulas.
    By Mysore in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-28-2016, 03:11 AM
  5. [SOLVED] Macro to copy the data from a closed file to another work book using filters
    By Kiran Kurapati in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-26-2014, 12:50 PM
  6. Need help to create a macro to automatically copy in one sheet and copy in the other
    By Thammiraju in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-24-2014, 09:36 PM
  7. Macro to create copy of a .xls file (Beginner)
    By Yaz Patel in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-11-2008, 10:30 AM

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