How can I copy one section of text from Word to Excel using an Excel macro?

前端 未结 1 1039
再見小時候
再見小時候 2020-12-19 11:54

I need to copy a specific item of text (one or a few words) from Word (2007) to Excel (2007) using an Excel macro, for multiple documents.

So far I have the Excel ma

相关标签:
1条回答
  • 2020-12-19 12:33

    Updated to show searching for text and then selecting content relative to its location:

    Sub FindAndCopyNext()
    
        Dim TextToFind As String, TheContent As String
        Dim rng As Word.Range
    
        TextToFind = "wibble" 'the text you're looking for to
                              ' locate the other content
    
        Set rng = wdApp.ActiveDocument.Content
        rng.Find.Execute FindText:=TextToFind, Forward:=True
    
        If rng.Find.Found Then
            If rng.Information(wdWithInTable) Then
              TheContent = rng.Cells(1).Next.Range.Text      'move right on row
              'TheContent = rng.Cells(1).Previous.Range.Text 'move left on row
              MsgBox "Found content '" & TheContent & "'"
            End If
        Else
            MsgBox "Text '" & TextToFind & "' was not found!"
        End If
    
    End Sub
    

    Then assign the variable TheContent to your required Excel range.

    0 讨论(0)
提交回复
热议问题