Hallo zusammen,
über folgenden VBA-Code (in Excel verwendet) kopiere ich Inhalt (hier die Kopfzeile) aus einem bestehenden Dokument in ein neues Dokument:
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
über folgenden VBA-Code (in Excel verwendet) kopiere ich Inhalt (hier die Kopfzeile) aus einem bestehenden Dokument in ein neues Dokument:
Visual Basic-Quellcode
- Option Explicit
- Dim objWord As Word.Application
- Dim MyDoc As Word.Document
- Dim MyRange As Word.Range
- Dim MyHeaderFooterSource As Word.Document
- Sub CreateWordInstance()
- Set objWord = GetObject(, "Word.Application") 'Used when Word is already started.
- Set MyDoc = objWord.ActiveDocument 'Add an active document to Word
- Set MyRange = MyDoc.Range 'Define the document as range to use
- Set MyHeaderFooterSource = objWord.Documents.Open("D:\HeaderFooterSample\Sample.dotx", Visible:=False) 'Open the defined temp file
- With objWord
- MyHeaderFooterSource.Sections(1).Headers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Copy 'Copy the header from the temp file
- MyRange.Sections(1).Headers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Paste 'Paste the header from the temp file into the active document
- End With
- MyHeaderFooterSource.Close (False) 'Close the temp file, but don't save it
- Set objWord = Nothing 'Release Word
- 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“ ()