• 182 views | 3 messages Discussion: LEAP
    Topic: LEAP API export does not work anymoreSubscribe | Previous | Next
  • Bryce McCall 7/6/2017

    Hello,

    About a year ago I used the VBA code posted by Taylor in the forum in 2015, which I managed to get to work (although slowly) in exporting all scenario energy consumption results from all branches. The link to the original code I used:
    https://www.energycommunity.org/default.asp?action=9&fid=22&TID=3431

    However, I have tried using this code again on a new project, and now the code throws some errors.
    The code I am using right now is slightly modified from that original post so as to exclude current accounts, my code (between the hashes):

    ######################

    If Not IsObject(LEAP) Then
    set LEAP = CreateObject("Leap.LEAPApplication")
    End If

    dim tech_branches
    set tech_branches = CreateObject("Scripting.Dictionary")

    for each b in LEAP.Branches
    if b.Branchtype = 4 then
    tech_branches.Add b.BranchID, b.Name

    end if
    next

    set xla = GetObject(,"Excel.Application")
    set xls = xla.sheets.add

    xls.name = "ATestNameOfSheet"

    LEAP.Verbose = 4
    LEAP.Visible = true

    ' Add headers to Excel worksheet '
    r = 1
    xls.Cells(r, 1).Value = "Scenario"
    xls.Cells(r, 2).Value = "Year"
    xls.Cells(r, 3).Value = "Branch"
    xls.Cells(r, 4).Value = "Fuel"
    xls.Cells(r, 5).Value = "Value (GJ)"

    ' Loop through scenarios, years, branches and export final energy demand in gigajoules '
    Print("Starting loop")
    for each s in LEAP.Scenarios
    If s<> "Current Accounts" Then

    for y = LEAP.BaseYear to LEAP.EndYear

    for each key in tech_branches.keys
    r = r + 1
    xls.Cells(r, 1).Value = s
    xls.Cells(r, 2).Value = y
    xls.Cells(r, 3).Value = LEAP.Branch(key).FullName
    xls.Cells(r, 4).Value = LEAP.Branch(key).Fuel.Name
    xls.Cells(r, 5).Value = LEAP.Branch(key).Variable("Energy Demand Final Units").Value(y, "Gigajoule")
    next
    next
    End If
    next

    xla.screenupdating = true
    xla.visible = true

    ######################

    The errors that are thrown are:
    "LEAP Automation error



    Branches.Item: Invalid Index (298) Must be in range 1 – 76"

    this happens twice, and then a third error:

    "Object Required: LEAP.Branch(…).Fuel"

    These errors are in reference to the last 3 lines of the loop print the values to excel cells.

    I'm not great at VBA, I know my way around with a bit of googling help, but I have not been able to get this script working.


    I did try a newer posted script, also by Taylor, from:

    https://www.energycommunity.org/default.asp?action=9&tid=3729&fid=22

    and get the same thrown errors.

    Any help please with fixing the code to export results would be great, thanks!

    -Bryce

  • Taylor Binnington 7/6/2017
      Best Response

    Hi Bryce,

    Sorry for the inconvenience. Indeed, the API has changed, and integers passed as arguments to LEAP.Branch are no longer interpreted by default as branch ID numbers.

    The LEAP.Branch object is now obsolete. Please use LEAP.Branches. You may refer to a particular LEAP branch by ID using the syntax:

    LEAP.Branches("id=X")

    ...where X is the ID of the branch. For your script, this means replacing instances of LEAP.Branch(key) with LEAP.Branches("id="&key).

    Hope this helps,
    Taylor

  • Bryce McCall 7/7/2017
      Best Response

    Thanks! Yes that did the trick.

    -Bryce