By gwentaylor ar ddydd Llun, 29 Mawrth 2021
Postiwyd yn Excel
atebion 0
hoff bethau 0
barn 2.8K
Pleidleisiau 0
Hi I am using your code to send a range in excel by e-mail.  If I cancel the range I get a runtime error.  Is it possible to add some code or msgbox to prevent this please and also to autofit the columns on attachment.  Part of the code is below
diolch/media/kunena/emoticons/smile.png

Dim xFile Fel Llinynnol
Dim xFformat Cyhyd
Dim Wb Fel Llyfr Gwaith
Dim Wb2 Fel Gweithlyfr
Dim Ws Fel Taflen Waith
Dim FfeilPath Fel Llinynnol
Enw Ffeil Dim Fel Llinyn
Dim OutlookApp Fel Gwrthrych
Dim OutlookMail Fel Gwrthrych
Dim WorkRng Fel Ystod
xTitleId = "Enghraifft"
Set WorkRng = Cais.Selection
Gosod WorkRng = Application.InputBox ("Ystod", xTitleId, WorkRng.Address, Math:=8)

Application.ScreenUpdating = Anghywir
Application.DisplayAlerts = Gau
Gosod Wb = Application.ActiveWorkbook
Taflenni Gwaith wb.Add
Gosod Ws = Application.ActiveSheet
WorkRng.Copi Ws.Cells(1, 1)
Ws.Copy
Gosod Wb2 = Application.ActiveWorkbook
Dewiswch Achos Wb.FileFormat
Achos xlOpenXMLWorkbook:
    xFile = ".xlsx"
    xFormat = xlOpenXMLWorkbook
Achos xlOpenXMLWorkbookMacro wedi'i alluogi:
    Os Wb2.HasVBProject Yna
        xFile = ".xlsm"
        xFormat = xlOpenXMLWorkbookMacroEnabled
    arall
        xFile = ".xlsx"
        xFormat = xlOpenXMLWorkbook
    Gorffennwch Os
Achos Excel8:
    xFile = ".xls"
    xFformat = Excel8
Achos xlExcel12:
    xFile = ".xlsb"
    xFformat = xlExcel12
Diwedd Dewis
Gweld y Post Llawn