Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-27-2023, 05:03 AM
syl3786 syl3786 is offline Need help with VBA macro to copy text from Word to Excel according to a list Windows 10 Need help with VBA macro to copy text from Word to Excel according to a list Office 2019
Advanced Beginner
Need help with VBA macro to copy text from Word to Excel according to a list
 
Join Date: Jan 2023
Posts: 97
syl3786 is on a distinguished road
Default

Quote:
Originally Posted by Journeyman View Post
Hi.

I'm sorry this didn't work for you first time. This strikes me as an odd error, however, I have tested this from a different PC and it works as intended.

I've added two files as an attachment to this message.
- a basic word doc with a table - save to a temp folder


- an excel SS with a button included on the sheet.

the button is the only real addition.

Click the button and point the dialog to the word doc.

Note that the word table only contains a couple rows, but you can add more - and more info into the second column also.

Try again - hope it works this time.

Cheers
Thanks for your help. I apologize for the inconvenience caused since I think you may misunderstand what I aim to do. I hope to write a macro like the following:

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
However, this VBA limited to copy text like "Speaker 1", "Speaker 2", "Speaker 3" etc. I want to edit it as a macro that can copy the text from a Word Document according to an Excel sheet and then paste on designated Excel sheet.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to copy text from Word to Excel according to a list? syl3786 Word VBA 6 04-09-2023 08:01 AM
Text To Copy From Excel To Relevant Word Document Covert Codger Word VBA 4 07-27-2022 11:40 PM
a macro that can copy data from copy.xls to our current excel macro.xls based on criteria: udhaya Excel Programming 1 11-12-2015 10:12 AM
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

Other Forums: Access Forums

All times are GMT -7. The time now is 01:37 PM.


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