Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42220

Help on code with Run-time error ‘91’: Object Variable or With Block variable not set

$
0
0
Have an error code: Run-time error ‘91’: Object Variable or With Block variable not set.

Upon user update of their own specific worksheet within the workbook, I'm looking to have Excel automatically email user a copy/paste special values version of their newly amended/saved worksheet with the subject line RECEIPT: AMENDMENTS CONFIRMED. As indicated, I presently have a Run-time error I need to overcome. Additionally, I have included just one recipient in the code at present; however, what would be the most efficient way to include multiple email addresses based upon their own respective amended/saved worksheets? I have been to Ron de Bruin's VB Excel tips and this code is a variant on one of his SentMail VB code efforts. Below please see code as it currently is (thanks in advance for any assistance):

Code:

Private Sub Mail_ActiveSheet()

'Working in 97-2007
    Dim rcell As Range
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
   
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set Sourcewb = ActiveWorkbook
   
   
    With Worksheets("John")
        For Each rcell In .Range("Z75")
            If rcell.Value > 0 Then
            'Copy the sheet to a new workbook
            ActiveSheet.Copy
            Set Destwb = ActiveWorkbook
            End If
        Next rcell
    End With
   
    'Determine the Excel version and file extension/format
    With Destwb
        If Val(Application.Version) < 12 Then
            'You use Excel 97-2003
            FileExtStr = ".xls": FileFormatNum = -4143
        End If
    End With

    'Change all cells in the worksheet to values
    With Destwb.Sheets(1).UsedRange
    .Cells.Copy
    .Cells.PasteSpecial xlPasteValues
    .Cells(1).Select
    End With
    Application.CutCopyMode = False

    'Save the new workbook/Mail it/Delete it
    TempFilePath = Environ$("temp") & "\"
    TempFileName = "Part of " & Sourcewb.Name & " " _
                & Format(Now, "dd-mmm-yy h-mm-ss")

    With Destwb
        .SaveAs TempFilePath & TempFileName & FileExtStr, _
                FileFormat:=FileFormatNum
        On Error Resume Next
        .SendMail "xxxxxxx@gmail.com", _
                  " RECEIPT: AMENDMENTS CONFIRMED"
        On Error GoTo 0
        .Close SaveChanges:=False
    End With

    'Delete the file sent
    Kill TempFilePath & TempFileName & FileExtStr

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub


Viewing all articles
Browse latest Browse all 42220

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>