Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-08-2023, 03:02 AM
syl3786 syl3786 is offline How to copy text from Word to Excel according to a list? Windows 10 How to copy text from Word to Excel according to a list? Office 2019
Advanced Beginner
How to copy text from Word to Excel according to a list?
 
Join Date: Jan 2023
Posts: 78
syl3786 is on a distinguished road
Default How to copy text from Word to Excel according to a list?

Hi community,

I'm sorry that I have questions to ask again...

I would like to use word macro to copy specific text with specific font style from Word document to Microsoft Excel according to a list. I drafted the following macro based on VBA Express : Word - Extract sentences containing a specific word to excel file. However, I don't know how to copy specific text with specific font style from Word document to Microsoft Excel according to a list. Does anyone have any solution for that?

Expected outcome (Excel file): Loading Google Sheets (Copy times new roman, font size 12)
List: Loading Google Sheets
Test document: Loading Google Docs


Code:
Private strWorkbook As String
Private strSheet As String
Sub FindWordCopySentence()

    Dim appExcel As Object
    Dim strSheet As Object
    Dim aRange As Range
    Dim intRowCount As Integer
    intRowCount = 1
    Set aRange = ActiveDocument.Range
    With aRange.Find
        Do
            .Text = "shall" ' the word I am looking for
            .Execute
            If .found Then
                aRange.Expand Unit:=wdSentence
                aRange.Copy
                aRange.Collapse wdCollapseEnd
                If objSheet Is Nothing Then
                    Set appExcel = CreateObject("Excel.Application")
                     'Change the file path to match the location of your test.xls
                     
                    Set strSheet = BrowseForFile("Select Workbook", True)
                    If Not strSheet = vbNullString Then
                    
                    strSheet = InputBox("Please enter the name of the Sheet", "Worksheet", "Please enter the name of the Sheet")
                    If Not strSheet = vbNullString Then
                    
                    intRowCount = 1
                End If
                objSheet.Cells(intRowCount, 1).Select
                objSheet.Paste
                intRowCount = intRowCount + 1
            End If
        Loop While .found
    End With
    If Not objSheet Is Nothing Then
        appExcel.Workbooks(1).Close True
        appExcel.Quit
        Set objSheet = Nothing
        Set appExcel = Nothing
    End If
    Set aRange = Nothing
End Sub
Private Function BrowseForFile(Optional strTitle As String, Optional bExcel As Boolean) As String
Dim fDialog As FileDialog
    On Error GoTo err_Handler
    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    With fDialog
        .Title = strTitle
        .AllowMultiSelect = False
        .Filters.Clear
        If bExcel Then
            .Filters.add "Excel workbooks", "*.xls,*.xlsx,*.xlsm"
        Else
            .Filters.add "Word documents", "*.doc,*.docx,*.docm"
        End If
        .InitialView = msoFileDialogViewList
        If .Show <> -1 Then GoTo err_Handler:
        BrowseForFile = fDialog.SelectedItems.Item(1)
    End With
lbl_Exit:
    Exit Function
err_Handler:
    BrowseForFile = vbNullString
    Resume lbl_Exit
End Function


Last edited by syl3786; 04-08-2023 at 03:10 AM. Reason: forgot to add add attachment
Reply With Quote
  #2  
Old 04-08-2023, 04:35 AM
gmayor's Avatar
gmayor gmayor is offline How to copy text from Word to Excel according to a list? Windows 10 How to copy text from Word to Excel according to a list? Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

This would be essentially the same as your other queries except in reverse.
Start with your document open and save an empty workbook. Then the following macro will find any text that is bold TNR and starts with 'Speaker' and writes them to the empty worksheet column A

Code:
Option Explicit

Private Const xlWB As String = "C:\Path\Empty Excel File name.xlsx"
Private Const xlSheet As String = "Sheet1"

Sub ExtractText()
Dim oDoc As Document
Dim oRng As Range
    Set oDoc = ActiveDocument
    Set oRng = oDoc.Range
    With oRng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Font.Name = "Times New Roman"
        .Font.Bold = True
        Do While .Execute()
            If oRng.Text Like "Speaker*" Then
                WriteToWorksheet xlWB, xlSheet, oRng.Text
            End If
        Loop
    End With
lbl_Exit:
    Exit Sub
End Sub

Private Function WriteToWorksheet(strWorkbook As String, _
                                  strRange As String, _
                                  strValues As String)
Dim ConnectionString As String
Dim strSQL As String
Dim CN As Object
    ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                       "Data Source=" & strWorkbook & ";" & _
                       "Extended Properties=""Excel 12.0 Xml;HDR=YES;"";"
    strSQL = "INSERT INTO [" & strRange & "$] VALUES('" & strValues & "')"
    Set CN = CreateObject("ADODB.Connection")
    Call CN.Open(ConnectionString)
    Call CN.Execute(strSQL, , 1 Or 128)
    CN.Close
    Set CN = Nothing
lbl_Exit:
    Exit Function
End Function
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 04-08-2023, 06:27 AM
syl3786 syl3786 is offline How to copy text from Word to Excel according to a list? Windows 10 How to copy text from Word to Excel according to a list? Office 2019
Advanced Beginner
How to copy text from Word to Excel according to a list?
 
Join Date: Jan 2023
Posts: 78
syl3786 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
This would be essentially the same as your other queries except in reverse.
Start with your document open and save an empty workbook. Then the following macro will find any text that is bold TNR and starts with 'Speaker' and writes them to the empty worksheet column A

Code:
Option Explicit

Private Const xlWB As String = "C:\Path\Empty Excel File name.xlsx"
Private Const xlSheet As String = "Sheet1"

Sub ExtractText()
Dim oDoc As Document
Dim oRng As Range
    Set oDoc = ActiveDocument
    Set oRng = oDoc.Range
    With oRng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Font.Name = "Times New Roman"
        .Font.Bold = True
        Do While .Execute()
            If oRng.Text Like "Speaker*" Then
                WriteToWorksheet xlWB, xlSheet, oRng.Text
            End If
        Loop
    End With
lbl_Exit:
    Exit Sub
End Sub

Private Function WriteToWorksheet(strWorkbook As String, _
                                  strRange As String, _
                                  strValues As String)
Dim ConnectionString As String
Dim strSQL As String
Dim CN As Object
    ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                       "Data Source=" & strWorkbook & ";" & _
                       "Extended Properties=""Excel 12.0 Xml;HDR=YES;"";"
    strSQL = "INSERT INTO [" & strRange & "$] VALUES('" & strValues & "')"
    Set CN = CreateObject("ADODB.Connection")
    Call CN.Open(ConnectionString)
    Call CN.Execute(strSQL, , 1 Or 128)
    CN.Close
    Set CN = Nothing
lbl_Exit:
    Exit Function
End Function
Thank you gmayor. I tried but the macro doesn't copy text to the empty Excel sheet1. I ticked the "Microsoft Excel 16.0 object library". Is there any other things I need to preprare for the macro? Thanks again for your help!
Reply With Quote
  #4  
Old 04-08-2023, 08:56 PM
gmayor's Avatar
gmayor gmayor is offline How to copy text from Word to Excel according to a list? Windows 10 How to copy text from Word to Excel according to a list? Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Did you change the path to reflect the empty workbook name? The "Microsoft Excel 16.0 object library is not required to be checked.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 04-09-2023, 12:00 AM
syl3786 syl3786 is offline How to copy text from Word to Excel according to a list? Windows 10 How to copy text from Word to Excel according to a list? Office 2019
Advanced Beginner
How to copy text from Word to Excel according to a list?
 
Join Date: Jan 2023
Posts: 78
syl3786 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
Did you change the path to reflect the empty workbook name? The "Microsoft Excel 16.0 object library is not required to be checked.

Yes. I had changed the path to reflect the empty workbook name. Now it can run. I mistakenly forgot to change the sheet name......

Can "If oRng.Text Like "Speaker*" Then" be changed as "Speaker 1", "Speaker 2" etc? Since I need to input some names other than "Speaker".....
Reply With Quote
  #6  
Old 04-09-2023, 05:31 AM
gmayor's Avatar
gmayor gmayor is offline How to copy text from Word to Excel according to a list? Windows 10 How to copy text from Word to Excel according to a list? Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Change the main macro as follows and enter the names to search in the array.
Code:
Option Explicit

Private Const xlWB As String = "C:\Path\Empty Excel File name.xlsx"
Private Const xlSheet As String = "Sheet1"
Private vList() As Variant

Sub ExtractText()
    vList = Array("Speaker 1", "Speaker 2", "Speaker 3")
    Dim oDoc As Document
    Dim oRng As Range
    Dim i As Long

    Set oDoc = ActiveDocument
    Set oRng = oDoc.Range
    With oRng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Font.Name = "Times New Roman"
        .Font.Bold = True
        Do While .Execute()
            For i = 0 To UBound(vList)
                If oRng.Text = CStr(vList(i)) Then
                     WriteToWorksheet xlWB, xlSheet, oRng.Text
                    Exit For
                 End If
            Next i
        Loop
    End With
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #7  
Old 04-09-2023, 08:01 AM
syl3786 syl3786 is offline How to copy text from Word to Excel according to a list? Windows 10 How to copy text from Word to Excel according to a list? Office 2019
Advanced Beginner
How to copy text from Word to Excel according to a list?
 
Join Date: Jan 2023
Posts: 78
syl3786 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
Change the main macro as follows and enter the names to search in the array.
Code:
Option Explicit

Private Const xlWB As String = "C:\Path\Empty Excel File name.xlsx"
Private Const xlSheet As String = "Sheet1"
Private vList() As Variant

Sub ExtractText()
    vList = Array("Speaker 1", "Speaker 2", "Speaker 3")
    Dim oDoc As Document
    Dim oRng As Range
    Dim i As Long

    Set oDoc = ActiveDocument
    Set oRng = oDoc.Range
    With oRng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Font.Name = "Times New Roman"
        .Font.Bold = True
        Do While .Execute()
            For i = 0 To UBound(vList)
                If oRng.Text = CStr(vList(i)) Then
                     WriteToWorksheet xlWB, xlSheet, oRng.Text
                    Exit For
                 End If
            Next i
        Loop
    End With
lbl_Exit:
    Exit Sub
End Sub
It works very well! Words are not enough to express my gratitude.
Reply With Quote
Reply

Tags
excel copy text, word macro



Similar Threads
Thread Thread Starter Forum Replies Last Post
Text To Copy From Excel To Relevant Word Document Covert Codger Word VBA 4 07-27-2022 11:40 PM
How to copy text from Word to Excel according to a list? how to copy different text from word into excel on consecutive rows mihnea96 Word VBA 4 05-08-2017 12:09 PM
how to copy addresses in word document to excel/mailmerge list msnarayanan Mail Merge 4 10-17-2015 03:17 PM
Copy Underline text from Word and Paste into excel rfaris Excel Programming 7 10-05-2015 05:56 AM
How to copy text from Word to Excel according to a list? copy a specific words to excel list romanticbiro Word VBA 12 12-03-2014 05:12 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:15 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft