Envoi de 2 onglets par email
salut
Je souhaiterais adapter le code suivant afin qu'il copie une plage diff2rente de cellules selon qu'il s'agisse de l'onglet "general" ou "daily".
Pour le moment le code copie les 2 feuilles de la même manière, intégralement.
Set Sourcewb = ActiveWorkbook
'Copy the sheets to a new workbook
'We add a temporary Window to avoid the Copy problem
'if there is a List or Table in one of the sheets and
'if the sheets are grouped
With Sourcewb
Set TheActiveWindow = ActiveWindow
Set TempWindow = .NewWindow
.Sheets(Array("general", "daily")).Copy
End WithCependant, dans la feuille "general" je souhaiterais ne copier uniquement les colonnes de A a T, et en ce qui concerne les lignes, au minimum jusqu'à la ligne 18 ou, s'il existe du texte aux lignes suivantes (ligne 19 et plus), alors jusqu'à la dernière ligne qui n'est pas vide (entre les colonnes A et T).
Je ne sais pas comment intégrer le code suivant afin qu'il agisse uniquement sur la copie de l'onglet "general":
LastRow = WorksheetFunction.Max(18, Range("B" & Rows.Count).End(xlUp).Row)
'Set Source = Range("A1:T" & LastRow).SpecialCells(xlCellTypeVisible)En ce qui concerne la feuille "daily", je souhaiterais que soient copiées uniquement les plages de la cellule A1 à la dernière colonne remplie en ligne 6 et aussi jusqu'è la dernière ligne remplie entre ces colonnes.
Et le fichier workbook - issu de la copie de ces deux onglets- doit être uniquement le texte copiés, sans les macros ...
Je ne sais pas si c'est assez clair ...
J'espere que quelqu'un saura comment m'aider!
Sub Mail_two_sheets()
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim OutApp As Object
Dim OutMail As Object
Dim sh As Worksheet
Dim TheActiveWindow As Window
Dim TempWindow As Window
Dim EmailAddress As String
Dim Remark As String
Dim agentname As String
Dim LastRow As Long
ThisWorkbook.Sheets("daily").Visible = True
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
EmailAddress = InputBox("Veuillez entrer ci-dessous l'adresse email à laquelle vous souhaitez envoyer la rooming list. (L'hôtel recevra en pièce jointe les données contenues dans l'onglet général sans le PNR ni la remarque sur le règlement.)", "Adresse email")
If EmailAddress = "" Then
MsgBox "Vous devez préciser un email pour l'envoi. Action interrompue!", vbOKOnly, "Entrée invalide"
Exit Sub
Else
End If
If InStr(EmailAddress, "@") = 0 Then
MsgBox "Adresse email invalide. Action interrompue!", vbOKOnly, "Adresse invalide"
Exit Sub
Else
End If
agentname = InputBox("Veuillez entrer le prenom de l'hotelier.", "Nom hotelier")
If agentname = "" Then
agentname = "Mrs/Mr"
End If
Remark = InputBox("Veuillez entrer ci-dessous vos remarques, si vous en avez. Elles seront intégrées dans l'email. ATTENTION! Ne cliquez pas sur la touche ENTER pour aller à la ligne", "Remarques")
If Remark = "" Then
Remark = "none"
Else
End If
ActiveSheet.Unprotect "obrat"
Columns("J").EntireColumn.Hidden = True
Columns("L").EntireColumn.Hidden = True
Set Sourcewb = ActiveWorkbook
'Copy the sheets to a new workbook
'We add a temporary Window to avoid the Copy problem
'if there is a List or Table in one of the sheets and
'if the sheets are grouped
With Sourcewb
Set TheActiveWindow = ActiveWindow
Set TempWindow = .NewWindow
.Sheets(Array("general", "daily")).Copy
End With
[b]'copy from row 18 until last filled row (from column A to T)
'LastRow = WorksheetFunction.Max(18, Range("B" & Rows.Count).End(xlUp).Row)
'Set Source = Range("A1:T" & LastRow).SpecialCells(xlCellTypeVisible)
[/b]
'Close temporary Window
TempWindow.Close
Set Destwb = ActiveWorkbook
'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
Else
'You use Excel 2007-2016
Select Case Sourcewb.FileFormat
Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
Case 52:
If .HasVBProject Then
FileExtStr = ".xlsm": FileFormatNum = 52
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
Case 56: FileExtStr = ".xls": FileFormatNum = 56
Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
End Select
End If
End With
'Save the new workbook/Mail it/Delete it
TempFilePath = Environ$("temp") & "\"
TempFileName = Range("B1") & " " & Range("D1")
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With Destwb
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
On Error Resume Next
With OutMail
.To = EmailAddress
.CC = ""
.BCC = ""
.Subject = "here text"
.Body = "here text"
.Attachments.Add Destwb.FullName
.display
End With
On Error GoTo 0
.Close savechanges:=False
End With
'WRITE TIME
[W10] = Date
[W9] = EmailAddress
[W11] = Time
'Delete the file you have send
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
Columns("J").EntireColumn.Hidden = False
Columns("L").EntireColumn.Hidden = False
'WRITE TIME
[W10] = Date
[W9] = EmailAddress
[W11] = Time
ThisWorkbook.Sheets("daily").Visible = False
ActiveSheet.Protect Password:="obrat", DrawingObjects:=False, AllowFormattingCells:=True
ActiveWorkbook.Save
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
slt,
Set Sourcewb = ActiveWorkbook
LastRow = WorksheetFunction.Max(18, Range("B" & Rows.Count).End(xlUp).Row)
Set Source = Sheets("general").Range("A1:T" & LastRow).SpecialCells(xlCellTypeVisible)
'Copy the sheets to a new workbook
'We add a temporary Window to avoid the Copy problem
'if there is a List or Table in one of the sheets and
'if the sheets are grouped
With Sourcewb
Set TheActiveWindow = ActiveWindow
Set TempWindow = .NewWindow
.Sheets(Array("Source", "daily")).Copy
End WithSalut
J'ai essayé mais cela copie tous les onglets intégralement, malgré que seulement deux doivent être copiés (general +daily)
de plus, ça donne 2 messages d'erreur:
- Path/File access error
- run-time error '1004'
Sub Mail_two_sheets()
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim OutApp As Object
Dim OutMail As Object
Dim sh As Worksheet
Dim TheActiveWindow As Window
Dim TempWindow As Window
Dim EmailAddress As String
Dim Remark As String
Dim agentname As String
Dim LastRow As Long
ThisWorkbook.Sheets("daily").Visible = True
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
EmailAddress = InputBox("Veuillez entrer ci-dessous l'adresse email à laquelle vous souhaitez envoyer la rooming list. (L'hôtel recevra en pièce jointe les données contenues dans l'onglet général sans le PNR ni la remarque sur le règlement.)", "Adresse email")
If EmailAddress = "" Then
MsgBox "Vous devez préciser un email pour l'envoi. Action interrompue!", vbOKOnly, "Entrée invalide"
Exit Sub
Else
End If
ActiveSheet.Unprotect "obrat"
Columns("J").EntireColumn.Hidden = True
Columns("L").EntireColumn.Hidden = True
Set Sourcewb = ActiveWorkbook
'copy from row 18 until last filled row (from column A to T)
LastRow = WorksheetFunction.Max(18, Range("B" & Rows.Count).End(xlUp).Row)
Set Source = Range("A1:T" & LastRow).SpecialCells(xlCellTypeVisible)
'Copy the sheets to a new workbook
'We add a temporary Window to avoid the Copy problem
'if there is a List or Table in one of the sheets and
'if the sheets are grouped
With Sourcewb
Set TheActiveWindow = ActiveWindow
Set TempWindow = .NewWindow
.Sheets(Array("general", "daily")).Copy
End With
'Close temporary Window
TempWindow.Close
Set Destwb = ActiveWorkbook
'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
Else
'You use Excel 2007-2016
Select Case Sourcewb.FileFormat
Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
Case 52:
If .HasVBProject Then
FileExtStr = ".xlsm": FileFormatNum = 52
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
Case 56: FileExtStr = ".xls": FileFormatNum = 56
Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
End Select
End If
End With
' 'Change all cells in the worksheets to values if you want
' For Each sh In Destwb.Worksheets
' sh.Select
' With sh.UsedRange
' .Cells.Copy
' .Cells.PasteSpecial xlPasteValues
' .Cells(1).Select
' End With
' Application.CutCopyMode = False
' Destwb.Worksheets(1).Select
' Next sh
'Save the new workbook/Mail it/Delete it
TempFilePath = Environ$("temp") & "\"
TempFileName = Range("B1") & " " & Range("D1")
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With Destwb
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
On Error Resume Next
With OutMail
.To = EmailAddress
.CC = ""
.BCC = ""
.Subject = Range("B1") & " " & Range("D1")
.Body = "here text"
.Attachments.Add Destwb.FullName
.display
End With
On Error GoTo 0
.Close savechanges:=False
End With
'Delete the file you have send
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
Columns("J").EntireColumn.Hidden = False
Columns("L").EntireColumn.Hidden = False
ThisWorkbook.Sheets("daily").Visible = False
ActiveSheet.Protect Password:="obrat", DrawingObjects:=False, AllowFormattingCells:=True
ActiveWorkbook.Save
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End SubChange ce code :
.Sheets(Array("general", "daily")).Copyavec:
.Sheets(Array("Source", "daily")).Copyj'ai corrigé le code comme tu l'as conseillé mais toujours pareil:
message erreur :subscript out of range
et le fichier qui est copié c'est tout le Workbook complet...