Closed Thread
Results 1 to 2 of 2

Export Powerpivot to CSV - macro error "Method 'Open' of object '_Recordset' failed"

  1. #1
    Registered User
    Join Date
    10-16-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1

    Export Powerpivot to CSV - macro error "Method 'Open' of object '_Recordset' failed"

    Hi

    I am seeking to apply the VBA code (shown below) from an article (link below) in Powerpivot for Excel 2013 (64 bit) and am getting the error:

    "macro returns error "Method 'Open' of object '_Recordset' failed"

    http://www.powerpivotblog.nl/export-...csv-using-vba/ (1 person got this working, 1 did not)

    As a beginner in VBA, I must be failing in some obvious way, e.g. not applying the correct references or something? Big thanks in advance for any help!


    VBA_error.jpg
    -----------------------------------------------------------------------------
    Option Explicit
    Public Sub ExportToCsv()

    Dim wbTarget As Workbook
    Dim ws As Worksheet
    Dim rs As Object
    Dim sQuery As String

    'Suppress alerts and screen updates
    With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
    End With

    'Bind to active workbook
    Set wbTarget = ActiveWorkbook

    Err.Clear

    On Error GoTo ErrHandler

    'Make sure the model is loaded
    wbTarget.Model.Initialize

    'Send query to the model
    sQuery = "EVALUATE <MOVERS_1of6_MoveHistory>"
    Set rs = CreateObject("ADODB.Recordset")
    rs.Open sQuery, wbTarget.Model.DataModelConnection.ModelConnection.ADOConnection
    Dim CSVData As String
    CSVData = RecordsetToCSV(rs, True)

    'Write to file
    Open "C:\temp\MOVERS_1of6_MoveHistory.csv" For Binary Access Write As #1
    Put #1, , CSVData
    Close #1

    rs.Close
    Set rs = Nothing

    ExitPoint:
    With Application
    .ScreenUpdating = True
    .DisplayAlerts = True
    End With
    Set rs = Nothing
    Exit Sub

    ErrHandler:
    MsgBox "An error occured - " & Err.Description, vbOKOnly
    Resume ExitPoint
    End Sub



    Public Function RecordsetToCSV(rsData As ADODB.Recordset, _
    Optional ShowColumnNames As Boolean = True, _
    Optional NULLStr As String = "") As String
    'Function returns a string to be saved as .CSV file
    'Option: save column titles

    Dim K As Long, RetStr As String

    If ShowColumnNames Then
    For K = 0 To rsData.Fields.Count - 1
    RetStr = RetStr & ",""" & rsData.Fields(K).Name & """"
    Next K

    RetStr = Mid(RetStr, 2) & vbNewLine
    End If

    RetStr = RetStr & """" & rsData.GetString(adClipString, -1, """,""", """" & vbNewLine & """", NULLStr)
    RetStr = Left(RetStr, Len(RetStr) - 3)

    RecordsetToCSV = RetStr
    End Function

  2. #2
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Export Powerpivot to CSV - macro error "Method 'Open' of object '_Recordset' failed"

    Welcome to the Forum, unfortunately:

    This is a duplicate post and as such does not comply with Rule 5 of our forum rules. This thread will now be closed, you may continue in your other thread.

    Thread Closed.
    Regards

    Fotis.

    -This is my Greek whisper to Europe.

    --Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

    Advanced Excel Techniques: http://excelxor.com/

    --KISS(Keep it simple Stupid)

    --Bring them back.

    ---See about Acropolis of Athens.

    --Visit Greece.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Excel 2011 Macro error "Method 'GetOpenFilename" of object'_Application'failed
    By rfox69 in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 1
    Last Post: 09-26-2012, 08:35 AM
  2. macro returns error "Method 'Open' of object '_Recordset' failed"
    By Leafgreen in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-09-2009, 05:40 AM
  3. Error in Macro: "Method 'Paste' of object '_Worksheet' failed"
    By blork in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-05-2006, 01:50 PM
  4. [SOLVED] What is Error "Method "Paste" of object "_Worksheet" failed?
    By vat in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-17-2006, 04:10 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1