0
votes

I have a document which is autogenerated. Each part of the document has a description and then a list of sentences that are listed as different list items. This works perfectly.

However, there is a need to change the style of certain words that are part of a dictionary, and which may appear anywhere in the text. Given that the dictionary is stored in database, this cannot be hardcoded. The solution is therefore to replace the style of the different words by means of Range.Find.

This solution works perfectly when the words are embedded in a sentence, but changes the bullet style when the word in question is at the beginning of a bulleted sentence.

- DictionaryWord that should be bolded in a list item

becomes

DictionaryWord that should be bolded in a list item

However,

- This DictionayWord is however bolded correctly without removing the "list" style

I have tried to enter a dummy text before the first word, and then delete it by means of anothe Range.Find, but the issue remains the moment I delete the dummy text.

The code for changing the dictionary words is as follows:

 Set oRange = oDoc.Content
 With oRange.Find
    .Replacement.ClearFormatting
    .Format = True
    .Forward = True
    .MatchCase = True
    .MatchWholeWord = True
    .Wrap = wdFindContinue
    Set rsdictionary = DB.OpenRecordset("qrydictionaryterms", dbOpenSnapshot)
    .Replacement.Style = "Dictionary Style"
    While Not rsdictionary .EOF
        .Execute findtext:=rsdictionary ("CDMStereotype"), MatchCase:=True, Replace:=wdReplaceAll
        rsdictionary .MoveNext
     Wend
 end with

I would greatly appreciate any suggestions about how to fix this.

1
You should make Dictionary Style a Character style and not a Paragraph style and that should handle the examples you provided. With that said, there still would be a problem if the word was last in a paragraph.Rich Michaels
Thanks for the suggestion, but unfortunately that is not possible - the styles are defined in a Word template that is shared by multiple documents and different users, so I cannot modify it.Ramon
Do you have to use that Style Name? VBA code could create a character style. I think your only other option is to make your current VBA much smarter and if the found word is in a list style then, direct format the located word without changing the existing style name.Rich Michaels
I disagree with @RichMichaels. Ignoring the missing bullets the results stated in the question can only be achieved if "Dictionary Style" is already a character style. It is impossible to apply a para style to just a single word in a para. Even if "Dictionary Style" were a Linked Style your code would apply it as a para style. Therefore it can only be a character style. That said I cannot replicate your issue (I'm using Build 2102 of O365), no matter how the list has been applied.Timothy Rylatt
I agree, you are getting character formatting from your style. If you click in something formatted with that and press Shift+F1, it should show that style in the information displayed in Reveal Formatting (Windows version).Charles Kenyon

1 Answers

0
votes

Thanks to everybody who provided an answer, but it looks as if nothing was wrong with my code.

For others who may encounter this problem in the future, please find the explanation below:

The issue is that the applied style for DictionaryWord is based not on the default paragraph style, but rather on another style. It seems that Word applies the style stepwise, and the first thing it does is to apply the style on which the DictionaryWord style is based (which removes the bullets), then apply the DictionaryWord style.

The solution to this is to define the DictionaryWord as a Character style based on the default paragraph style, so that no other styles are applied first.