Quantcast
Channel: Word for Developers forum
Viewing all articles
Browse latest Browse all 4350

How to add a textbox to every footer type with C# and Microsoft.Office.Interop.Word

$
0
0

I'm writing a Word Add-in that is supposed to put a textbox in the footer of every page, even when footer options such as "Different First Page" and "Different Odd and Even Pages" are checked. I'm seeing some inconsistency between .doc, 2007 .docx, 2010 .docx, and 2013 .docx files.

With 2010 .docx files, this is easy to achieve with the following code, which I was expecting to work across all document formats:

int i = 0, j = 0;
Microsoft.Office.Interop.Word.Shape myIDbox = null;
foreach (Section wordSection in Application.ActiveDocument.Sections)
{
     i++;
     foreach (Microsoft.Office.Interop.Word.HeaderFooter footerRange in wordSection.Footers)
     {
          j++;
          myIDbox = footerRange.Shapes.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, 18, Application.ActiveDocument.PageSetup.PageHeight - 36, 300, 36);
     }
}

In Word 2010, while modifying 2010 .docx files, this works as expected and places a textbox in each footer type correctly.

In word 2010 or 2013, while modifying any file besides 2010 .docx files, this will place 3 overlapping textboxes in the same footer type (I believe Primary).

Is there any way to get this code working for all document formats?

 Thanks!

Viewing all articles
Browse latest Browse all 4350

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>