Many years later, but I'm adding a different solution for those, like me, still stuck with Excel 2013. As stated by @Alejandro in his response, Power Query was only added to the object model in Excel 2016.
If you are using an older version of Excel, you can use a cell-based solution similar to the one talked about here for relative source paths:
https://techcommunity.microsoft.com/t5/excel/power-query-source-from-relative-paths/m-p/206150
Basically have a cell some where in your workbook that contains your query's source. Name that cell, using standard Excel names. In the example below, I've named the cell SourceFileName
, and I'm trying to load an Excel file to PowerQuery. The full name of the source Excel file (including path) is in SourceFileName
. You can then access the contents of that cell via Power Query:
<previous M code>
sourceFileName= Excel.CurrentWorkbook(){[Name="SourceFileName"]}[Content]{0}[Column1],
Source = Excel.Workbook(File.Contents(sourceFileName ), null, true),
<rest of M code>