By gwentaylor på måndagen den 29 mars 2021
Inlagd i excel
svar 0
Gillar 0
Visningar 2.8K
Röster 0
Hej, jag använder din kod för att skicka ett Excel-intervall som en e-postbilaga men får ett körtidsfel om jag avbryter intervallet. Finns det kod jag kan lägga till eller en msgbox för att förhindra att detta händer? Tackkod nedan.

Sub SendRange()
Dim xFile As String
Dim xFormat As Long
Dim Wb Som arbetsbok
Dim Wb2 Som arbetsbok
Dim Ws Som arbetsblad
Dimma filsökväg som sträng
Dim filnamn som sträng
Dimma OutlookApp som objekt
Dimma OutlookMail som objekt
Dim WorkRng As Range
xTitleId = "Exempel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Ställ in Wb = Application.ActiveWorkbook
Wb.Worksheets.Add
Ställ in Ws = Application.ActiveSheet
WorkRng.Copy Ws.Cells(1, 1)
Ws.Copy
Ställ in Wb2 = Application.ActiveWorkbook
Välj Case Wb.FileFormat
Case xlOpenXMLWorkbook:
    xFile = ".xlsx"
    xFormat = xlOpenXMLWorkbook
Fall xlOpenXMLWorkbookMacroEnabled:
    Om Wb2.HasVBProject Då
        xFile = ".xlsm"
        xFormat = xlOpenXMLWorkbookMacroEnabled
    annars
        xFile = ".xlsx"
        xFormat = xlOpenXMLWorkbook
    End If
Fall Excel8:
    xFile = ".xls"
    xFormat = Excel8
Fodral xlExcel12:
    xFile = ".xlsb"
    xFormat = xlExcel12
Avsluta Välj
FilePath = Environ$("temp") & "\"
Filnamn = Wb.Name & Format(Nu, "dd-mmm-åå h-mm-ss")
Ställ in OutlookApp = CreateObject("Outlook.Application")
Ställ in OutlookMail = OutlookApp.CreateItem(0)
Wb2.SaveAs FilePath & FileName & xFile, FileFormat:=xFormat
Med OutlookMail
    .To = "gtest@email.com"
    .CC = ""
    .BCC = ""
    .Subject = "Tester"
    .Body = "Hej ."
    .Attachments.Add Wb2.FullName
    .Skicka
Sluta med
Wb2.Stäng
Döda FilePath & FileName & xFile
Ställ in OutlookMail = Ingenting
Ställ in OutlookApp = Ingenting
Ws.Delete
Application.DisplayAlerts = Sant
Application.ScreenUpdating = True
End Sub
 
Visa hela inlägget