如何使用VBA将斜体文本存储到变量中

如何使用VBA将斜体文本存储到变量中,vba,ms-word,Vba,Ms Word,我是VBA的初学者。我只需要将特定段落中的斜体文本存储到变量中。如何操作?您可以使用以下示例查找具有所需格式的文本范围,然后将其存储在变量中进行处理,每次可以将新文本与变量的先前内容一起追加时都会使用该变量。 例如: 在哪里 文本格式搜索将替换为需要搜索的文本格式 参考资料来源:-感谢KazJaw。最后,我得到了解决方案,并粘贴在下面 sub test() ActiveDocument.Paragraphs(2).Range.Select Selection.Find.Font.Italic

我是VBA的初学者。我只需要将特定段落中的斜体文本存储到变量中。如何操作?

您可以使用以下示例查找具有所需格式的文本范围,然后将其存储在变量中进行处理,每次可以将新文本与变量的先前内容一起追加时都会使用该变量。 例如:

在哪里 文本格式搜索将替换为需要搜索的文本格式


参考资料来源:-

感谢KazJaw。最后,我得到了解决方案,并粘贴在下面

sub test()

ActiveDocument.Paragraphs(2).Range.Select 
Selection.Find.Font.Italic = True

With Selection.Find
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute
str_italic = Selection.Range.Text
End Sub

如果希望查找斜体文本并仅将文本存储在变量中,则可以尝试:

Sub findItal()

    Dim italText As String

    'Find italic text
    With Selection.Find
        .ClearFormatting
        .Font.Italic = True
        .Wrap = wdFindStop
        .Execute

        If .Found = True Then
            italText = Selection.Range.Text
        End If
    End With

End Sub

我不明白你在说什么。对不起,我误读了这个问题。文本从哪里来?那么没有其他方法可以从文档中只捕获斜体文本并存储它吗?在msword中,我有一些斜体文本的内容。我只需要获取斜体文本并将其存储到变量中。使用Word Find功能时录制宏。在您的情况下搜索特定的斜体样式。如何将斜体内容存储到变量中。我不想在文档中显示它
Sub findItal()

    Dim italText As String

    'Find italic text
    With Selection.Find
        .ClearFormatting
        .Font.Italic = True
        .Wrap = wdFindStop
        .Execute

        If .Found = True Then
            italText = Selection.Range.Text
        End If
    End With

End Sub