Zusätzliche Absatzmarke beim Einfügen eines Dokumentes/Kopfzeile verhindern

  • Word

Es gibt 1 Antwort in diesem Thema. Der letzte Beitrag () ist von Bodycount.

    Zusätzliche Absatzmarke beim Einfügen eines Dokumentes/Kopfzeile verhindern

    Hallo zusammen,

    über folgenden VBA-Code (in Excel verwendet) kopiere ich Inhalt (hier die Kopfzeile) aus einem bestehenden Dokument in ein neues Dokument:

    Visual Basic-Quellcode

    1. Option Explicit
    2. Dim objWord As Word.Application
    3. Dim MyDoc As Word.Document
    4. Dim MyRange As Word.Range
    5. Dim MyHeaderFooterSource As Word.Document
    6. Sub CreateWordInstance()
    7. Set objWord = GetObject(, "Word.Application") 'Used when Word is already started.
    8. Set MyDoc = objWord.ActiveDocument 'Add an active document to Word
    9. Set MyRange = MyDoc.Range 'Define the document as range to use
    10. Set MyHeaderFooterSource = objWord.Documents.Open("D:\HeaderFooterSample\Sample.dotx", Visible:=False) 'Open the defined temp file
    11. With objWord
    12. MyHeaderFooterSource.Sections(1).Headers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Copy 'Copy the header from the temp file
    13. MyRange.Sections(1).Headers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Paste 'Paste the header from the temp file into the active document
    14. End With
    15. MyHeaderFooterSource.Close (False) 'Close the temp file, but don't save it
    16. Set objWord = Nothing 'Release Word
    17. End Sub


    Die Kopfzeile des einzufügenden Dokumentes (Sample.dotx) hat nur eine einzige Zeile, also einen Absatz/Paragraph. Nach dem Einfügen in das neue Dokument sind dann jedoch zwei Absätze da. Das versuche ich zu verhindern, hab' aber noch keinen Anstaz für eine Umsetzung finden können.

    Mein Verdacht: die bestehende Absatzmarke wird um die eingefügte erweitert und dadurch bekommt man die zweite Absatzmarke. Das ist nun aber nur meine Spekulation. Wie kann ich denn den bestehenden Absatz komplett ersetzten, danstatt einen weiteren einzufügen? Oder wie kann ggf. die zweite Absatzmarke entfernt werden?

    Kann mir dazu bitte jemand mal einen Tipp in eine Richtung geben?

    ​Nachtrag: das Problem tritt im Übrigen nicht nur im Zusammenhang mit Kopf/Fußzeile auf. Auch wenn ich den kopierten Inhalt einfach ins aktive Dokument einfügen will, bekomme ich eine zusätzliche (nicht erwünschte) Absatzmarke mit eingefügt.

    Bodycount

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Bodycount“ ()

    Tja, keine Antwort ist auch eine Antwort. Ich hab' mir jetzt so beholfen, dass ich im Nachgang über das Selection-Object die überflüssige Absatzmarke entferne:

    Visual Basic-Quellcode

    1. Option Explicit
    2. Dim objWord As Word.Application
    3. Dim MyDoc As Word.Document
    4. Dim MyRange As Word.Range
    5. Dim MyHeaderFooterSource As Word.Document
    6. Sub CreateWordInstance()
    7. Set objWord = GetObject(, "Word.Application") 'Used when Word is already started.
    8. Set MyDoc = objWord.ActiveDocument 'Add an active document to Word
    9. Set MyRange = MyDoc.Range 'Define the document as range to use
    10. Set MyHeaderFooterSource = objWord.Documents.Open("D:\HeaderFooterSample\Sample.dotx", Visible:=False) 'Open the defined temp file
    11. With objWord
    12. MyHeaderFooterSource.Sections(1).Headers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.FormattedText.Copy 'Copy the header from the temp file
    13. MyRange.Sections(1).Headers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.FormattedText.Paste 'Paste the header from the temp file into the active document
    14. 'Letzte Absatzmarke entfernen
    15. .ActiveWindow.View.Type = wdPrintView
    16. .ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    17. MyRange.Sections(1).Headers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Select
    18. .Selection.EndKey Unit:=wdStory
    19. .Selection.Delete Unit:=wdCharacter, Count:=1
    20. .ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    21. End With
    22. MyHeaderFooterSource.Close (False) 'Close the temp file, but don't save it
    23. Set objWord = Nothing 'Release Word
    24. End Sub