简体   繁体   English

Word VBA Range.Find 更改样式

[英]Word VBA Range.Find changes style

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.因此解决方案是通过 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 - 应在列表项中加粗的 DictionaryWord

becomes变成

DictionaryWord that should be bolded in a list item列表项中应加粗的DictionaryWord

However,然而,

- This DictionayWord is however bolded correctly without removing the "list" style - 然而,这个DictionayWord是正确粗体的,而没有删除“列表”样式

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.我试图在第一个单词之前输入一个虚拟文本,然后通过另一个 Range.Find 将其删除,但问题仍然存在于我删除虚拟文本的那一刻。

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.我将不胜感激有关如何解决此问题的任何建议。

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.问题是DictionaryWord的应用样式不是基于默认段落样式,而是基于另一种样式。 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.似乎 Word 逐步应用样式,它做的第一件事是应用DictionaryWord样式所基于的样式(删除项目符号),然后应用DictionaryWord样式。

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.解决这个问题的方法是将DictionaryWord定义为基于默认段落样式的 Character 样式,这样就不会先应用其他 styles。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM