XL 2021 Export d'un Userform au format Docx;

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

jeff1494

XLDnaute Occasionnel
Bonsoir à toutes et tous;

Je suis en train de me faire une petite boite à outils me permettant d'avoir des informations pour m'aider à monter une sorte de documentation du projet VBE.
Mon idée est de regrouper des informations du projet me permettant de monter une documentation cohérente du projet.

J'ai créé quelques macros pour me fournir des éléments, qui me serviront à créer de la doc, comme par exemple :
  1. Liste de tous les formulaires du projet.
  2. Liste des contrôles par formulaire.
  3. Liste des feuilles avec les attributs "masquée, protégée".
  4. Liste des noms de cellules et tableaux du classeur.
J'aimerai aussi pouvoir créer au format Word '.Docx' une documentation sur les formulaires. Pouvoir insérer dans un fichier ".docx" une capture des formulaires, et ainsi pouvoir documenter chaque contrôle par exemple.

J'ai trouvé une macro créée par @kiki29 il y a déjà un bon moment (Pris dans son récap des possibilités de jouer avec les PDF et Excel dont voici le lien).

Mon problème est qu'il exporte la capture d'écran au format PDF. Cela fonctionne parfaitement, mais je voudrais bien, en fait, sauvegarder la capture comme image dans un fichier Word.
J'ai bien sûr essayer de voir le code et si je pouvais le modifier. Mais le Hic est que Kiki29 a utilisé des concepts que je ne connais pas. Du genre "Const VK_SNAPSHOT = 44" voire " keybd_event VK_LMENU, 0, KEYEVENTF_EXTENDEDKEY, 0" et d'autres.

Donc j'avoue humblement que d'une part j'aimerais bien comprendre ce genre d'instruction (L'aide sur les "Constantes KeyCode" m'a permis de comprendre l'idée générale, mais c'est tout.).

Je vous joins un fichier exemple très simple qui utilise la macro de Kiki29, et qui marche parfaitement bien.
Si une personne charitable pouvait :
  1. M'expliquer le code de kiki29.
  2. M'aider à créer une macro qui ferait le même genre de chose, à savoir:
    1. Prendre un "cliché" du formulaire au format Bmp, ou autre (.jpg, ...). Le format n'est une contrainte pour autant qu'il puisse être inclus dans un document.
    2. Pouvoir créer un document Word (format .docx) qui contiendrait ce cliché du formulaire. Le doc sera créé dans le répertoire du classeur, et avec le nom du formulaire.
Je vous joins un petit fichier qui m'a permis de tester la macro de kiki29. Il est minimaliste, juste un formulaire avec quelques contrôles bidons, et 3 boutons.
Un pour l'export vers PDF = "CommandButton1", le deuxième "CommandButton2" qui ne sert à rien d'autre que d'exister, et un troisième "CommandButton3" qui me servirait à créer le fameux document Word;

Si une âme charitable pouvait m'aider je lui en serais reconnaissant.
En attendant je vous souhaite à tous une bonne soirée.
 

Pièces jointes

Solution
bonsoir jeff
VB:
Private Sub CommandButton3_Click()
    PrintScreen
    DoEvents
    Dim wdApp As Object, wdDoc As Object
    ' creation de l'object application word
    Set wdApp = CreateObject("Word.Application")
    wdApp.Visible = True
    ' on créé un document
    Set wdDoc = wdApp.Documents.Add
    ' on colle la capture dans le document
    wdApp.Selection.Paste
    'on centre le tout
    wdApp.Selection.ParagraphFormat.Alignment = 1 ' 0=Left, 1=Center, 2=Right
    ' enregistrement du document
    wdDoc.SaveAs2 Environ("userprofile") & "\desktop\mon userform.docx"
    'on ferme
    wdDoc.Close
    wdApp.Quit
    MsgBox "enregistrement de la capture dans un fichier docw(word) terminé"
End Sub
Au post #27 j'ai modifié cette ligne :
VB:
With Wapp.Selection.ParagraphFormat: .SpaceAfter = .SpaceAfter + Wdoc.Shapes(1).Height - .LineSpacing: End With
Je n'ai pas de problème chez moi avec Wdoc.Shapes(1) et j'obtiens ceci :
1765277197588.png
 
Patrick, tu peux peut-être essayer en remplaçant Wdoc.Shapes(1) par Wdoc.Shapes.Range(1) :
VB:
    Wdoc.Shapes.Range(1).Left = (Wdoc.PageSetup.PageWidth - Wdoc.Shapes.Range(1).Width) / 2 - Wdoc.PageSetup.LeftMargin 'centrage
    With Wapp.Selection.ParagraphFormat: .SpaceAfter = .SpaceAfter + Wdoc.Shapes.Range(1).Height - .LineSpacing: End With
Chez moi ça fonctionne bien aussi.
 
Alors essaie maintenant avec ShapeRange, ça va bien chez moi :
VB:
Private Sub CommandButton1_Click()
Dim Wapp As Object, Wdoc As Object
 
    Application.ScreenUpdating = False
    PrintScreen
    DoEvents
   
    Set Wapp = CreateObject("Word.Application")
    Wapp.Visible = True
    Set Wdoc = Wapp.Documents.Add
    Wapp.Selection.Range.Text = "Ci-dessous la copie d'écran" & vbCrLf & vbCrLf & "Fin de la copie d'écran" '3 paragraphes
    Wdoc.Paragraphs(2).Range.Select
    Wapp.Selection.Paste
    Wapp.Selection.ShapeRange.Left = (Wdoc.PageSetup.PageWidth - Wapp.Selection.ShapeRange.Width) / 2 - Wdoc.PageSetup.LeftMargin 'centrage
    With Wapp.Selection.ParagraphFormat: .SpaceAfter = .SpaceAfter + Wapp.Selection.ShapeRange.Height - .LineSpacing: End With

    Wdoc.SaveAs ActiveWorkbook.Path & "\UserForm.docx"
    Wdoc.ExportAsFixedFormat ActiveWorkbook.Path & "\UserForm.pdf", 17 'wdExportFormatPDF
    Wdoc.Close False
    Wapp.Quit
    MsgBox "Les fichiers 'UserForm.docx' et 'UserForm.pdf' ont été créés..."
    Unload Me
End Sub
 

Pièces jointes

Dernière tentative, teste de nouveau les fichiers des posts #27 et #34 en remplaçant Wapp.Selection.Paste par :
VB:
Wapp.Selection.PasteSpecial 4 'wdPasteBitmap
Cela va bien chez moi mais je doute que ça change quelque chose chez toi.
 
comme je t'ai dit mais visiblement tu ne m'a pas compris
ches moi le shapes.count reste a zero
donc tu peux essayer shapes.ranges ou shaperange ou shape quoi que ce soit ca plantera toujours au premier shape.quelque chose puisque shape est nothing
j'espere avoir été plus clair
1765310073520.png
 
J'ai bien compris que Shapes.Count peut-être égal à zéro, c'est pour ça que je fais une boucle.

Il faudrait d'ailleurs sortir le Paste de la boucle.

Mais si Shapes.Count reste à zéro c'est que ce n'est pas une Shape qui est créée, alors c'est quoi ?
 
Bonjour jeff1494, Patrick, le forum,
je confirme c'est un inlineshapes j'ai testé
Alors voici une solution qui devrait fonctionner dans les 2 cas, Shape ou InlineShape :
VB:
Private Sub CommandButton1_Click()
Dim Wapp As Object, Wdoc As Object, Sh
 
    Application.ScreenUpdating = False
    PrintScreen
    DoEvents
    
    Set Wapp = CreateObject("Word.Application")
    Wapp.Visible = True
    Set Wdoc = Wapp.Documents.Add
    Wapp.Selection.Range.Text = "Ci-dessous la copie d'écran" & vbCrLf & vbCrLf & "Fin de la copie d'écran" '3 paragraphes
    Wdoc.Paragraphs(2).Range.Select
    Wapp.Selection.Paste
    Do: DoEvents: Loop Until Wdoc.Shapes.Count + Wdoc.InlineShapes.Count > 0 'en attente du collage
    If Wdoc.Shapes.Count Then Set Sh = Wdoc.Shapes(1): _
        Sh.Left = (Wdoc.PageSetup.PageWidth - Sh.Width) / 2 - Wdoc.PageSetup.LeftMargin 'centrage
    If Wdoc.InlineShapes.Count Then Set Sh = Wdoc.InlineShapes(1): _
        Wapp.Selection.ParagraphFormat.Alignment = 1 'centrage avec le texte
    With Wapp.Selection.ParagraphFormat: .SpaceAfter = .SpaceAfter + Sh.Height - .LineSpacing: End With

    Wdoc.SaveAs ActiveWorkbook.Path & "\UserForm.docx"
    Wdoc.ExportAsFixedFormat ActiveWorkbook.Path & "\UserForm.pdf", 17 'wdExportFormatPDF
    Wdoc.Close False
    Wapp.Quit
    MsgBox "Les fichiers 'UserForm.docx' et 'UserForm.pdf' ont été créés..."
    Unload Me
End Sub
A+
 

Pièces jointes

Bonjour à toutes et tous;

@patricktoulon : Merci pour tes explications concernant "VK_SNAPSHOT" et autres choses du genre. Peut-être cela serait en utilisant un de ces "VK_" de lancer la sauvegarde au format ".docx" au lieu d'avoir un bouton. Ceci dans l'optique de sauvegarder le formulaire sans que ce bouton "parasite" (dans le sens qu'il n'apparaitra pas dans la réalité lors de l'utilisation du formulaire).
Juste choisir judicieusement la touche totalement improbable pour un utilisateur lambda.

Aurais-tu un lien à me conseiller pour accéder aux détails de chacun de ces "VK_" ? Même si mon idée d'utiliser ce genre de choses n'est pas réaliste, cela me permettra d'apprendre et de comprendre. Ton niveau de détail pour chaque élément est suffisant pour ma compréhension.

@job75 : Merci pour le temps que tu passes sur ce sujet, j'apprécie grandement car cela me fais comprendre qu'il y a toujours plusieurs solutions à un problème.
Dans mon cas présent je me suis arrêté sur la solution de @patricktoulon .
Mais je garde précieusement tous vos échanges au cas où.

Dans tous les cas merci à vous deux pour votre aide.
Bonne journée à toutes et tous.
 
Bonjour @job75
1765357025023.png

@jeff1494
VB:
'---------------------------------------------------------
' TOUCHES ALPHABÉTIQUES
'---------------------------------------------------------
Const VK_A = &H41
Const VK_B = &H42
Const VK_C = &H43
Const VK_D = &H44
Const VK_E = &H45
Const VK_F = &H46
Const VK_G = &H47
Const VK_H = &H48
Const VK_I = &H49
Const VK_J = &H4A
Const VK_K = &H4B
Const VK_L = &H4C
Const VK_M = &H4D
Const VK_N = &H4E
Const VK_O = &H4F
Const VK_P = &H50
Const VK_Q = &H51
Const VK_R = &H52
Const VK_S = &H53
Const VK_T = &H54
Const VK_U = &H55
Const VK_V = &H56
Const VK_W = &H57
Const VK_X = &H58
Const VK_Y = &H59
Const VK_Z = &H5A

'---------------------------------------------------------
' TOUCHES CHIFFRES (LIGNE DU HAUT)
'---------------------------------------------------------
Const VK_0 = &H30
Const VK_1 = &H31
Const VK_2 = &H32
Const VK_3 = &H33
Const VK_4 = &H34
Const VK_5 = &H35
Const VK_6 = &H36
Const VK_7 = &H37
Const VK_8 = &H38
Const VK_9 = &H39

'---------------------------------------------------------
' TOUCHES FONCTION F1–F24
'---------------------------------------------------------
Const VK_F1 = &H70
Const VK_F2 = &H71
Const VK_F3 = &H72
Const VK_F4 = &H73
Const VK_F5 = &H74
Const VK_F6 = &H75
Const VK_F7 = &H76
Const VK_F8 = &H77
Const VK_F9 = &H78
Const VK_F10 = &H79
Const VK_F11 = &H7A
Const VK_F12 = &H7B
Const VK_F13 = &H7C
Const VK_F14 = &H7D
Const VK_F15 = &H7E
Const VK_F16 = &H7F
Const VK_F17 = &H80
Const VK_F18 = &H81
Const VK_F19 = &H82
Const VK_F20 = &H83
Const VK_F21 = &H84
Const VK_F22 = &H85
Const VK_F23 = &H86
Const VK_F24 = &H87

'---------------------------------------------------------
' TOUCHES DE CONTRÔLE
'---------------------------------------------------------
Const VK_BACK = &H8
Const VK_TAB = &H9
Const VK_CLEAR = &HC
Const VK_RETURN = &HD
Const VK_SHIFT = &H10
Const VK_CONTROL = &H11
Const VK_MENU = &H12           'ALT (gauche ou droite)
Const VK_PAUSE = &H13
Const VK_CAPITAL = &H14        'CAPS LOCK

Const VK_ESCAPE = &H1B
Const VK_SPACE = &H20
Const VK_PRIOR = &H21          'PAGE UP
Const VK_NEXT = &H22           'PAGE DOWN
Const VK_END = &H23
Const VK_HOME = &H24
Const VK_LEFT = &H25
Const VK_UP = &H26
Const VK_RIGHT = &H27
Const VK_DOWN = &H28
Const VK_SELECT = &H29
Const VK_PRINT = &H2A
Const VK_EXECUTE = &H2B
Const VK_SNAPSHOT = &H2C       'PRINT SCREEN
Const VK_INSERT = &H2D
Const VK_DELETE = &H2E

'---------------------------------------------------------
' TOUCHES NUMPAD
'---------------------------------------------------------
Const VK_NUMLOCK = &H90
Const VK_SCROLL = &H91
Const VK_NUMPAD0 = &H60
Const VK_NUMPAD1 = &H61
Const VK_NUMPAD2 = &H62
Const VK_NUMPAD3 = &H63
Const VK_NUMPAD4 = &H64
Const VK_NUMPAD5 = &H65
Const VK_NUMPAD6 = &H66
Const VK_NUMPAD7 = &H67
Const VK_NUMPAD8 = &H68
Const VK_NUMPAD9 = &H69
Const VK_MULTIPLY = &H6A
Const VK_ADD = &H6B
Const VK_SEPARATOR = &H6C
Const VK_SUBTRACT = &H6D
Const VK_DECIMAL = &H6E
Const VK_DIVIDE = &H6F

'---------------------------------------------------------
' TOUCHES SPÉCIALES
'---------------------------------------------------------
Const VK_LSHIFT = &HA0
Const VK_RSHIFT = &HA1
Const VK_LCONTROL = &HA2
Const VK_RCONTROL = &HA3
Const VK_LMENU = &HA4           'ALT gauche
Const VK_RMENU = &HA5           'ALT droite

Const VK_BROWSER_BACK = &HA6
Const VK_BROWSER_FORWARD = &HA7
Const VK_BROWSER_REFRESH = &HA8
Const VK_BROWSER_STOP = &HA9
Const VK_BROWSER_SEARCH = &HAA
Const VK_BROWSER_FAVORITES = &HAB
Const VK_BROWSER_HOME = &HAC
Const VK_VOLUME_MUTE = &HAD
Const VK_VOLUME_DOWN = &HAE
Const VK_VOLUME_UP = &HAF
Const VK_MEDIA_NEXT_TRACK = &HB0
Const VK_MEDIA_PREV_TRACK = &HB1
Const VK_MEDIA_STOP = &HB2
Const VK_MEDIA_PLAY_PAUSE = &HB3
Const VK_LAUNCH_MAIL = &HB4
Const VK_LAUNCH_MEDIA_SELECT = &HB5
Const VK_LAUNCH_APP1 = &HB6
Const VK_LAUNCH_APP2 = &HB7

'---------------------------------------------------------
' TOUCHES DE PONCTUATION
'---------------------------------------------------------
Const VK_OEM_1 = &HBA       ';:'
Const VK_OEM_PLUS = &HBB    '+'
Const VK_OEM_COMMA = &HBC   ','
Const VK_OEM_MINUS = &HBD   '-'
Const VK_OEM_PERIOD = &HBE  '.'
Const VK_OEM_2 = &HBF       '/?'
Const VK_OEM_3 = &HC0       '`~'
Const VK_OEM_4 = &HDB       '[{'
Const VK_OEM_5 = &HDC       '\|'
Const VK_OEM_6 = &HDD       ']}'
Const VK_OEM_7 = &HDE       ''"'
Const VK_OEM_8 = &HDF
 
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD

Discussions similaires

Réponses
16
Affichages
469
Réponses
5
Affichages
364
Réponses
7
Affichages
140
Retour