BaseBallBatBoy
New Member
- Joined
- Sep 12, 2011
- Messages
- 24
Hi,
So far I wrote into a word doc line after line. Just like this:
This works perfect so far. But now I no longer open a new document and just write, instead I open an existing one, search for a keyword which I would like to replace by some text (with text i mean text with some styles applied to them, different tables, line breaks, page breaks and so on, just like the output of the snippet before). But of course it doesn't work as I expected. This is how I search for a keyword to replace it:
The problem here is that I'm no longer at the end of the file but somewhere right in the middle. So InsertAfter doen't work. Also I have now a Word.Paragraph object instead of a Word.Document object
Do you have an idea what I could do to replace my keyword with the output my first snippet would produce?
My idea was to add all the text at the end of the document and define a range object over it. Then cut it out and paste it where it belongs:
But this cut/paste stuff doesn't do anything.... Maybe I'm simply doing it the wrong way, I don't know.
Thanks in advance, your help is very much appreciated!
Regards
So far I wrote into a word doc line after line. Just like this:
Code:
With wrdDoc[INDENT] .range(0).Style = .Styles(wdStyleHeading1)
.Content.InsertAfter "HEAD foo"
.Content.InsertParagraphAfter
.Content.InsertAfter "foo"
.Content.InsertParagraphAfter
[INDENT]Set wrdTable = wrdDoc.Tables.Add(range:=.range(.Characters.Count - 1), NumRows:=1, NumColumns:=2)
[/INDENT] With wrdTable
.cell(1, 1).range.InsertAfter "Name"
.cell(1, 2).range.InsertAfter "foo"[INDENT]End With
[/INDENT] With .Paragraphs(.Paragraphs.Count).range
.Collapse Direction:=wdCollapseEnd
.InsertBreak Type:=wdPageBreak
End With
.range(.Characters.Count - 1).Style = .Styles(wdStyleHeading1)
.Content.InsertAfter "HEAD bar"
.Content.InsertParagraphAfter
.Content.InsertAfter "bar"
.Content.InsertParagraphAfter
[INDENT]Set wrdTable = wrdDoc.Tables.Add(range:=.range(.Characters.Count - 1), NumRows:=1, NumColumns:=2)
[/INDENT] With wrdTable
.cell(1, 1).range.InsertAfter "Last name"
.cell(1, 2).range.InsertAfter "bar"[INDENT]End With[/INDENT][/INDENT]End With
This works perfect so far. But now I no longer open a new document and just write, instead I open an existing one, search for a keyword which I would like to replace by some text (with text i mean text with some styles applied to them, different tables, line breaks, page breaks and so on, just like the output of the snippet before). But of course it doesn't work as I expected. This is how I search for a keyword to replace it:
Code:
toReplace = "[[MYKEYWORD]]"
For Each myParagraph In wrdDoc.Paragraphs
If InStr(1, myParagraph.range.text, toReplace, vbTextCompare) <> 0 Then
' now here should be my new text
End If
Next
The problem here is that I'm no longer at the end of the file but somewhere right in the middle. So InsertAfter doen't work. Also I have now a Word.Paragraph object instead of a Word.Document object
Do you have an idea what I could do to replace my keyword with the output my first snippet would produce?
My idea was to add all the text at the end of the document and define a range object over it. Then cut it out and paste it where it belongs:
Code:
For Each myParagraph In wrdDoc.Paragraphs
If InStr(1, myParagraph.range.text, toReplace, vbTextCompare) <> 0 Then
With wrdDoc
Set startCopyRange = .range(.Characters.Count)
.Content.InsertAfter "THIS IS SOME TEXT ADDED AT THE VERY END OF THIS DOCUMENT"
Set endCopyRange = .range(.Characters.Count)
Set myRange = .range(startCopyRange, endCopyRange)
myRange.Cut
End With
myParagraph.range.Paste
End If
Next
But this cut/paste stuff doesn't do anything.... Maybe I'm simply doing it the wrong way, I don't know.
Thanks in advance, your help is very much appreciated!
Regards