title: LoadLibrary in VBA returns 0 when trying to load R.dll in Office from Microsoft Store
body: <p>I figure out that some very specific machines were given an error when trying to load <code>R.dll</code>. At first, I thought that it would be a problem with McAfee because 100% of the machines that I got the error was caused by Dell laptops with McAfee installed. But later then, I find out that the problem was caused by Office that is installed using the Microsoft Store, instead of the traditional way (downloading the installer and executing it). The Office installed using the Microsoft Store won't let me load the <code>R.dll</code>. It returns zero in the following code: </p>
  
  <pre><code>Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
  Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
  
  Private Sub TestDLL()
  
      Dim ret As Long
      ret = LoadLibrary("C:\Program Files\R\R-3.0.2\bin\i386\R.dll") ' returns zero
      MsgBox ret
      FreeLibrary ret
  
  End Sub
  </code></pre>
  
  <p>In order to get the above code to work you will first need to include the following folder to your <code>PATH</code> system variable, <em>before</em> opening VBA:</p>
  
  <pre><code>C:\Program Files\R\R-3.0.2\bin\i386
  </code></pre>
  
  <p>The code above works fine in all Office version that I've tested (2007~2019) when downloading the installer and then installing (without using the Microsoft Store). However, if I login in Windows with my Microsoft account and then use the Microsoft Store to install Office the <code>ret</code> variable returns zero. When I say Microsoft Store, I mean this:
  <a href="https://i.stack.imgur.com/rHA6o.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rHA6o.png" alt="Microsoft Store used to install Office and reproduce the problem"></a></p>
  
  <p>My guess is that <code>R.dll</code> cannot load some dependent DLL in Office (from Windows Store) since it runs in a sandbox and has limited access to the file system and, perhaps, other system resources.</p>
  
  <p>So, basically any information regarding to the below questions would help me:</p>
  
  <ol>
  <li>Perhaps this kind of version of Office won't let me do this and period? So I 
  have to give up to the Windows Store Office version?</li>
  <li>Is it worth trying to debug <code>R.dll</code>? I know that this would require some effort to compile <code>R.dll</code> and set the things up. </li>
  <li>Can I put some sort of <code>try-except</code> in the <code>LoadLibrary</code> call to give a more information of <em>why</em> it cannot load <code>R.dll</code>? </li>
  <li>Other ideas?</li>
  </ol>
  
  <p>Thanks for reading and for any help given.</p>