VBA SQL Connection to Excel from Word returns no records

0 votes

Trying to figure out how to read data from an Excel spreadsheet into a Word document is giving me fits. I work with Office 365. I connected to the workbook and sheet using a couple of articles I read, but no records were returned. I used the ActiveX Data Objects 2.8 Library as a reference. The code returns a -1 when it reaches the message box.

Here is what I have for code at this point.

Sub CreateLetter()

Dim rs As ADODB.Recordset, rsCount As ADODB.Recordset
Dim cn As ADODB.Connection
Dim sqlGetTbl As String
Dim sDataSource As String, sDataTable As String
Dim sProvider As String

Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
sDataSource = "D:\spreadsheetname.xlsx"
sDataTable = "[Donor Contact List$]"

sProvider = "Microsoft.ACE.OLEDB.16.0;"
sDataSource = sDataSource & ";Extended Properties = 'Excel 12.0 Xml;HDR=Yes';"

With cn
    .Provider = sProvider
    .ConnectionString = "Data Source=" & sDataSource
    .Open
End  With

sqlGetTbl = "SELECT * FROM " & sDataTable
Set rs = cn.Execute(sqlGetTbl)
MsgBox rs.RecordCount

Do
    With Selection
        .TypeText FullName & Chr(11) & Street & Chr(11) & City & ", " & St & "  " & Zip
        .TypeParagraph
    End With
Loop Until rs.EOF

rs.Close
cn.Close
Set cn = Nothing
Set rs = Nothing

End Sub
Nov 27, 2022 in Others by Kithuzzz
• 38,000 points
627 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

What follows functions for me. Include IMEX=1 in the connection string to indicate that the data source has headers in order to obtain the recordset information from the test data source I used.

The code in question has additional problems:

  1. It's necessary to use rs.MoveFirst to "initialize" the recordset (get it to the first record.
  2. It's necessary to use rs.MoveNext in each iteration of the loop, otherwise the code will "get stuck" on the first record and go into an infinite loop.
  3. It's safer to use Do While Not rs.EOF for the test for exiting the loop in case there really are no records in the recordset.
  4. It's necessary to specify the fields from the recordset. Fullname, city, street, etc. are meaningless, at least in the code in the question.
  5. Always release objects (set to Nothing) in the reverse order they were created. It's not possible to release the connection before the recordset is released - the active recordset will keep the connection "live".

My sample code:

Sub CreateLetter()

    Dim rs As ADODB.Recordset, rsCount As ADODB.Recordset
    Dim cn As ADODB.Connection
    Dim sqlGetTbl As String
    Dim sDataSource As String, sDataTable As String
    Dim sProvider As String

    Set cn = New ADODB.Connection
    Set rs = New ADODB.Recordset
    sDataSource = "D:\spreadsheetname.xlsx"
    sDataTable = "[Donor Contact List$]"

    sProvider = "Microsoft.ACE.OLEDB.16.0;"
    sDataSource = sDataSource & ";Extended Properties = 'Excel 12.0 Xml;HDR=Yes;IMEX=1';"

    With cn
        .Provider = sProvider
        .ConnectionString = "Data Source=" & sDataSource
        .Open
    End With

    sqlGetTbl = "SELECT * FROM " & sDataTable
    cn.CursorLocation = adUseClient
    Set rs = cn.Execute(sqlGetTbl)
    MsgBox rs.RecordCount
    rs.MoveFirst

    Do While Not rs.EOF
        With Selection
            .TypeText rs.Fields("Fullname").value & Chr(11) & rs.Fields("street").value & Chr(11) & rs.Fields("city").value & ", " & st & "  " & rs.Fields("Zip").Value
            .TypeParagraph
        End With
        rs.MoveNext
    Loop

    rs.Close
    cn.Close
    Set rs = Nothing
    Set cn = Nothing
End Sub
answered Nov 27, 2022 by narikkadan
• 63,600 points

edited Mar 5
0 votes

I think you need to specify a data range not just the sheet. that is:

sDataTable = "[Donor Contact List$A1:N567]"
answered Jan 4, 2023 by anonymous

edited Mar 5

Related Questions In Others

0 votes
1 answer
0 votes
1 answer

Convert CASE WHEN logic from SQL to EXCEL

A combination of IF(), ISNUMBER(SEARCH()), and OR() ...READ MORE

answered Sep 21, 2022 in Others by narikkadan
• 63,600 points
1,553 views
0 votes
1 answer
0 votes
1 answer

Paste special issue, From Excel to Word

Since the table in the word document ...READ MORE

answered Nov 20, 2022 in Others by narikkadan
• 63,600 points
674 views
0 votes
1 answer

How to add text on new page in word doc via excel vba

First start with: objDoc.Range.Insertafter vbCr & Chr(12) & ...READ MORE

answered Nov 25, 2022 in Others by narikkadan
• 63,600 points
2,034 views
0 votes
1 answer

How can I find and replace text in Word using Excel VBA?

Try this code Option Explicit Const wdReplaceAll = 2 Sub ...READ MORE

answered Oct 15, 2022 in Others by narikkadan
• 63,600 points
5,414 views
0 votes
1 answer

Multiple find and replace in MS Word from a list in MS Excel

If I understand you correctly, you want ...READ MORE

answered Oct 28, 2022 in Others by narikkadan
• 63,600 points
921 views
0 votes
1 answer

Trying to create an enclosing bookmark after pasting a picture from Excel into Word using VBA

Try this: Sub IMPORT_TO_WORD() Dim ws1 As Worksheet, ws2 ...READ MORE

answered Oct 30, 2022 in Others by narikkadan
• 63,600 points
1,003 views
0 votes
1 answer

Indent table pasted from Excel into Word

It is feasible to pick up the ...READ MORE

answered Oct 30, 2022 in Others by narikkadan
• 63,600 points
655 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP