Vba 将宏运行到特定样式

Vba 将宏运行到特定样式,vba,ms-word,styles,Vba,Ms Word,Styles,下面的代码正在尝试将中的小写单词转换为大写。然而,我只需要在一个特定的词风格(“正常”)运行它。我试图将doc设置为ActiveDocument.Styles(“正常”),但我不断出错。任何帮助都会很有帮助。先谢谢你 Option Explicit Public Sub TitleCaseDocument() Dim doc As Document: Set doc = ActiveDocument.Styles("Normal") Dim wrd As Range

下面的代码正在尝试将中的小写单词转换为大写。然而,我只需要在一个特定的词风格(“正常”)运行它。我试图将doc设置为ActiveDocument.Styles(“正常”),但我不断出错。任何帮助都会很有帮助。先谢谢你

Option Explicit

   Public Sub TitleCaseDocument()
   Dim doc As Document: Set doc = ActiveDocument.Styles("Normal")
   Dim wrd As Range

   For Each wrd In doc.Words
       If wrd.Text <> UCase$(wrd.Text) Then wrd.Case = wdTitleWord
Next
End Sub
选项显式
公共子标题CaseDocument()
将文档设置为文档:设置文档=ActiveDocument.Styles(“正常”)
变暗wrd As范围
以文件文字表示的每个工作日
如果wrd.Text UCase$(wrd.Text),则wrd.Case=wdTitleWord
下一个
端接头

如果样式正常,是否要将中的小写改为大写? 对 我在word方面没有丰富的经验,但类似的内容可能会对您有所帮助(基于您的代码):

公共子标题CaseDocument()
将单据设置为单据:设置单据=ActiveDocument
变暗wrd As范围
以文件文字表示的每个工作日
如果wrd.Text UCase$(wrd.Text)和wrd.Style=“Normal”,则
wrd.Text=UCase$(wrd.Text)
如果结束
下一个
端接头

如果样式正常,是否要将中的小写改为大写? 对 我在word方面没有丰富的经验,但类似的内容可能会对您有所帮助(基于您的代码):

公共子标题CaseDocument()
将单据设置为单据:设置单据=ActiveDocument
变暗wrd As范围
以文件文字表示的每个工作日
如果wrd.Text UCase$(wrd.Text)和wrd.Style=“Normal”,则
wrd.Text=UCase$(wrd.Text)
如果结束
下一个
端接头

由@eaazel提供的解决方案落入默认成员陷阱

代码

wrd.Style
实际上是使用样式对象的默认成员,即“NameLocal”。因此,上述代码所隐含的代码是真实的

wrd.Style.NameLocal
通常这不会是一个问题,但是,用于提取样式对象的粒度级别意味着,有时会遇到没有样式的单词(例如ToC字段)。在这种情况下,返回的style对象为nothing,这会产生一个令人惊讶的错误,因为您无法对nothing对象调用NameLocal方法

因此,更正确的方法是使用保证有样式对象(例如段落)的单词单元,并在测试每个单词之前在此对象上测试样式

Option Explicit

Public Sub TitleCaseDocument()
    Dim myDoc As Document: Set myDoc = ActiveDocument

    Dim myPara As Range
    For Each myPara In myDoc.StoryRanges.Item(wdMainTextStory).Paragraphs

        If myPara.Style.NameLocal = "Normal" Then

            TitleParagraph myPara

       End If

    Next

End Sub

Public Sub TitleParagraph(ByVal ipRange As Word.Range)

    Dim myText As Range
    For Each myText In ipRange.Words

        If Not UCase$(myText.Text) = myText.Text Then

            myText.Words.Item(1).Case = wdTitleWord

        End If

    Next

End Sub
更新2020年4月16日修订的以下代码,该代码已被证明适用于Word文档

选项显式

Public Sub TitleCaseDocument()
    Dim myDoc As Document: Set myDoc = ActiveDocument

    Dim myPara As Word.Paragraph
    For Each myPara In myDoc.StoryRanges.Item(wdMainTextStory).Paragraphs

        If myPara.Style.NameLocal = "Normal" Then

            TitleParagraph myPara

       End If

    Next

End Sub

Public Sub TitleParagraph(ByVal ipPara As Word.Paragraph)

    Dim myText As Range
    For Each myText In ipPara.Range.Words

        If Not UCase$(myText.Text) = myText.Text Then

            myText.Words.Item(1).Case = wdTitleWord

        End If

    Next

End Sub

@eaazel提供的解决方案落入默认成员陷阱

代码

wrd.Style
实际上是使用样式对象的默认成员,即“NameLocal”。因此,上述代码所隐含的代码是真实的

wrd.Style.NameLocal
通常这不会是一个问题,但是,用于提取样式对象的粒度级别意味着,有时会遇到没有样式的单词(例如ToC字段)。在这种情况下,返回的style对象为nothing,这会产生一个令人惊讶的错误,因为您无法对nothing对象调用NameLocal方法

因此,更正确的方法是使用保证有样式对象(例如段落)的单词单元,并在测试每个单词之前在此对象上测试样式

Option Explicit

Public Sub TitleCaseDocument()
    Dim myDoc As Document: Set myDoc = ActiveDocument

    Dim myPara As Range
    For Each myPara In myDoc.StoryRanges.Item(wdMainTextStory).Paragraphs

        If myPara.Style.NameLocal = "Normal" Then

            TitleParagraph myPara

       End If

    Next

End Sub

Public Sub TitleParagraph(ByVal ipRange As Word.Range)

    Dim myText As Range
    For Each myText In ipRange.Words

        If Not UCase$(myText.Text) = myText.Text Then

            myText.Words.Item(1).Case = wdTitleWord

        End If

    Next

End Sub
更新2020年4月16日修订的以下代码,该代码已被证明适用于Word文档

选项显式

Public Sub TitleCaseDocument()
    Dim myDoc As Document: Set myDoc = ActiveDocument

    Dim myPara As Word.Paragraph
    For Each myPara In myDoc.StoryRanges.Item(wdMainTextStory).Paragraphs

        If myPara.Style.NameLocal = "Normal" Then

            TitleParagraph myPara

       End If

    Next

End Sub

Public Sub TitleParagraph(ByVal ipPara As Word.Paragraph)

    Dim myText As Range
    For Each myText In ipPara.Range.Words

        If Not UCase$(myText.Text) = myText.Text Then

            myText.Words.Item(1).Case = wdTitleWord

        End If

    Next

End Sub

非常感谢您的友好回复。但是,在为每个循环运行上述代码时,我遇到了“类型不匹配”错误。我不知道怎么解决这个问题非常感谢你。以上更新的代码非常有效,非常感谢您的回复。但是,在为每个循环运行上述代码时,我遇到了“类型不匹配”错误。我不知道怎么解决这个问题非常感谢你。上面更新的代码可以完美地工作