Kill PdfFile ne marche pas

Bonjour

Je reçois un message d'erreur (" bad file name or number ") sur la ligne "Kill PdfFile", censée effacer du Bureau le fichier PDF qui y a été sauvegardé pendant la macro.

Au début tout marchait sur un autre ordinateur. Est-ce éventuellement une macro qui ne fonctionne que sur un seul type de Windows?

Comment peut-on arranger ce problème (je travaille maintenant avec Windows 10 Pro en anglais).

Merci d'avance!

Sub sendemail()

  Dim IsCreated As Boolean
  Dim i As Long
  Dim PdfFile As String, Title As String
  Dim OutlApp As Object

  ' title for email subject
  Title = Range("C3") & " " & Range("b5") & " " & "-" & " " & Range("F4") & " " & Range("g4")

  ' Define PDF filename
  PdfFile = ActiveWorkbook.FullName & " " & Range("j62")

  i = InStrRev(PdfFile, ".")
  If i > 1 Then PdfFile = Left(PdfFile, i - 1)
  PdfFile = PdfFile & ".pdf"

  ' Export activesheet as PDF
  With ActiveSheet
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
  End With

  ' Use already open Outlook if possible
  On Error Resume Next
  Set OutlApp = GetObject(, "Outlook.Application")
  If Err Then
    Set OutlApp = CreateObject("Outlook.Application")
    IsCreated = True
  End If
  OutlApp.Visible = True
  On Error GoTo 0

  ' Prepare e-mail with PDF attachment
  With OutlApp.CreateItem(0)

    ' Prepare e-mail
    .Subject = Title
    .To = "my email"
    .CC = " "

    .Body = " Hey  " & vbLf & vbLf _
          & "  Please find in attachment the details for preparing salary. " & vbLf & vbLf _
          & "Best regards" & vbLf _
          & Application.UserName & vbLf & vbLf
    .Attachments.Add PdfFile

    ' Try to send
    On Error Resume Next
   '.send
     .display

    Application.Visible = True
    If Err Then
      MsgBox "Some error occurred", vbExclamation
    Else

    End If
    On Error GoTo 0

  End With

  ' Delete PDF file
  Kill PdfFile

  ' Quit Outlook if it was created by this code
  If IsCreated Then OutlApp.Quit

  ' Release the memory of object variable
  Set OutlApp = Nothing

End Sub

Bonjour,

et si tu inscris ta ligne en dernier après avoir quitté Outlook ?

non, pareil

Bonjour à tous,

Je reprends mes tests ...

ric

Le fichier est joint à un email, et seulement après son envoi la "commande d'assassinat" (kill) s'exécute.

De plus- le fichier Pdf est sauvegardé sur le Bureau et il n'est ouvert nulle part.

Juste un fichier sur le Bureau que je veux effacer...

Cette même macro fonctionnait il y a un an environ dans un autre ordinateur (que je n'ai plus), donc je suis certain que la macro fonctionnait parfaitement et le fichier était bien effacé.

Mais là la fonction qui "tue" le fichier pdf ne fonctionne plus et c'est pourquoi j'ai tendance à croire que c'est peut-être lié au changement d'ordinateur...

En ce qui concerne le chemin, il risque d'être différent sur chaque ordinateur, car ce fichier est destiné à être utilisé sur plein d'ordinateurs différents et chacun a un chemin différent...

Bonjour à tous,

Je reprends mes tests ...

ric

Pour le chemin comme il va varier en fonction des pc ;

ce ne serait pas plus simple d'utiliser la fonction

this workbook.path

Comme çà il aurait la même destination que le fichier excel.

Bonjour à tous,

Après avoir redémarré ma machine, j'ai testé le fichier placé sur le Bureau d'un compte Administrateur, puis sur le Bureau d'un compte Utilisateur.

Tout s'est bien déroulé avec ton code sans modification.

A+

ric

Bonjour à tous,

Après avoir redémarré ma machine, j'ai testé le fichier placé sur le Bureau d'un compte Administrateur, puis sur le Bureau d'un compte Utilisateur.

Tout s'est bien déroulé avec ton code sans modification.

A+

ric

j'ai eu aussi un fichier de se type là ... à chaque fois qu'il était déplacé il fallait le lancer une fois puis redémarrer la machine pour qu'il fonctionne.. j'avoue que je n'ai jamais compris pourquoi

Bonjour,

peut-être qu'Outlook n'a pas encore libéré le fichier.

Essayer avec une petite tempo avant :

Application.Wait DateAdd("s", 1, Now())

eric

j'ai mis

 Application.Wait DateAdd("s", 1, Now())

dans le code mais ça bug toujours à la ligne de KillPdf

Sub sendemail()

  Dim IsCreated As Boolean
  Dim i As Long
  Dim PdfFile As String, Title As String
  Dim OutlApp As Object

  ' title for email subject
  Title = Range("C3") & " " & Range("b5") & " " & "-" & " " & Range("F4") & " " & Range("g4")

  ' Define PDF filename
  PdfFile = ActiveWorkbook.FullName & " " & Range("j62")

  i = InStrRev(PdfFile, ".")
  If i > 1 Then PdfFile = Left(PdfFile, i - 1)
  PdfFile = PdfFile & ".pdf"

  ' Export activesheet as PDF
  With ActiveSheet
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
  End With

  ' Use already open Outlook if possible
  On Error Resume Next
  Set OutlApp = GetObject(, "Outlook.Application")
  If Err Then
    Set OutlApp = CreateObject("Outlook.Application")
    IsCreated = True
  End If
  OutlApp.Visible = True
  On Error GoTo 0

  ' Prepare e-mail with PDF attachment
  With OutlApp.CreateItem(0)

    ' Prepare e-mail
    .Subject = Title
    .To = "my email"
    .CC = " "

    .Body = " Hey  " & vbLf & vbLf _
          & "  Please find in attachment the details for preparing salary. " & vbLf & vbLf _
          & "Best regards" & vbLf _
          & Application.UserName & vbLf & vbLf
    .Attachments.Add PdfFile

    ' Try to send
    On Error Resume Next
   '.send
     .display

    Application.Visible = True
    If Err Then
      MsgBox "Some error occurred", vbExclamation
    Else

    End If
    On Error GoTo 0

  End With

  Application.Wait DateAdd("s", 1, Now())

    ' Delete PDF file
 Kill PdfFile

  ' Quit Outlook if it was created by this code
  If IsCreated Then OutlApp.Quit

  ' Release the memory of object variable
  Set OutlApp = Nothing

End Sub

Bonjour,

c'était à tenter

Tu peux mettre en commentaire tout ce qui concerne Outlook ?

Ca permettra de voir si c'est son appel ou des éventuels problèmes de droits.

eric

Que veux-tu dire par "mettre en commentaires tout ce qui concerne Outlook" ?

mettre une ' devant les lignes.

Faire juste la création et suppression du fichier, pour déterminer la cause.

Si tu arrives à supprimer c'est que c'est le traitement par outlook qui est bloquant, si tu n'y arrives toujours pas c'est possiblement une histoire de droits.

comme cela?

mais c'est toujours le meme bug sur la ligne du KillPdf

Sub sendemail()

  Dim IsCreated As Boolean
  Dim i As Long
  Dim PdfFile As String, Title As String
  Dim OutlApp As Object

  ' title for email subject
  Title = Range("C3") & " " & Range("b5") & " " & "-" & " " & Range("F4") & " " & Range("g4")

  ' Define PDF filename
  PdfFile = ActiveWorkbook.FullName & " " & Range("j62")

  i = InStrRev(PdfFile, ".")
  If i > 1 Then PdfFile = Left(PdfFile, i - 1)
  PdfFile = PdfFile & ".pdf"

  ' Export activesheet as PDF
  With ActiveSheet
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
  End With

  ' Use already open Outlook if possible
  'On Error Resume Next
  'Set OutlApp = GetObject(, "Outlook.Application")
  'If Err Then
    'Set OutlApp = CreateObject("Outlook.Application")
   ' IsCreated = True
 ' End If
 ' OutlApp.Visible = True
 ' On Error GoTo 0

  ' Prepare e-mail with PDF attachment
  'With OutlApp.CreateItem(0)

    ' Prepare e-mail
    '.Subject = Title
   ' .To = "my email"
   ' .CC = " "

   ' .Body = " Hey  " & vbLf & vbLf _
      '    & "  Please find in attachment the details for preparing salary. " & vbLf & vbLf _
        '  & "Best regards" & vbLf _
        '  & Application.UserName & vbLf & vbLf
  '  .Attachments.Add PdfFile

    ' Try to send
   ' On Error Resume Next
   '.send
  '   .display

    'Application.Visible = True
    'If Err Then
    '  MsgBox "Some error occurred", vbExclamation
   ' Else

   ' End If
   ' On Error GoTo 0

 ' End With

  'Application.Wait DateAdd("s", 1, Now())

    ' Delete PDF file
  Kill PdfFile

  ' Quit Outlook if it was created by this code
 ' If IsCreated Then OutlApp.Quit

  ' Release the memory of object variable
  'Set OutlApp = Nothing

End Sub

Donc je suppose que tu n'as pas les droits.

Essaie dans un autre répertoire où tu es sûr d'avoir tous les droits.

Mias je suis dans mon Username, je suis connecté avec mon nom d'utilisateur.

Je ne comprends pas cette histoire de "droits...

Rechercher des sujets similaires à "kill pdffile marche pas"