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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| Sub ET_Excel_to_word()
On Error Resume Next
Dim myFile$, n As Byte, nn As Byte, appWrd As Word.Application, wrdDoc As Word.Document, wrdFullName As String, MyPath, Chemin$, TemplateName$
Application.ScreenUpdating = False
TemplateName = "Pied de page.docx" ' Nom du modèle
Chemin = "\\srv-dom\Commun\Transfert - Partage\Anthony - Ne pas supprimer\Devis\" ' Sous-répertoire où se trouve le modèle
MyPath = Dir(Chemin & TemplateName)
If MyPath = "" Then
Chemin = VBA.Environ("UserProfile") & "\desktop\"
End If
MyPath = Dir(Chemin & TemplateName)
If MyPath = "" Then
Set appWrd = CreateObject("Word.Application")
appWrd.Visible = True
Set wrdDoc = appWrd.Documents.Add
MsgBox "Attention les pieds de page n'ont pas été chargés"
GoTo ajoutpage
End If
wrdFullName = Chemin & TemplateName
Set appWrd = CreateObject("Word.Application")
With appWrd
Set wrdDoc = .Documents.Add(Template:=wrdFullName) ' Ouvre un nouveau docment basé sur un modèle
.Visible = True ' Rend visible l'application
.Activate ' Active l'application
End With
ajoutpage:
For n = 1 To 3
If exist("En_tête", "page_" & Format(n, "00")) Then
ThisWorkbook.Worksheets("En_tête").Range("page_" & Format(n, "00")).CopyPicture Appearance:=xlScreen, Format:=xlPicture
With appWrd.Selection
nn = wrdDoc.InlineShapes.Count + 1
While wrdDoc.InlineShapes.Count < nn: DoEvents: .Paste: Wend 'en attente de l'exécution
.InsertBreak Type:=6
End With
'.PasteSpecial Link:=True, DataType:=wdPasteBitmap, Placement:=wdInLine, DisplayAsIcon:=False
'.PasteSpecial Link:=True, DataType:=wdPasteEnhancedMetafile, Placement:=wdInLine, DisplayAsIcon:=False
End If
Next
For n = 1 To 15
If exist("Descriptif", "page_" & Format(n, "00")) Then
ThisWorkbook.Worksheets("Descriptif").Range("page_" & Format(n, "00")).CopyPicture Appearance:=xlScreen, Format:=xlPicture
With appWrd.Selection
nn = wrdDoc.InlineShapes.Count + 1
While wrdDoc.InlineShapes.Count < nn: DoEvents: .Paste: Wend 'en attente de l'exécution
.InsertBreak Type:=6
End With
End If
Next
For n = 1 To 5
If exist("Carac_tech", "page_" & Format(n, "00")) Then
ThisWorkbook.Worksheets("Carac_tech").Range("page_" & Format(n, "00")).CopyPicture Appearance:=xlScreen, Format:=xlPicture
With appWrd.Selection
nn = wrdDoc.InlineShapes.Count + 1
While wrdDoc.InlineShapes.Count < nn: DoEvents: .Paste: Wend 'en attente de l'exécution
.InsertBreak Type:=6
End With
End If
Next
ThisWorkbook.Worksheets("CGV").Range("CGV").CopyPicture Appearance:=xlScreen, Format:=xlPicture
With appWrd.Selection
nn = wrdDoc.InlineShapes.Count + 1
While wrdDoc.InlineShapes.Count < nn: DoEvents: .Paste: Wend 'en attente de l'exécution
End With
'newObj.Sections(1).Footers(1).PageNumbers.Add (1) 'option 1
'appWrd.Sections(1).Footers(wdHeaderFooterPrimary).PageNumbers.AddPageNumberAlignment:=wdAlignPageNumberRight 'option 2
'MonChemin = VBA.Environ("UserProfile") & "\AppData\Roaming\Microsoft\Document Building Blocks\1036\16\Building Blocks.dotx" 'option 3
'appWrd.ActivePane.View.SeekView = wdSeekCurrentPageFooter
'appWrd.Templates(MonChemin).BuildingBlockEntries("MCTM_PDP").Insert Where:=Selection.Range, RichText:=True
Application.CutCopyMode = False
myFile = Replace(ActiveWorkbook.Name, "xlsm", "docx") 'remplacer "docx" par l'extension qui convient, si nécessaire
wrdDoc.SaveAs Filename:=Application.ActiveWorkbook.Path & "\" & myFile
appWrd.Activate
Set appWrd = Nothing
Set wrdDoc = Nothing
Application.ScreenUpdating = True
MsgBox "Export vers Word terminé", vbInformation + vbOKOnly, "Export vers Word"
End Sub |
Partager