Erreur d’exécution 91

Bonjour,

J'ai fait un post précédemment dont j'ai trouvé la solution de mon problème quelques temps après.

Or là je bloque vraiment...

Le but est d'afficher le graph présent dans mon fichier actuellement ouvert sur Excel et de l'exporter sous forme d'image directement sur Word.

Voici mon code

Sub Export_Chart_Word()

    'Name of an existing Word document, and the name the chart will have when exported.
    Const stWordDocument As String = "C:\Users\xxxx\Documents\ChartReport.docx"
    Const stChartName As String = "ChartReport.png"

    'Word objects.
    Dim wdApp As Word.Application
    Dim wdDoc As Word.Document
    Dim wdbmRange As Word.Range

    'Excel objects.
    Dim wbBook As Workbook
    Dim wsSheet As Worksheet
    Dim ChartObj As ChartObject

    'Initialize the Excel objects.
    Set wbBook = ThisWorkbook
    Set wsSheet = wbBook.Worksheets("Feuil1")
    Set ChartObj = wsSheet.ChartObjects("Graphique 1")

    'Turn off screen updating.
    Application.ScreenUpdating = False

    'Export the chart to the current directory, using the specified name, and save the chart as a .gif
    ChartObj.Chart.Export _
                   Filename:=wbBook.Path & "C:\Users\xxxx\Documents" & stChartName, _
                   FilterName:="PNG"

    'Initialize the Word objects to the existing Word document and bookmark.
    Set wdApp = New Word.Application
    Set wdDoc = wdApp.Documents.Open(wbBook.Path & "C:\Users\xxxx\Documents" & stWordDocument)
    Set wdbmRange = wdDoc.Bookmarks("ChartReport").Range

    'If there is already an inline shape, that means the macro has been run before - clean up any artifacts.
    On Error Resume Next
    With wdDoc.InlineShapes(1)
        .Select
        .Delete
    End With
    On Error GoTo 0

    'Add the .gif file to the document at the bookmarked location,
    'and ensure that it is saved inside the Word doc.
    With wdbmRange
        .Select
        .InlineShapes.AddPicture _
        Filename:=wbBook.Path & "C:\Users\xxxx\Documents" & stChartName, _
        LinkToFile:=False, _
        savewithdocument:=True
    End With

    'Save and close the Word document.
    With wdDoc
        .Save
        .Close
    End With

    'Quit Word.
    wdApp.Quit

    'Clear the variables.
    Set wdbmRange = Nothing
    Set wdDoc = Nothing
    Set wdApp = Nothing

    'Delete the temporary .gif file.
    On Error Resume Next
    Kill wbBook.Path & "C:\Users\xxxx\Documents" & stChartName
    On Error GoTo 0

    MsgBox "Chart exported successfully to " & stWordDocument

End Sub

Et le problème survient à cette ligne :

    Set wdbmRange = wdDoc.Bookmarks("ChartReport").Range 

"Erreur d’exécution 91 Variable objet ou variable de bloc with non définie"

Merci

Bonjour,

je pense que le problème vient de cette ligne

Set wdDoc = wdApp.Documents.Open(wbBook.Path & "C:\Users\xxxx\Documents" & stWordDocument)

tu renseignes 2 fois le répertoire

Bonjour i20100,

Merci pour ta réponse,

Mais malheureusement ça n'a pas résolu mon problème, j'ai toujours la même erreur...

Avez-vous d'autre piste ?

Merci

salut,

t'as testé comme ca?

Set wdDoc = wdApp.Documents.Open(stWordDocument)

Oui ça fonctionne !!! Merci beaucoup.

J'aurais une seconde demande (sans abuser de votre temps), j'aimerais maintenant pouvoir récupérer de la même manière, tous les fichiers qui commence par le caractère "&" et qui ont la même couleur.

J'avais crée ce code :

 For i = 1 To exl.Sheets.Count
        spl = Split(exl.Sheets(i).Name, "&")
        If exl.Sheets(i).Name <> spl(0) Then
            j = 0
            trouve = 0
            Do While trouve = 0
                j = j + 1
                If j = 7 Then GoTo skyp
                    If exl.Sheets(i).Tab.ColorIndex = couleur(j) Then
                        trouve = 1
                    End If
            Loop
            exl.Sheets(i).Select
            exl.Sheets(i).ChartObjects(1).Activate 'Activate
            nom_graphe = exl.ActiveChart.ChartTitle.text
            Print #j, nom_graphe + " = " + exl.Sheets(i).Name
        End If

Mais je n'arrive pas à l'adapter à celui-là.

Merci encore.

Re,

il faut changer peut être cette ligne

If exl.Sheets(i).Name <> spl(0) Then

comme ca

If exl.Sheets(i).Name = spl(0) Then

@++

Je pense que ça va être plus compliqué que ça,

Dans l'idée, j'aimerais remplacer cette ligne :

Set ChartObj = wsSheet.ChartObjects("Graphique 1")

Par une ligne permettant de récupérer toutes les figures ou graph, commençant par "&".

Merci pour votre aide.

Rechercher des sujets similaires à "erreur execution"