Delete all in Footer
-
mardi 21 septembre 2010 17:16
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();
Toutes les réponses
-
mercredi 22 septembre 2010 06:28Modérateur
Hi Renley
How does it not work?
Version of Word?
Cindy Meister, VSTO/Word MVP -
mercredi 22 septembre 2010 16:23
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?
-
mercredi 22 septembre 2010 17:36Modérateur
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 locatedThat 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 -
mercredi 22 septembre 2010 17:57
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.
-
jeudi 23 septembre 2010 21:02
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();- Marqué comme réponse Renley jeudi 23 septembre 2010 21:02

