none
Delete all in Footer

    Question

  • Ok, this is a simple question i think.  I have a mix of contentControls and text in my documents Footer.  I want to delete it all programatically.  I have tried the following but it does not work:

    rng = Globals.ThisAddIn.Application.ActiveDocument.Sections[1].Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
    rng.Delete();

    Tuesday, September 21, 2010 5:16 PM

Answers

  • Ha!  I got it to work!

    Ok, what i was doing wrong was something along the lines of "going too deep into the document"

     

    my non-working code =

    rng = Globals.ThisAddIn.Application.ActiveDocument.Sections[1].Footers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range;

    rng.Application.ActiveDocument .Tables[1].Delete();

     

    However it works when i do the following:

    rng = Globals.ThisAddIn.Application.ActiveDocument.Sections[1].Footers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range;
    rng.Tables[1].Delete();

    • Marked as answer by Renley Thursday, September 23, 2010 9:02 PM
    Thursday, September 23, 2010 9:02 PM

All replies

  • Hi Renley

    How does it not work?

    Version of Word?


    Cindy Meister, VSTO/Word MVP
    Wednesday, September 22, 2010 6:28 AM
  • Sure. 

     

    I am using C#

    Word 2007

    I am executing this code from a Windows Form.

     

    And specifically; it does absolutely nothing.  The code does not error, and the range deletes nothing.  All text and controls are still there. 

     

    Perhaps I am simply going about this the wrong way?  How can I simply select the Footer and delete all?

     

     

    Wednesday, September 22, 2010 4:23 PM
  • Hi Renley

    <<I am executing this code from a Windows Form.>>

    Ahhh.

    <<rng = Globals.ThisAddIn.Application.ActiveDocument.Sections[1].Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;>>

    You can't use Globals.ThisAddin in a Windows Forms application, only from within a VSTO Add-in. I'm actually surprised you're not getting compiler errors...

    Could you show me how your code is declaring and instantiating:

    - the Word application object
    - the Word document in which the header/footer is located

    That way I can give you sample code that's meaningful in your project's context and perhaps catch any other problems waiting to jump out and trip you up :-)


    Cindy Meister, VSTO/Word MVP
    Wednesday, September 22, 2010 5:36 PM
  • Ok, here goes:

    using Word = Microsoft.Office.Interop.Word;

    Word.ContentControls conCtrls;

    conCtrls = Globals.ThisAddIn.Application.ActiveDocument.SelectContentControlsByTag("pic1");

    ^ So the above works, and allows me to access the controls on a loaded document which is also a template

     

    //***this loads the specified template into a new document, we can now edit it or access its controls
    object tempName = _templatefilepath + "\\Letter.dotx";
    object newTemp = false;
    object documentType = Word.WdNewDocumentType.wdNewBlankDocument;
    object visible = true;

    Globals.ThisAddIn.Application.Documents.Add(ref tempName, ref newTemp, ref documentType, ref visible);
    //***

    ^ This code also works fine and allows me access to the document.

     

     

    • Marked as answer by Renley Thursday, September 23, 2010 9:02 PM
    • Unmarked as answer by Renley Thursday, September 23, 2010 9:02 PM
    Wednesday, September 22, 2010 5:57 PM
  • Ha!  I got it to work!

    Ok, what i was doing wrong was something along the lines of "going too deep into the document"

     

    my non-working code =

    rng = Globals.ThisAddIn.Application.ActiveDocument.Sections[1].Footers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range;

    rng.Application.ActiveDocument .Tables[1].Delete();

     

    However it works when i do the following:

    rng = Globals.ThisAddIn.Application.ActiveDocument.Sections[1].Footers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range;
    rng.Tables[1].Delete();

    • Marked as answer by Renley Thursday, September 23, 2010 9:02 PM
    Thursday, September 23, 2010 9:02 PM