I'm writing a VBA script to generate word documents from an already defined template. In it, I need to be able to write headings along with a body for each heading. As a small example, I have a word document that contains only <PLACEHOLDER>
. For each heading and body I need to write, I use the find-and-replace feature in VBA to find <PLACEHOLDER>
and replace it with the heading name, a newline, and then <PLACEHOLDER>
again. This is repeated until each heading name and body is written and then the final <PLACEHOLDER>
is replaced with a newline.
The text replacing works fine, but the style I specify gets overwritten by the next call to the replacement. This results in everything I just replaced having the style of whatever my last call to my replacement function is.
VBA code (run main
)
Option Explicit
Sub replace_stuff(search_string As String, replace_string As String, style As Integer)
With ActiveDocument.Range.Find
.Text = search_string
.Replacement.Text = replace_string
.Replacement.style = style
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchWholeWord = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
End Sub
Sub main()
Dim section_names(2) As String
section_names(0) = "Introduction"
section_names(1) = "Background"
section_names(2) = "Conclusion"
Dim section_bodies(2) As String
section_bodies(0) = "This is the body text for the introduction! Fetched from some file."
section_bodies(1) = "And Background... I have no issue fetching data from the files."
section_bodies(2) = "And for the conclusion... But I want the styles to 'stick'!"
Dim i As Integer
For i = 0 To 2
' Writes each section name as wsStyleHeading2, and then the section body as wdStyleNormal
Call replace_stuff("<PLACEHOLDER>", section_names(i) & Chr(11) & "<PLACEHOLDER>", wdStyleHeading2)
Call replace_stuff("<PLACEHOLDER>", section_bodies(i) & Chr(11) & "<PLACEHOLDER>", wdStyleNormal)
Next i
Call replace_stuff("<PLACEHOLDER>", Chr(11), wdStyleNormal)
End Sub
Input document: A word document with only <PLACEHOLDER>
in it.
<PLACEHOLDER>
Expected Output: I expect that each heading will be displayed in the style I specified and can be viewed from the navigation pane like this:
Actual Output: However what I actually get is everything as wdStyleNormal
style like this:
I think the problem can be solved by inserting a paragraph break between every style transition, but when I try using vbCrLF
or Chr(10) & Chr(13)
or vbNewLine
instead of the chr(11)
I am using now, Each line begins with a boxed question mark like this:
<PLACEHOLDER>
after every replace so the next find-and-replace will be able to "find" it and know where to insert the text. It's replaced each time, and the final one is replaced with a new line (chr(11)
) which is why no<PLACEHOLDER>
s appear in the final document. This is the approach I came up with, but I'm not very familiar with vba so there probably is a better way – SyntaxVoid supports Monica