Erreur d'éxécution '462'

pat01200

XLDnaute Occasionnel
Bonjour le forum,

Dans un fichier Excel, j'ai écrit une macro pour générer un document word. En voici le code :
Code:
Sub CréerRapport()
    Application.ScreenUpdating = False

    Pat = ActiveWorkbook.Name
    
    Dim appWrd As Word.Application
    Dim docWrd As Word.Document
    Set appWrd = New Word.Application
    appWrd.Visible = False
    Set docWrd = appWrd.Documents.Add

    With docWrd
    .PageSetup.LeftMargin = CentimetersToPoints(1)
    .PageSetup.TopMargin = CentimetersToPoints(1)
    .PageSetup.BottomMargin = CentimetersToPoints(1)
    .PageSetup.RightMargin = CentimetersToPoints(1)
    End With

    Windows(Pat).Activate
    Range("B2:AE51").Select
    Selection.Copy
    With appWrd.Selection
    .Paste
    .InsertBreak Type:=wdSectionBreakNextPage
    End With

    Windows(Pat).Activate
    Range("B53:AE107").Select
    Selection.Copy
    With appWrd.Selection
    .Paste
    .InsertBreak Type:=wdSectionBreakNextPage
    End With

    Range("B109:AE160").Select
    Selection.Copy
    Range("L13").Select
    With appWrd.Selection
    .Paste
    End With

    Application.CutCopyMode = False
    docWrd.SaveAs Filename:="Rapport_de_" & ActiveCell.Text & ".doc", FileFormat:=wdFormatDocument
    Set docWrd = Nothing
    appWrd.Quit
    Set appWrd = Nothing

    Windows(Pat).Activate
    Range("AH9").Select
    Application.ScreenUpdating = True

    MsgBox " Ce rapport a bien été enregistré ! "
End Sub
Lorsque j'exécute cette macro une fois, tout se passe bien, mais lorsque je veux générer un 2e rapport, là ça bogue et j'ai droit au message d'erreur suivant :
"Erreur d'éxécution '462' Le serveur distant n'existe pas ou n'est pas disponible"
En choisissant le débogage, la ligne surlignée en jaune est :
.PageSetup.LeftMargin = CentimetersToPoints(1)

Quelqu'un peut-il me venir en aide ? Merci d'avance...
 

pat01200

XLDnaute Occasionnel
Re : Erreur d'éxécution '462'

C'est bon, je viens de trouver... et pour tous ceux qui seraient confrontés au même problème, voici la correction apportée au code pour que tout fonnctionne :
With docWrd
.PageSetup.LeftMargin = appWrd.CentimetersToPoints(1)
.PageSetup.TopMargin = appWrd.CentimetersToPoints(1)
.PageSetup.BottomMargin = appWrd.CentimetersToPoints(1)
.PageSetup.RightMargin = appWrd.CentimetersToPoints(1)
End With
 
Dernière édition:

Discussions similaires

Statistiques des forums

Discussions
312 219
Messages
2 086 369
Membres
103 198
dernier inscrit
CACCIATORE