Office: [VBA] - Kommentare aus Word in Excel schreiben

Helfe beim Thema [VBA] - Kommentare aus Word in Excel schreiben in Microsoft Excel Hilfe um das Problem gemeinsam zu lösen; Hallo zusammen, ich habe ein Makro, das die Kommentare aus Worddateien ausliest und in eine Excel-Tabelle (ab Zeile 4) einfügt. Dabei schreibe ich... Dieses Thema im Forum "Microsoft Excel Hilfe" wurde erstellt von Buggy, 17. Februar 2023.

  1. Buggy Erfahrener User

    [VBA] - Kommentare aus Word in Excel schreiben


    Hallo zusammen,

    ich habe ein Makro, das die Kommentare aus Worddateien ausliest und in eine Excel-Tabelle (ab Zeile 4) einfügt. Dabei schreibe ich verschiedene Dinge wie z.B. Textpassage, Kommentar, heutiges Datum etc. in die Tabelle.

    Allerdings bekomme einen Fehler bei folgender Zeile in meinem Code, der die Seitenzahl des Kommentars in Spalte 5 schreiben soll:

    Cells(rowNum, 5).Value = comment.Scope.Information(wdActiveStartPageNumber)

    Weiß einer von euch, was ich falsch mache?

    Viele Grüße


    Code:
    Sub ExtractCommentsFromWord()
    
        ' Declare variables
        Dim wdApp As Object
        Dim wdDoc As Object
        Dim comment As Object
        Dim rowNum As Integer
        Dim varFile As Variant
        Dim fd As Office.FileDialog
        
        
        Set fd = Application.FileDialog(msoFileDialogFilePicker)
        
        With fd
        
            .Filters.Clear
            .Filters.Add "Doc Files", "*.docx?", 1
            .Title = "Choose an Doc file"
            .AllowMultiSelect = True
        
            .InitialFileName = "C:\"
        
        End With
        
        If fd.Show = True Then
                
            ' Initialize row number for writing comments to Excel
            rowNum = 4
                
                
            For Each varFile In fd.SelectedItems
                      
                ' Create a new instance of Word application
                Set wdApp = CreateObject("Word.Application")
                ' Open the Word document
                Set wdDoc = wdApp.Documents.Open(varFile)
                    
                
                    ' Loop through all comments in the Word document
                    For Each comment In wdDoc.Comments
                    
                    
                        ' Write comment information to Excel sheet
                        Cells(rowNum, 1).Value = rowNum - 3 ' Count from 1 to n
                        Cells(rowNum, 2).Value = wdDoc.Name
                        Cells(rowNum, 6).Value = comment.Scope.FormattedText
                        Cells(rowNum, 7).Value = comment.Range.Text
                        Cells(rowNum, 8).Value = Date
                        Cells(rowNum, 5).Value = comment.Scope.Information(wdActiveStartPageNumber)
                        
                        rowNum = rowNum + 1
                        
                    Next comment
        
                ' Close the Word document and application
                wdDoc.Close
                wdApp.Quit
            
            Next varFile
            
        End If
        
        ' Release memory
        Set wdDoc = Nothing
        Set wdApp = Nothing
        
    
        
        MsgBox "Done"
        
    End Sub
     
  2. Exl121150 Erfahrener User
    Hallo,

    im Word.Application-Object gibt es keine vordefinierte Konstante namens wdActiveStartPageNumber.

    Aber selbst wenn es sie gäbe, würde sie nicht funktionieren, so wie du die Word.Application-Instanz erzeugst: Im Late-Binding-Verfahren musst du die Konstante explizit deklarieren/definieren mit "Const wdKonstantenname = Zahl oder String", damit der Compiler sie kennt - oder zumindest eine Referenz zB. mit "wdApp.wdKonstante" hinzufügen.
     
    Zuletzt bearbeitet: 18. Februar 2023
    Exl121150, 18. Februar 2023
    #2
  3. Buggy Erfahrener User
    Hallo Anton,

    danke. Leider bekomme ich es mit deinen Hinweisen nicht selbst hin. Hättest du mir bitte den passenden Code dafür?

    Mit Late-Binding oder Early-Binding habe ich mich noch nicht beschäftigt. Könnte ich meinen Code mit Early-Binding verbessern?

    viele Grüße
     
  4. Exl121150 Erfahrener User

    [VBA] - Kommentare aus Word in Excel schreiben

    Hallo,

    welche wdInformation-Konstanten es in Word tatsächlich gibt und was sie bedeuten, kannst du folgendem Microsoft-Hilfelink entnehmen:
    WdInformation-Enumeration (Word) | Microsoft Learn

    Eine wdInformation-Konstante, die tatsächlich existiert, ist zB.: wdActiveEndPageNumber, sodass die Problemzeile modifiziert dann lauten würde:
    Cells(rowNum, 5).Value = comment.Scope.Information(wdApp.wdActiveEndPageNumber)
     
    Exl121150, 20. Februar 2023
    #4
  5. Exl121150 Erfahrener User
    Hallo,
    Code:
    Option Explicit
    
    #Const IsEarlyBinding = False  '<-- Compilierung für Late-Binding ist eingeschaltet !!
    '#Const IsEarlyBinding = True
    
    Sub ExtractCommentsFromWord()
    
    #If IsEarlyBinding Then
    
        ' Declare variables
        ' Early-Binding
        ' Create a new instance of Word application AT COMPILE-TIME
        Dim wdApp As New Word.Application
        Dim wdDoc As Word.Document
        Dim comment As Word.comment
     
    #Else
    
        ' Declare variables
        ' Late-Binding
        Dim wdApp As Object
        Dim wdDoc As Object
        Dim comment As Object
        ' Create a new instance of Word application AT RUN-TIME
        Set wdApp = CreateObject("Word.Application")
     
    #End If
    
        Dim rowNum As Integer
        Dim varFile As Variant
        Dim fd As Office.FileDialog
     
     
        Set fd = Application.FileDialog(msoFileDialogFilePicker)
     
        With fd
     
            .Filters.Clear
            .Filters.Add "Doc Files", "*.docx?", 1
            .Title = "Choose an Doc file"
            .AllowMultiSelect = True
     
            .InitialFileName = "C:\"
     
        End With
     
        If fd.Show = True Then
             
            ' Initialize row number for writing comments to Excel
            rowNum = 4
             
             
            For Each varFile In fd.SelectedItems
                   
                ' Open the Word document
                Set wdDoc = wdApp.Documents.Open(varFile)
                 
             
                    ' Loop through all comments in the Word document
                    For Each comment In wdDoc.Comments
                 
                 
                        ' Write comment information to Excel sheet
                        Cells(rowNum, 1).Value = rowNum - 3 ' Count from 1 to n
                        Cells(rowNum, 2).Value = wdDoc.Name
                        Cells(rowNum, 6).Value = comment.Scope.FormattedText
                        Cells(rowNum, 7).Value = comment.Range.Text
                        Cells(rowNum, 8).Value = Date
                        Cells(rowNum, 5).Value = comment.Scope.Information(wdDoc.wdActiveEndPageNumber)
                     
                        rowNum = rowNum + 1
                     
                    Next comment
     
                ' Close the Word document and application
                wdDoc.Close
                wdApp.Quit
         
            Next varFile
         
        End If
     
        ' Release memory
        Set wdDoc = Nothing
        Set wdApp = Nothing
     
        MsgBox "Done"
     
    End Sub
    

    obiger VBA-Code ist so eingestellt, dass er im Late-Binding-Modus kompiliert wird. Stellst du jedoch die beiden Zeilen
    '#Const IsEarlyBinding = False
    #Const IsEarlyBinding = True '<-- Kompilierung für Early-Binding ist eingeschaltet !!

    um, wird bei der nächsten Kompilierung der VBA-Code fürs Early-Binding-Verfahren erzeugt.
    Voraussetzung, dass diese Kompilierung ohne Fehlermeldung abläuft, ist, dass du die Word-Library vor der Kompilierung eingebunden hast, wie folgt: Menü des VBA-Editors > Extras > Verweise ...
    [VBA] - Kommentare aus Word in Excel schreiben upload_2023-2-20_16-19-31.png
     
    Exl121150, 20. Februar 2023
    #5
Thema:

[VBA] - Kommentare aus Word in Excel schreiben

Die Seite wird geladen...
  1. [VBA] - Kommentare aus Word in Excel schreiben - Similar Threads - VBA Kommentare Word

  2. Excel VBA Codes Pivot-Aktualisierung, Kommentare, etc.

    in Microsoft Excel Hilfe
    Excel VBA Codes Pivot-Aktualisierung, Kommentare, etc.: Hallo liebe Office-Hilfe Mitglieder, ich bin seit kurzem hier neu registriert und hätte eine (für mich als absolutem Laien in Sachen VBA) Problemstellung, bei der mir hoffentlich jemand von Euch...
  3. Excel Kommentar schreibschützen (read-only) via VBA?

    in Microsoft Excel Hilfe
    Excel Kommentar schreibschützen (read-only) via VBA?: Hallo! Ich schreibe per VBA Infotexte in das Kommentar eines geänderten Feldes. Das Kommentar soll aber nicht von Hand gelöscht oder geändert werden können sondern nur von meiner VBA-Routine....
  4. Kommentar Popup entfernen

    in Microsoft Excel Hilfe
    Kommentar Popup entfernen: Hallo miteinander, Ich habe folgendes (Lästiges) Problem: Und zwar haben wir für unsere Arbeitszeit-/ Leistungserfassung eine Excel in dem wir unsere Tätigkeiten mit eintragen sollen....
  5. VBA: Erstellen/Editieren/Löschen von Kommentaren per VBA erlauben.

    in Microsoft Excel Hilfe
    VBA: Erstellen/Editieren/Löschen von Kommentaren per VBA erlauben.: Hallo. Bislang habe ich meine Tabellenblätter per VBA automatisch geschützt, da dies ganz gerne vergessen wird. Folgene Routine habe ich dafür verwendet: (Auskommentiert derzeit) 'Sub...
  6. Kommentar in Zellen per VBA

    in Microsoft Excel Hilfe
    Kommentar in Zellen per VBA: Hallo zusammen, ich habe eine Excel-Mappe die aus mehreren Tabellenreitern besteht. Ich habe euch mal eine vereinfachte Version angehängt. Ein Tabellenreiter "Übersicht" fasst quasi den Umsatz...
  7. Temporaere Anzeige eines Textes in einer Text oder Kommentar Box

    in Microsoft Excel Hilfe
    Temporaere Anzeige eines Textes in einer Text oder Kommentar Box: Hallo zusammen, ich moechte den aus max. 160 Zeichen bestehenden Text einer Excel Zelle erst komlett anzeigen wenn ich diese selektiere. Normal wuerde ich Text per Umbruch komlett darstellen,...
  8. pdf in kommentar via Makro (VBA)

    in Microsoft Excel Hilfe
    pdf in kommentar via Makro (VBA): Guten Morgen zusammen, ich habe im Internet ein Makro gefunden, womit ich Bilder in ein Kommentarfeld einfügen kann. Mein Problem ist allerdings, dass ich es gerne so erweitert haben möchte,...
  1. Diese Seite verwendet Cookies, um Inhalte zu personalisieren, diese deiner Erfahrung anzupassen und dich nach der Registrierung angemeldet zu halten.
    Auf dieser Website werden Cookies für die Zugriffsanalyse und Anzeigenmessung verwendet.
    Wenn du dich weiterhin auf dieser Seite aufhältst, akzeptierst du unseren Einsatz von Cookies.
    Information ausblenden