Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1396 connectés 

  FORUM HardWare.fr
  Programmation
  VB/VBA/VBS

  Macro copy/send qui fonctionnait sur XP et plus sur Windows 7

 


Je ne comprends pas?? sondage de...




Attention si vous cliquez sur "voir les résultats" vous ne pourrez plus voter

 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Macro copy/send qui fonctionnait sur XP et plus sur Windows 7

n°2285284
Coellophys​is
Posté le 18-07-2016 à 09:52:38  profilanswer
 

Bonjour à tous,
 
Voilà, pour le travail, j'avais une macro qui copiait un tableau et l'envoyait via Outlook mais depuis que j'ai migré de XP à Wds 7, elle fonctionne plus. Le bug arrive au moment où le tableau doit être copié.  Je reçois un message plutôt général comme "method or data member not found".
 
J'ai essayé de chercher des pistes sur le net mais je ne trouve pas, je vous remercie pour votre aide.
 
Apparemment il y a des balises C/C pour les codes mais honnêtement, je ne comprends pas de quoi il s'agit, si vous m'expliquez, je modifierai le codes dans le format désiré.
 
Voici le code:
 

Code :
  1. Option Explicit
  2. Sub CreateEmail()
  3. 'create picture files
  4. Dim rng As Range
  5. Dim strFolder As String
  6. strFolder = ActiveWorkbook.path & "\"
  7. Set rng = Sheet5.Range("v_report" )
  8. If Not ExportRangeToPicture(rng, strFolder & "profile_vol.png" ) Then
  9.     MsgBox "Error while creating png files"
  10.     GoTo ExitProc
  11. End If
  12. 'prepare email
  13. Dim strHTML As String
  14. Dim myOlApp As Variant, myItem As Variant, olMailItem As Variant
  15. Set myOlApp = CreateObject("Outlook.Application" )
  16. Set myItem = myOlApp.CreateItem(olMailItem)
  17. myItem.Subject = Sheet5.Range("em_subject" ).Value
  18. myItem.To = Sheet5.Range("em_to" ).Value
  19. myItem.CC = Sheet5.Range("em_cc" ).Value
  20. 'myItem.Importance = Outlook.OlImportance.olImportanceLow
  21. myItem.Attachments.Add strFolder & "profile_vol.png"
  22. strHTML = "<html><img src='profile_vol.png'><br><br>"
  23. strHTML = strHTML + "</html>"
  24. myItem.HTMLBody = strHTML
  25. myItem.Display
  26. ExitProc:
  27. End Sub
  28. Function ExportRangeToPicture(rng As Excel.Range, img As String) As Boolean
  29. ' save a range from Excel as a picture
  30. ' rng = Range to export
  31. ' img = filename & path
  32. ' basic error checking
  33. ' check for valid filetypes
  34. ' from http://peltiertech.com/WordPress/e [...] mage-file/
  35. 'Const FILE_EXT As String = "gif,png,jpg,jpe,jpeg"
  36. 'If InStr(FILE_EXT, LCase$(Right$(img, 3))) = 0 Then
  37. 'GoTo ExitProc
  38. 'End If
  39. ' check for valid path
  40. 'Dim path As String
  41. 'path = Left(img, InStrRev(img, "\" ))
  42. 'If Dir(path, vbDirectory) = "" Then GoTo ExitProc
  43. ' check for valid range
  44. Dim rRng As Range
  45. On Error Resume Next
  46. Set rRng = rng
  47. On Error GoTo 0
  48. If rRng Is Nothing Then GoTo ExitProc
  49. ' check for protected worksheet
  50. If ActiveSheet.ProtectContents Then GoTo ExitProc
  51. ' copy range to picture, put into chart, export it
  52. 'Application.ScreenUpdating = False
  53. rRng.CopyPicture xlScreen, xlBitmap
  54. Dim cht As ChartObject
  55. Set cht = Sheet1.ChartObjects.Add(0, 0, rRng.Width + 1, rRng.Height + 1)
  56. With cht
  57. .Chart.ChartArea.Border.LineStyle = xlLineStyleNone
  58. .Chart.PasteSpecial.xlPasteValues
  59. .Chart.Export img, Filtername:="png"
  60. .Delete
  61. End With
  62. ' if we got this far, assume success
  63. ExportRangeToPicture = True
  64. ExitProc:
  65. 'Application.ScreenUpdating = True
  66. Set cht = Nothing
  67. Set rRng = Nothing
  68. End Function


 
Bien à vous


Message édité par Coellophysis le 19-07-2016 à 09:40:35
mood
Publicité
Posté le 18-07-2016 à 09:52:38  profilanswer
 

n°2285356
Chelmi18
Posté le 19-07-2016 à 08:45:45  profilanswer
 

Salut,
 
Les balises [ cpp ][ /cpp ] s'utilisent très simplement : Tu mets ton code entre les deux et c'est tout.
 
Exemple :  [ cpp ]Toto[ /cpp ]  affichera
 

Code :
  1. Toto


 
Sinon, pour ton problème qu'appelle-tu le moment où le tableau doit être copié ?
C'est l'instruction rRng.CopyPicture xlScreen, xlBitmap qui plante ?
 
Si oui, est-ce que ca marche en pas à pas (Débogage puis F8) ?

n°2285364
Coellophys​is
Posté le 19-07-2016 à 09:42:41  profilanswer
 

Bonjour Chelmi18,
 
Je te remercie pour ton explication cpp /cpp, j'ai pu édité le message.
 
Pour le code, non il ne fonctionne pas étape par étape via le F8, lors du bug, la surbrillance se trouve ici ".Chart.PasteSpecial.xlPasteValues"  
 
Bien à toi.
 

n°2285370
Chelmi18
Posté le 19-07-2016 à 13:09:32  profilanswer
 

Je ne comprends pas bien ce que tu cherches à faire : un objet Chart est un graphique pas une image.
Je ne suis donc pas sûr que tu puisses agir sur un graphique avec l'instruction .PasteSpecial.xlPasteValues.
 
Si tu cherches juste à sauvegarder un graphique déjà existant en tant qu'image, tu peux essayer en enlevant cette instruction je pense.
Sinon, essaye en passant plutôt par un objet de type Shape qui sera plus approprié à mon avis.
 
 
 

n°2285374
Coellophys​is
Posté le 19-07-2016 à 14:34:51  profilanswer
 

Mon but est de copier un chart vers un email, cette méthode a fonctionné pendant 2 ans jusqu'à je migre vers Windows 7.

n°2285393
Chelmi18
Posté le 20-07-2016 à 09:13:24  profilanswer
 

Dans ce cas, remplace la ligne 80
 

Code :
  1. .Chart.PasteSpecial.xlPasteValues


 
par
 

Code :
  1. .Chart.Paste

n°2285396
Coellophys​is
Posté le 20-07-2016 à 09:57:01  profilanswer
 

Bonjour,
 
Ca n'a pas fonctionné mais c'était bien tenté.

n°2285412
Chelmi18
Posté le 20-07-2016 à 13:15:26  profilanswer
 

C'est curieux, cela a fonctionné nickel chez moi ?!
 
J'avais bien une image de mon range dans le mail (Windows 7, Excel 2010).
 
Ça fait quoi chez toi ? même erreur ?

n°2285432
Coellophys​is
Posté le 20-07-2016 à 15:45:12  profilanswer
 

Voici ce que je reçois comme message d'erreur:
 
Run time error '1004': Method Paste of Object_Chart failed
 
bien à toi

n°2285454
Chelmi18
Posté le 21-07-2016 à 08:21:34  profilanswer
 

Apparemment, ce problème semble se produire si la copy n'a pas fonctionné comme prévue, par exemple si le Range copié déborde de la partie visible de l'écran.
Est-ce ton cas ?
 
Si oui, regarde la solution expliquée là : http://stackoverflow.com/questions [...] creen-area
Notamment la partie ZoomToRange.
 
Hope this helps.


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  VB/VBA/VBS

  Macro copy/send qui fonctionnait sur XP et plus sur Windows 7

 

Sujets relatifs
piloter la fenetre windows en vbamacro
[VB/VBA/VBS] Macro excel d’impression sur plusieurs feuilles.Macro générer fichier excel avec info dans l'ordre
Script windows, help pleasemacro impression excel sous condition
Macro excel - décocher toutes les cases d'un filtre sauf uneMacro Excel - Importation fichier htm
Macro Exel 
Plus de sujets relatifs à : Macro copy/send qui fonctionnait sur XP et plus sur Windows 7


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR