View Single Post
 
Old 04-17-2023, 01:18 AM
syl3786 syl3786 is offline Windows 10 Office 2019
Advanced Beginner
 
Join Date: Jan 2023
Posts: 97
syl3786 is on a distinguished road
Default VBA Copy Text issues

Hi community,

I encounter some difficulties in writing a VBA to do the following steps:

Copy text from a Word Document to an Excel file according to an Excel List

1. Browse an Excel file for copying text from the active Word document to it.
2. After selected the Excel file, the VBA will find text according to the Excel List and then copy those text to the Excel file.
3. Open the Excel file copied with specific text.

The system keep saying "xlsWB1 = "D:\databases\ENG.xlsx"" Object variable not set (Error 91). What should I do to resolve this issue?

Your help will be greatly appreciated.

Code:
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 Object
Dim xlsWB2 As Object
Dim xlsPath As String
Dim oDoc As Document
Dim oRng As Range

xlsWB1 = "D:\databases\ENG.xlsx"

xlWB2 = BrowseForFile("Select Workbook", True)
If Not xlWB2 = vbNullString Then

xlSheet = "sheet1"

    Set EXL = CreateObject("Excel.Application")
    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 = xlsWB1 Then
                  WriteToWorksheet xlWB1, xlSheet, oRng.Text
            End If
        Loop

    End With
    
lbl_Exit:
    Exit Sub
    End If
            Set xlsWB2 = EXL.Workbooks.Open(xlWB2)
    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
Reply With Quote