1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| Sub export_excel_to_word()
Dim obj As Object
Dim newObj As Object
Dim sh As Worksheet
Dim myRange, myFile
Set obj = CreateObject("Word.Application")
obj.Visible = True
Set newObj = obj.Documents.Add
For Each sh In ActiveWorkbook.Sheets
If sh.Index < 4 Then
sh.Activate
myRange = InputBox("Donnez la zone d'impression de la feuille " & sh.Name, "Zones à exporter vers Word")
If myRange = "" Then Exit For 'on a cliqué sur Cancel
If InStr(1, myRange, ":") = 0 Then MsgBox "Coordonnées de plage NON valide !", vbOKOnly, "Incorrect Range": Exit Sub
'on peut créer un loop sur la ligne ci-dessus
Range(myRange).Select
ActiveSheet.Range(myRange).Copy
With obj.Selection
.PasteSpecial Link:=False, DataType:=wdPasteEnhancedMetafile, _
Placement:=wdInLine, DisplayAsIcon:=False
.TypeParagraph
End With
Application.CutCopyMode = False
End If
Next sh
myFile = Replace(ActiveWorkbook.Name, "xlsm", "docx") 'remplacer "docx" par l'extension qui convient, si nécessaire
newObj.SaveAs Filename:=Application.ActiveWorkbook.Path & "\" & myFile
MsgBox "Export vers Word terminé", vbInformation + vbOKOnly, "Export vers Word"
obj.Activate 'vous pouvez jouer sur les marges pour améliorer la lecture
Set obj = Nothing
Set newObj = Nothing
End Sub |
Partager