<?xml version="1.0"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>DeleteVBAProject</Title>
      <Author>Kevin Boske</Author>
      <Description>Given a file name, delete the VBAProject and its relationship (does not convert the file to macro-free</Description>
      <Shortcut>DeleteVBAProject</Shortcut>
    </Header>
    <Snippet>
      <References>
        <Reference>
          <Assembly>WindowsBase</Assembly>
          <URL>windowsbase.dll</URL>
        </Reference>
      </References>
      <Imports>
        <Import>
          <Namespace>System.IO</Namespace>
        </Import>
        <Import>
          <Namespace>System.IO.Packaging</Namespace>
        </Import>
        <Import>
          <Namespace>System.Collections.Generic</Namespace>
        </Import>
      </Imports>
      <Code Language="csharp" Kind="method decl">
        <![CDATA[/// <summary>
        /// Delete the VBA project part and relationship in any file type.
        /// </summary>
        /// <param name="fileName">name of the file to delete</param>
        public void DeleteVBAPart(string fileName)
        {
            string officeDocRelType = @"http://schemas.microsoft.com/office/2006/relationships/officeDocument";
            const string vbaRelationshipType = "http://schemas.microsoft.com/office/2006/relationships/vbaProject";
            PackagePart documentPart = null;
            Uri documentUri = null;

            //Open the package with Read permission
            using (Package officePackage = Package.Open(fileName, FileMode.Open, FileAccess.ReadWrite))
            {
                //Get the start part
                foreach (PackageRelationship relationship in officePackage.GetRelationshipsByType(officeDocRelType))
                {
                    // There should only be one document part in the package
                    documentUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri);
                    documentPart = officePackage.GetPart(documentUri);
                    break;
                }
                //Find the VBA project part.
                foreach (System.IO.Packaging.PackageRelationship relationship in documentPart.GetRelationshipsByType(vbaRelationshipType))
                {
                    Uri vbaUri = PackUriHelper.ResolvePartUri(documentUri, relationship.TargetUri);
                    PackagePart vbaPart = officePackage.GetPart(vbaUri);
                    //Delete the VBA part
                    officePackage.DeletePart(vbaUri);
                    //Remove the VBA relationship
                    documentPart.DeleteRelationship(relationship.Id);
                    //There is only VBA part:
                    break;
                }
                //Close the package
                officePackage.Close();
            }
         }
]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>