title: Retrieving Building Blocks from user generated Word document
body: <p>I've searched far and wide, and I haven't found a proper solution yet.</p>
  
  <p>First off, this is a WinForms application using .NET 4.0 and DevExpress.</p>
  
  <p>I've been attempting to retrieve Building Blocks (Watermarks, Cover Pages etc) from Word documents (.docx), or at least user generated template files (.dotx), that a user uploads to my application, which will then be saved to a database.</p>
  
  <p>I must be able to retrieve all the Building Blocks used in that file.</p>
  
  <p>I have tried a lot of different ways of retrieving them, but I can only retrieve them from the Built-In Building Blocks.dotx file, located in:</p>
  
  <pre><code>C:\Users\&lt;username&gt;\AppData\Roaming\Microsoft\Document Building Blocks\1033\14\Built-In Building Blocks.dotx
  </code></pre>
  
  <p><strong>I haven't figured out how to extract them from a user generated file.</strong></p>
  
  <p>Here is work in progress code I've been using (many iterations so I can easily debug it):</p>
  
  <pre><code>private void SaveBuildingBlock(string savedFile)
  {
    try
    {
      Microsoft.Office.Interop.Word.ApplicationClass wordApplication = null;
      wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
      Microsoft.Office.Interop.Word.Document wordDocument = wordApplication.Documents.OpenNoRepairDialog(savedFile);
  
      object missing = System.Type.Missing;
  
      Database.ToolbarItemsWordInsertFileData.FileData = FileHandler.FileByteLocked(savedFile);
      Database.ToolbarItemsWordInsertFileData.Id = 0;
      Database.ToolbarItemsWordInsertFileData.ToolbarItemId = ToolbarId;
      Database.ToolbarItemsWordInsertFileData.Save();
  
      ListBuildingBlocks(wordApplication, wordPage);
    }
    catch (Exception err)
    {
      Logger.Log(err);
    }
  }
  </code></pre>
  
  <p>Next method:</p>
  
  <pre><code>private void ListBuildingBlocks(Microsoft.Office.Interop.Word.Application wordApplication, WordPages wordPage)
  {
    Microsoft.Office.Interop.Word.WdBuildingBlockTypes type = wordPage == WordPages.CoverPage ? type = Microsoft.Office.Interop.Word.WdBuildingBlockTypes.wdTypeCoverPage : type = Microsoft.Office.Interop.Word.WdBuildingBlockTypes.wdTypeWatermarks;
    for (int i = 1; i &lt;= wordApplication.Templates.Count; i++)
    {
      try
      {
        if (wordApplication.Templates[i] != null)
        {
          Microsoft.Office.Interop.Word.Categories categories = wordApplication.Templates[i].BuildingBlockTypes.Item(type).Categories;
          for (int c = 1; c &lt;= categories.Count; c++)
          {
            try
            {
              //Category cat = categories.Application[0];
              Microsoft.Office.Interop.Word.BuildingBlocks buildingBlocks = wordApplication.Templates[i].BuildingBlockTypes.Item(type).Categories.Item(categories.Item(c).Name).BuildingBlocks;
              for (int b = 1; b &lt;= buildingBlocks.Count; b++)
              {
                try
                {
                  Microsoft.Office.Interop.Word.BuildingBlock buildingBlock = buildingBlocks.Item(b);
                  if (buildingBlock != null)
                  {
                    //saving to database occurs here
                  }
                }
                catch (Exception err)
                {
                  MessageBox.Show(err.Message);
                }
              }
            }
            catch (Exception err)
            {
              MessageBox.Show(err.Message);
            }
          }
        }
      }
      catch (Exception err)
      {
        MessageBox.Show(err.Message);
      }
    }
  }
  </code></pre>
  
  <p>The WordPages parameter [wordPage] is used in the application to specify if it's a Watermark or Cover Page (it's a property in this user control).</p>