Home

PowerQuery fail when run via VBA

%3CLINGO-SUB%20id%3D%22lingo-sub-563027%22%20slang%3D%22en-US%22%3EPowerQuery%20fail%20when%20run%20via%20VBA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-563027%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20everyone!%3C%2FP%3E%3CP%3EMy%20scenario%20is%20like%20this%3A%3C%2FP%3E%3CP%3EI%20must%20schedule%20an%20xlsm%20file%20to%20run%20on%20a%20remote%20machine%2C%20every%20night%2C%20which%20basically%20opens%2030%2B%20excel%20files%20with%20power%20queries%20to%20be%20updated%20on%20specific%20worksheets%3B%20this%20is%20achieved%20by%20the%20following%20expression%3A%3C%2FP%3E%3CP%3E%60%60%60vba%3CBR%20%2F%3EIf%20SelectRow%20%26lt%3B%26gt%3B%20%22%22%20And%20SelectColumn%26lt%3B%26gt%3B%20%22%22%20Then%3CBR%20%2F%3ECells(SelectRow%2C%20SelectColumn).Select%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EOn%20Error%20Resume%20Next%3CBR%20%2F%3ESelection.PivotTable.PivotCache.Refresh%3C%2FP%3E%3CP%3ESelection.ListObject.QueryTable.Refresh%20BackgroundQuery%3A%3DFalse%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EDoEvents%3CBR%20%2F%3EApplication.CalculateUntilAsyncQueriesDone%3CBR%20%2F%3EApplication.Wait%20DateAdd(%22n%22%2C%20Esperar%2C%20Now)%3C%2FP%3E%3CP%3E%3CBR%20%2F%3ExlBook.Save%3CBR%20%2F%3ExlBook.Close%20True%3CBR%20%2F%3E%60%60%60%3C%2FP%3E%3CP%3EThe%20mentioned%20power%20queries%20connect%20to%20excel%20files%20and%20folders%20within%20the%20same%20machine%2C%20with%20local%20paths%20set%20up.%3C%2FP%3E%3CP%3ENote%20that%20the%20queries%20themselves%20WORK%2C%20if%20run%20manually.%20I%20could%20even%20run%20this%20macro%20once%20with%20a%20remote%20session%20open%2C%20but%20they%20invariably%20fail%20when%20run%20via%20scheduler%20or%20when%20I%20close%20the%20remote%20session.%3C%2FP%3E%3CP%3EThe%20code%20will%20fail%20with%20the%20following%20errors.%20Could%20anyone%20please%20advise%3F%3C%2FP%3E%3CP%3EThanks%20a%20lot%2C%3C%2FP%3E%3CP%3E%60%60%60%3CBR%20%2F%3E**************%20Exception%20Text%20**************%3CBR%20%2F%3EMicrosoft.Mashup.Client.UI.Shared.Com.ComWrapperException%3A%20Cannot%20cast%20null%20to%20type%20'System.Double'.%20---%26gt%3B%20System.NullReferenceException%3A%20Object%20reference%20not%20set%20to%20an%20instance%20of%20an%20object.%3CBR%20%2F%3Eat%20Microsoft.Mashup.Client.UI.Shared.Com.ComWrapper.As%5BT%5D(Object%20value)%3CBR%20%2F%3E---%20End%20of%20inner%20exception%20stack%20trace%20---%3CBR%20%2F%3Eat%20Microsoft.Mashup.Client.UI.Shared.Com.ComWrapper.As%5BT%5D(Object%20value)%3CBR%20%2F%3Eat%20Microsoft.Mashup.Client.Excel.Com.ExcelComWrapper.AsIntFromDouble(Object%20value)%3CBR%20%2F%3Eat%20Microsoft.Mashup.Client.Excel.Com.ApplicationFeatures..ctor(IApplication%20application)%3CBR%20%2F%3Eat%20Microsoft.Mashup.Client.Excel.Com.Application..ctor(Object%20application%2C%20Boolean%20enableEvents)%3CBR%20%2F%3Eat%20Microsoft.Mashup.Client.Excel.AddIn.TryCreateApplication(Object%20appObject%2C%20IApplication%26amp%3B%20application)%3CBR%20%2F%3Eat%20Microsoft.Mashup.Client.Excel.AddIn.Extensibility.IDTExtensibility2.OnConnection(Object%20application%2C%20ext_ConnectMode%20connectMode%2C%20Object%20addInInst%2C%20Array%26amp%3B%20custom)%3C%2FP%3E%3CP%3E%60%60%60%3C%2FP%3E%3CP%3E%60%60%60%3CBR%20%2F%3E**************%20Exception%20Text%20**************%3CBR%20%2F%3EMicrosoft.Practices.Unity.ResolutionFailedException%3A%20Resolution%20of%20the%20dependency%20failed%2C%20type%20%3D%20%22Microsoft.Mashup.Host.Document.IApplicationConstants%22%2C%20name%20%3D%20%22(none)%22.%3CBR%20%2F%3EException%20occurred%20while%3A%20while%20resolving.%3CBR%20%2F%3EException%20is%3A%20InvalidOperationException%20-%20The%20current%20type%2C%20Microsoft.Mashup.Host.Document.IApplicationConstants%2C%20is%20an%20interface%20and%20cannot%20be%20constructed.%20Are%20you%20missing%20a%20type%20mapping%3F%3CBR%20%2F%3E-----------------------------------------------%3CBR%20%2F%3EAt%20the%20time%20of%20the%20exception%2C%20the%20container%20was%3A%3C%2FP%3E%3CP%3EResolving%20Microsoft.Mashup.Host.Document.IApplicationConstants%2C(none)%3CBR%20%2F%3E---%26gt%3B%20System.InvalidOperationException%3A%20The%20current%20type%2C%20Microsoft.Mashup.Host.Document.IApplicationConstants%2C%20is%20an%20interface%20and%20cannot%20be%20constructed.%20Are%20you%20missing%20a%20type%20mapping%3F%3CBR%20%2F%3Eat%20Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForAttemptingToConstructInterface(IBuilderContext%20context)%3CBR%20%2F%3Eat%20BuildUp_Microsoft.Mashup.Host.Document.IApplicationConstants(IBuilderContext%20)%3CBR%20%2F%3Eat%20Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext%20context)%3CBR%20%2F%3Eat%20Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext%20context)%3CBR%20%2F%3Eat%20Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type%20t%2C%20Object%20existing%2C%20String%20name%2C%20IEnumerable%601%20resolverOverrides)%3CBR%20%2F%3E---%20End%20of%20inner%20exception%20stack%20trace%20---%3CBR%20%2F%3Eat%20Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type%20t%2C%20Object%20existing%2C%20String%20name%2C%20IEnumerable%601%20resolverOverrides)%3CBR%20%2F%3Eat%20Microsoft.Practices.Unity.UnityContainer.Resolve(Type%20t%2C%20String%20name%2C%20ResolverOverride%5B%5D%20resolverOverrides)%3CBR%20%2F%3Eat%20Microsoft.Practices.Unity.UnityContainerExtensions.Resolve%5BT%5D(IUnityContainer%20container%2C%20ResolverOverride%5B%5D%20overrides)%3CBR%20%2F%3Eat%20Microsoft.Mashup.Host.Document.DependencyInjectionService.Resolve%5BT%5D()%3CBR%20%2F%3Eat%20Microsoft.Mashup.Host.Document.ClientConfig.ClientConfigManager..ctor()%3CBR%20%2F%3Eat%20Microsoft.Mashup.Host.Document.ClientConfig.ClientConfigManager.get_Instance()%3CBR%20%2F%3Eat%20Microsoft.Mashup.Client.Excel.AddIn.Extensibility.IDTExtensibility2.OnDisconnection(ext_DisconnectMode%20disconnectMode%2C%20Array%26amp%3B%20custom)%3C%2FP%3E%3CP%3E%60%60%60%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-563027%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EBI%20%26amp%3B%20Data%20Analysis%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-563707%22%20slang%3D%22en-US%22%3ERe%3A%20PowerQuery%20fail%20when%20run%20via%20VBA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-563707%22%20slang%3D%22en-US%22%3EWhich%20credentials%20have%20you%20set%20the%20task%20scheduler%20to%20use%20to%20run%20this%3F%20You%20may%20have%20to%20provide%20yours%20for%20them%20to%20work%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-563787%22%20slang%3D%22en-US%22%3ERe%3A%20PowerQuery%20fail%20when%20run%20via%20VBA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-563787%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F22322%22%20target%3D%22_blank%22%3E%40Jan%20Karel%20Pieterse%3C%2FA%3E%26nbsp%3BNo%20credentials%20are%20required%2C%20all%20source%20directories%2Fworkbooks%20are%20located%20on%20the%20same%20machine.%3C%2FP%3E%3CBLOCKQUOTE%3E%3CHR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F22322%22%20target%3D%22_blank%22%3E%40Jan%20Karel%20Pieterse%3C%2FA%3E%26nbsp%3Bwrote%3A%3CBR%20%2F%3EWhich%20credentials%20have%20you%20set%20the%20task%20scheduler%20to%20use%20to%20run%20this%3F%20You%20may%20have%20to%20provide%20yours%20for%20them%20to%20work%3F%3CHR%20%2F%3E%3C%2FBLOCKQUOTE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-563842%22%20slang%3D%22en-US%22%3ERe%3A%20PowerQuery%20fail%20when%20run%20via%20VBA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-563842%22%20slang%3D%22en-US%22%3EIn%20which%20folders%20are%20the%20files%3F%20If%20they%20are%20anywhere%20in%20your%20user%20path%2C%20the%20task%20will%20have%20to%20run%20using%20YOUR%20credentials.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-563887%22%20slang%3D%22en-US%22%3ERe%3A%20PowerQuery%20fail%20when%20run%20via%20VBA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-563887%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F22322%22%20target%3D%22_blank%22%3E%40Jan%20Karel%20Pieterse%3C%2FA%3E%26nbsp%3BYes%2C%20the%20task%20scheduler%20run%20with%20my%20user%20credentials%3B%20the%20specific%20directory%20is%20D%3A%5CTableau_Share%5CTableauSources%5C%20and%20a%20lot%20of%20subdirectories%20from%20here.%20I%20should%20note%2C%20however%2C%20that%20all%20files%20involved%20were%20copied%20via%20robocopy%20from%20another%20machine%20and%20from%20another%20user%2C%20from%20whom%20we%20are%20migrating%20this%20whole%20structure.%20Perhaps%20this%20can%20raise%20the%20issues%20inside%20Power%20Query%3F%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20so%2C%20what%20should%20be%20done%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20in%20advance%2C%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-615157%22%20slang%3D%22en-US%22%3ERe%3A%20PowerQuery%20fail%20when%20run%20via%20VBA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-615157%22%20slang%3D%22en-US%22%3E%3CP%3EI%20ended%20up%20solving%20this%20and%20will%20share%20the%20solution%20in%20case%20anyone%20faces%20a%20similar%20issue%20in%20the%20future%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20problem%20was%20running%20the%20macro%20on%26nbsp%3B%20auto%20open%2C%20because%20Excel%20starts%20running%20your%20code%20before%20loading%20the%20add%20ins%2C%20so%20it%20was%20running%20a%20power%20query%20refresh%20without%20the%20power%20query%20addin%20being%20loaded.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20solution%20was%20to%20open%20excel%2C%20wait%20for%2010%20seconds%2C%20and%20only%20then%20open%20the%20workbook.%20If%20automation%20is%20required%2C%20some%20code%20must%20be%20written%20to%20open%20Excel%2C%20wait%2010%20seconds%2C%20then%20open%20the%20workbook%20by%20sending%20shortcut%20keystrokes%20(ALT%2C%20O%2C%20O%2C%20etc).%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-615721%22%20slang%3D%22en-US%22%3ERe%3A%20PowerQuery%20fail%20when%20run%20via%20VBA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-615721%22%20slang%3D%22en-US%22%3EAha!%20I%20think%20you%20can%20also%20solve%20this%20by%20not%20calling%20the%20refresh%20macro%20from%20Auto_Open%20directly%2C%20but%20by%20scheduling%20it%20from%20Auto_Open%20using%20Application.OnTime.%20This%20is%20an%20often%20used%20solution%20because%20indeed%20Excel%20triggers%20Workbook_Open%20or%20Auto_Open%20even%20before%20it%20has%20finished%20all%20of%20its%20startup%20tasks.%20Using%20Application.OnTime%20solves%20this%20as%20-even%20if%20you%20schedule%20it%200%20seconds%20from%20now-%20this%20is%20not%20processed%20before%20Excel%20has%20finished%20with%20all%20of%20that.%3C%2FLINGO-BODY%3E
BLaget
New Contributor

Hello everyone!

My scenario is like this:

I must schedule an xlsm file to run on a remote machine, every night, which basically opens 30+ excel files with power queries to be updated on specific worksheets; this is achieved by the following expression:

```vba
If SelectRow <> "" And SelectColumn<> "" Then
Cells(SelectRow, SelectColumn).Select
End If


On Error Resume Next
Selection.PivotTable.PivotCache.Refresh

Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False


DoEvents
Application.CalculateUntilAsyncQueriesDone
Application.Wait DateAdd("n", Esperar, Now)


xlBook.Save
xlBook.Close True
```

The mentioned power queries connect to excel files and folders within the same machine, with local paths set up.

Note that the queries themselves WORK, if run manually. I could even run this macro once with a remote session open, but they invariably fail when run via scheduler or when I close the remote session.

The code will fail with the following errors. Could anyone please advise?

Thanks a lot,

```
************** Exception Text **************
Microsoft.Mashup.Client.UI.Shared.Com.ComWrapperException: Cannot cast null to type 'System.Double'. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Mashup.Client.UI.Shared.Com.ComWrapper.As[T](Object value)
--- End of inner exception stack trace ---
at Microsoft.Mashup.Client.UI.Shared.Com.ComWrapper.As[T](Object value)
at Microsoft.Mashup.Client.Excel.Com.ExcelComWrapper.AsIntFromDouble(Object value)
at Microsoft.Mashup.Client.Excel.Com.ApplicationFeatures..ctor(IApplication application)
at Microsoft.Mashup.Client.Excel.Com.Application..ctor(Object application, Boolean enableEvents)
at Microsoft.Mashup.Client.Excel.AddIn.TryCreateApplication(Object appObject, IApplication& application)
at Microsoft.Mashup.Client.Excel.AddIn.Extensibility.IDTExtensibility2.OnConnection(Object application, ext_ConnectMode connectMode, Object addInInst, Array& custom)

```

```
************** Exception Text **************
Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "Microsoft.Mashup.Host.Document.IApplicationConstants", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, Microsoft.Mashup.Host.Document.IApplicationConstants, is an interface and cannot be constructed. Are you missing a type mapping?
-----------------------------------------------
At the time of the exception, the container was:

Resolving Microsoft.Mashup.Host.Document.IApplicationConstants,(none)
---> System.InvalidOperationException: The current type, Microsoft.Mashup.Host.Document.IApplicationConstants, is an interface and cannot be constructed. Are you missing a type mapping?
at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForAttemptingToConstructInterface(IBuilderContext context)
at BuildUp_Microsoft.Mashup.Host.Document.IApplicationConstants(IBuilderContext )
at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)
at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)
--- End of inner exception stack trace ---
at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)
at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides)
at Microsoft.Practices.Unity.UnityContainerExtensions.Resolve[T](IUnityContainer container, ResolverOverride[] overrides)
at Microsoft.Mashup.Host.Document.DependencyInjectionService.Resolve[T]()
at Microsoft.Mashup.Host.Document.ClientConfig.ClientConfigManager..ctor()
at Microsoft.Mashup.Host.Document.ClientConfig.ClientConfigManager.get_Instance()
at Microsoft.Mashup.Client.Excel.AddIn.Extensibility.IDTExtensibility2.OnDisconnection(ext_DisconnectMode disconnectMode, Array& custom)

```

6 Replies
Which credentials have you set the task scheduler to use to run this? You may have to provide yours for them to work?

@Jan Karel Pieterse No credentials are required, all source directories/workbooks are located on the same machine.


@Jan Karel Pieterse wrote:
Which credentials have you set the task scheduler to use to run this? You may have to provide yours for them to work?

 

In which folders are the files? If they are anywhere in your user path, the task will have to run using YOUR credentials.

@Jan Karel Pieterse Yes, the task scheduler run with my user credentials; the specific directory is D:\Tableau_Share\TableauSources\ and a lot of subdirectories from here. I should note, however, that all files involved were copied via robocopy from another machine and from another user, from whom we are migrating this whole structure. Perhaps this can raise the issues inside Power Query? 

 

If so, what should be done?

 

Thanks in advance,

I ended up solving this and will share the solution in case anyone faces a similar issue in the future:

 

The problem was running the macro on  auto open, because Excel starts running your code before loading the add ins, so it was running a power query refresh without the power query addin being loaded.

 

The solution was to open excel, wait for 10 seconds, and only then open the workbook. If automation is required, some code must be written to open Excel, wait 10 seconds, then open the workbook by sending shortcut keystrokes (ALT, O, O, etc).

Aha! I think you can also solve this by not calling the refresh macro from Auto_Open directly, but by scheduling it from Auto_Open using Application.OnTime. This is an often used solution because indeed Excel triggers Workbook_Open or Auto_Open even before it has finished all of its startup tasks. Using Application.OnTime solves this as -even if you schedule it 0 seconds from now- this is not processed before Excel has finished with all of that.
Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
46 Replies
Extentions Synchronization
Deleted in Discussions on
3 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
29 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
13 Replies