XL 2019 copier un tableau Excel dans un document word

Akafal50

XLDnaute Nouveau
Bonjour chers amis

Je voudrais copier un tableau Excel dans un nouveau fichier Word.

Je n’arrive pas à faire tenir toutes les colonnes sur le tableau alors que j’ai la possibilité d’ajuster les colonnes dans le tableau Word.

Je sollicite votre assistance pour faire figurer toutes les colonnes, quitte à les ajuster.

Je vous remercie pour votre assistance.
ci joints deux fichiers un en Excel et un en Word
 

Pièces jointes

  • TABLEAU EXCEL A TRANSFORMER EN WORD.xlsx
    184.8 KB · Affichages: 5
  • AJUSTER TABLEAU WORD A PARTIR EXCEL.docx
    15.3 KB · Affichages: 4

Staple1600

XLDnaute Barbatruc
Bonjour le fil

@Akafal50
En guise de départ, voir le code VBA ci-dessous
VB:
Sub ExcelRangeToWord()

'PURPOSE: Copy/Paste An Excel Table Into a New Word Document
'NOTE: Must have Word Object Library Active in Order to Run _
  (VBE > Tools > References > Microsoft Word 12.0 Object Library)
'SOURCE: www.TheSpreadsheetGuru.com

Dim tbl As Excel.Range
Dim WordApp As Word.Application
Dim myDoc As Word.Document
Dim WordTable As Word.Table

'Optimize Code
  Application.ScreenUpdating = False
  Application.EnableEvents = False

'Copy Range from Excel
  Set tbl = ThisWorkbook.Worksheets(1).Cells(1).CurrentRegion

'Create an Instance of MS Word
  On Error Resume Next

    'Is MS Word already opened?
      Set WordApp = GetObject(class:="Word.Application")

    'Clear the error between errors
      Err.Clear

    'If MS Word is not already open then open MS Word
      If WordApp Is Nothing Then Set WordApp = CreateObject(class:="Word.Application")

    'Handle if the Word Application is not found
      If Err.Number = 429 Then
        MsgBox "Microsoft Word could not be found, aborting."
        GoTo EndRoutine
      End If

  On Error GoTo 0

'Make MS Word Visible and Active
  WordApp.Visible = True
  WordApp.Activate

'Create a New Document
  Set myDoc = WordApp.Documents.Add

'Copy Excel Table Range
  tbl.Copy

'Paste Table into MS Word
  myDoc.Paragraphs(1).Range.PasteExcelTable _
    LinkedToExcel:=False, _
    WordFormatting:=False, _
    RTF:=False

'Autofit Table so it fits inside Word Document
  Set WordTable = myDoc.Tables(1)
  WordTable.AutoFitBehavior (wdAutoFitWindow)

EndRoutine:
'Optimize Code
  Application.ScreenUpdating = True
  Application.EnableEvents = True

'Clear The Clipboard
  Application.CutCopyMode = False

End Sub
Ici dans cet exemple, on créé un nouveau word
mais il est facile d'adapter pour que la copie se fasse sur un document Word existant

test OK sur Office 365

NB: juste mettre ensuite le document en mode Paysage
 

Discussions similaires

Réponses
4
Affichages
342
Réponses
3
Affichages
2 K

Statistiques des forums

Discussions
313 029
Messages
2 094 537
Membres
106 046
dernier inscrit
abdelhay