title: vba Excel data connection
body: <p>I need some help. I have a workbook (A) and i want open another workbook (B) and run a macro that is inside B.
  Workbook (B) have a macro that make a sql connection to another workbook (C) to extract data, when i run this macro in the workbook (B) work fine, but if i open the workbook (B) from workbook (A) and run the macro i get a message from Windows “The file is open” (WorkBooK (C)).
  How can I skip this message.</p>
  
  <p><a href="http://i.stack.imgur.com/bPpmC.png" rel="nofollow">enter image description here</a></p>
  
  <p>I get the error when open the connection data.</p>
  
  <p>The code to open the Workbook(B) is :</p>
  
  <pre><code>    Sub OpenWorkBookandRun()
      tarPath = "..............\"
      strfilename = "..........xlsm"
  
      Application.Workbooks.Open tarPath &amp; strfilename
      'Run Macro
      Workbooks(strfilename).Activate
      Application.Run "'................'!Module1.Macro"
      ActiveWorkbook.Save
      ActiveWorkbook.Close
  End Sub
  </code></pre>
  
  <p>And the code in the workbook (B) to open data origin is:</p>
  
  <pre><code>    ' Establezco los parametros de la conexion ADO, el RecordSet y la Cadena de conexion
  Set oCONN = New ADODB.Connection
  Set oRS = New ADODB.Recordset
  strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" &amp; OrigDatos &amp; ";Extended Properties=""Excel 12.0;HDR=Yes;"";"
  ' Abro la conexion y ejecuto la sentencia SQL
  If Not (oCONN Is Nothing) Then
    If (oCONN.State And adStateOpen) = adStateOpen Then oCONN.Close
  End If
  oCONN.Open strConn  &lt;--- Line where i get the error
  oRS.Open strSQL, oCONN, 1, 3
  </code></pre>
  
  <p>Note. Sometimes in the fisrt workbook I get the same messages, but only sometimes and I don’t know why because anybody has the origin of data open</p>