A more complex macro of words that I'm used to (i.e. I can't write it down)

I am trying to write a macro of words that selects everything that is heading 2 (or any particular style), copies it and pastes it into an Excel document. The macro recording does not work here, and I do not understand how the syntax of the necessary commands is. Can this be done?

PS It would be great if he could choose any style that the cursor was based on (i.e., if you click on heading 1 and then run the macro, it selects everything in heading 1), but I doubt that it is close to opportunities.

Thank you very much, and I hope that I will receive answers soon (without any luck today).

Pavja2

This is what I have so far (note, I don’t have IDEA how to do an excellent thing, so if anyone knows this will be a big help):

 Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles( _
    "Heading 2,Heading 2 Char Char2 Char1,Heading 2 Char1 Char Char1 Char,Heading 2 Char Char Char Char1 Char,Heading 2 Char1 Char Char Char1 Char Char1,Heading 2 Char Char Char Char Char1 Char Char,Heading 2 Char2 Char Char Char Char Char,Heading 2 Cha" _
    )
Selection.Find.ParagraphFormat.Borders.Shadow = False
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.Copy

So, can anyone figure this out ...? Bump ... I really need answers to this ...

+3
source share
1 answer

This is a brief example and does not apply to the part of Excel, but hopefully does something good. It uses the style from the selection or the beginning of the selection and displays the result in the Immediate window.

Public Sub SelectStyles()
    On Error GoTo MyErrorHandler

    Dim currentDocument As Document
    Set currentDocument = ActiveDocument

    Dim selectedStyle As String
    selectedStyle = Selection.Style

    Dim findRange As Range
    Set findRange = currentDocument.Range
    findRange.Find.ClearFormatting
    findRange.Find.Style = selectedStyle

    Do While (findRange.Find.Execute)
        Debug.Print findRange.Text
        DoEvents
    Loop

    Exit Sub

MyErrorHandler:
    MsgBox "SelectStyles" & " error: " & Err.Number & vbCrLf & Err.Description
End Sub
+4
source

Source: https://habr.com/ru/post/1719464/


All Articles