views:

185

answers:

1

I am trying to write a word macro that Selects everything that is Heading 2 (or any specific style), copies it and pastes it into an Excel document. Macro recording doesn't work here and I'm stumped as to the syntax for the necessary commands. Is it possible to do?

P.S. It would be really great if it could select whatever style the cursor was next to (i.e. if you click on a Heading 1 word and then run the macro it selects everything that is Heading 1) but I doubt that this is close to possible.

Thank you so much, and I hope I get some answers soon (with any luck tonight).

Pavja2

This is what I have so far (note, I have NO IDEA how to do the excel thing so if anyone knows that it would be a big help to):

 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 on this...

+4  A: 

This is a short example and doesn't address the Excel part, but hopefully does some good. It uses the style from the selection or the start of the selection and displays the output 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
ForEachLoop