title: Programatically editing an Excel 2013 Powerpivot Datafeed Connection
body: <p>I have an excel 2013 powerpivot document... that connects to a wcfdataservice that i host... data connectivity works fine, all data is brought back and populated into my powerpivot charts/tables succesfully.</p>
  
  <p>The data is hosted at Data Feed URL that looks like this.:</p>
  
  <pre><code>http://mydomain/Reporting/WcfDataService1.svc/
  </code></pre>
  
  <p>I have dozens of clients, that will each need to receive this same document... but with the DataFeedURL edited to point to their hosted data service... each one will have a different URL... such as </p>
  
  <pre><code>http://Client1/Reporting/WcfDataService1.svc/ 
  
  http://Client2/Reporting/WcfDataService1.svc/
  </code></pre>
  
  <p>Long story short... I wish for clients to use a C# Windows Form application of mine, that will have a reference to the template PowePivot Report... and when the user gets done setting up various information regarding their company... i need to programatically save a version of the PowerPivot report that contains an updated Connection to their new URL that I generate.</p>
  
  <p>I am attempting to do this as follows:</p>
  
  <pre><code>string workbookPath = String.Format("{0}\\{1}",      Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "PowerPivotSource.xlsx");
  string workbookPath2 = String.Format("{0}\\{1}", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "PowerPivotTestOut.xlsx");
  
  foreach (Excel.WorkbookConnection connection in excelWorkbook.Connections)
  {
      if (connection.Type == Excel.XlConnectionType.xlConnectionTypeDATAFEED)
      {
          Excel.DataFeedConnection dataFeedCur = connection.DataFeedConnection;
  
          dataFeedCur.Connection = dataFeedCur.Connection.Replace("mydomain", "Client1");
          break;
       }
   }
  
   excelWorkbook.Close(true);
  </code></pre>
  
  <p>I can read the contents of dataFeedCur.Connection, but attempting to edit it as shown here</p>
  
  <pre><code>dataFeedCur.Connection = dataFeedCur.Connection.Replace("localhost", "Client1");
  </code></pre>
  
  <p>throws the following exception:</p>
  
  <pre><code>Exception Message: "Exception from HRESULT: 0x800A03EC"
  Exception Stack Trace: ""   at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData&amp; msgData)\r\n   at Microsoft.Office.Interop.Excel.DataFeedConnection.set_Connection(Object value)\r\n
  </code></pre>
  
  <p>There is some indication the WorkBookConnection.DataFeedConnection objet is Read Only... but I've heard people in other forums talk about being able to edit this connection string, but have seen no examples... any one know how this DataFeedConnection.Connection object can be edited?</p>