I have a Word document that contains some specific text that I need to copy and paste onto a selected Excel sheet according to an Excel file. Can anyone suggest a way to achieve this using VBA?
Here's what I have so far:
Option Explicit
Private xlWB1 As String
Private xlWB2 As String
Private xlSheet As String
Sub CopyText_from_Word_to_Excel()
Dim EXL As Object
Dim xlsWB1 As String
Dim xlsWB2 As Object
Dim xlsPath As String
Dim oDoc As Document
Dim oRng As Range
Dim Arr() As Variant
xlsWB1 = "D:\databases\ENG.xlsx"
xlsWB2 = BrowseForFile("Select Workbook", True)
If Not xlsWB2 = vbNullString Then
xlSheet = "sheet1"
Set EXL = CreateObject("Excel.Application")
Set oDoc = ActiveDocument
Set oRng = oDoc.Range
Arr = xlFillArray(xlsWB1, xlSheet)
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Font.Name = "Times New Roman"
.Font.Bold = True
Dim ind As Long
For ind = LBound(Arr) To UBound(Arr)
.Text = Arr(ind)
Do While .Execute()
WriteToWorksheet xlsWB1, xlSheet, oRng.Text
Loop
Next ind
End With
lbl_Exit:
Exit Sub
End If
Set xlsWB2 = EXL.Workbooks.Open(xlsWB2)
EXL.Visible = True
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
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
Private Function xlFillArray(strWorkbook As String, _
strRange As String) As Variant
Dim RS As Object
Dim CN As Object
Dim iRows As Long
strRange = strRange & "$]" 'Use this to work with a named worksheet
'strRange = strRange & "]" 'Use this to work with a named range
Set CN = CreateObject("ADODB.Connection")
'Set HDR=NO for no header row
CN.Open ConnectionString:="Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & strWorkbook & ";" & _
"Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1"""
Set RS = CreateObject("ADODB.Recordset")
RS.Open "SELECT * FROM [" & strRange, CN, 2, 1
With RS
.MoveLast
iRows = .RecordCount
.MoveFirst
End With
xlFillArray = RS.GetRows(iRows)
If RS.State = 1 Then RS.Close
Set RS = Nothing
If CN.State = 1 Then CN.Close
Set CN = Nothing
lbl_Exit:
Exit Function
End Function
I tried the above code but it doesn't work. Those three private functions are workable in other VBA codes. I don't know how to write the code so as to copy specific text from a Word document and paste on selected Excel sheet according to an Excel file.
How can I achieve this?
The error occurred:https://drive.google.com/file/d/1eueMnOIaBuKlLUq2WSa298qOCklPkSbV/view?usp=share_link
The sample word file for testing:https://docs.google.com/document/d/1c68y2uu0BgUIjITIOpC7h8UpAv68rCpc/edit?usp=share_link&ouid=102729134456357219586&rtpof=true&sd=true
The expected outcome file: https://docs.google.com/spreadsheets/d/1-bGM88Tl4Ibou3bnK5QJHO8lo_0Vg53D/edit?usp=share_link&ouid=102729134456357219586&rtpof=true&sd=true
Thanks in advance for your help!
Set xlsWB1 = EXL.Open("D:\databases\ENG.xlsx")