none
Header/Footer active when reading text from footer (Word2010, RTF file formular, C# VS2010) RRS feed

  • Question

  • Hello,

    1. our Oracle system fill a RTF template (3 tables with data text in plain page), and footer (data text)
    2. I have developped an application under C# which reads data from the rtf file. Software takes control of Word, then upload the rtf file. I encounter a strange problem when I read the footer (1 table, 1 row and 3 columns). When the code access to footer, it become active (similar effect when you double click on it when you work under Word). The problem is that a carriage return appears in the header, causing a 2nd page to appear. I have checked on Word what's going wrong, but no success.
    3. Here is the code :

    variable declared on cGlobalVar class to get access on whome software

    public static  Microsoft.Office.Interop.Word.Application    varWordApp    = new Microsoft.Office.Interop.Word.Application();

    public static Microsoft.Office.Interop.Word.Document            varWordDoc    = new Microsoft.Office.Interop.Word.Document();

    function that read the rtf file

       Sections oSection = cGlobalVar.varWordApp.ActiveDocument.Sections;
       HeaderFooter oFooter = oSection[1].Footers[WdHeaderFooterIndex.wdHeaderFooterFirstPage];
       Range oRange = oFooter.Range.Tables[1].Range;  //when this is called, header/footer become active
                        
       strBuffer = oRange.Text;  //data that I can read works fine
       oRange = oSection[1].Range.Tables[1].Range;  //try to reaffect the portability of the object, header/footer stay active

    I don't find why this problem occurs. The only way to eliminate the 2nd page is to reduce the font size of the header return carriage.

    Kindly, Herve

    • Moved by Cindy Meister MVPModerator Tuesday, December 4, 2012 7:46 AM not using VSTO technology (From:Visual Studio Tools for Office)
    Monday, December 3, 2012 4:14 PM

Answers

  • Hi Herve

    1. You can declare the varWordApp object at the class/global level, but you should not instantiate it there.

    2. Same remark about instatiating outside a method, pluse: NEVER use the keyword "new" with a Document object. Word will do "something", but it's never the right thing and it will cause unexplainable issues. New was not meant to be used by the developer. If you want a new document use the varWordApp.Documents.Add() method.

    3. When using the Documents.Add method you can directly assign the Document object being created to your object variable: varWordDoc = varWordApp.Documents.Add(). Since that's the object you want to work with, do NOT use ActiveDocument. So: Sections oSection = varWordDoc.Sections;

    4. If you haven't added a table to a Range you cannot address a table in a document object. Since you apparently want to start with a clean, new document, this makes no sense:
      Range oRange = oFooter.Range.Tables[1].Range;

    First, you must do something like:
      Word.Table oTable = varWordDoc.Tables.Add(oFooter.Range, 1, 3, Word.WdDefaultTableBehavior.wdWord8TableBehavior, Word.WdAutoFitBehavior.wdAutoFitFixed)

    Correct these things then test again. (Actually, I'm surprised that (4) isn't causing an error message - it should be!)

    Note: I'm interpreting your question literally, that Word is actually moving into the Header/Footer editing mode. However, I get the feeling that you may simply mean that you're getting a paragraph in the Header when content is put in the footer. This is by design and you cannot prevent it simply because you're adding content using code rather than as an end-user. You're activating the Header/Footer management in the section, which puts content in its headers/footers. You may need to check and adjust some Page Layout settings - take a look at the "Layout" tab in the Page Setup dialog box. It lets you adjust the distance the header/footer start from the edge of the page. You might try decreasing that setting to give the header more room before it starts impinging on the body of the document. But in any case, you should fix the points I list above as they are critical to working with the Word API.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, December 4, 2012 9:48 AM
    Moderator

All replies

  • Hi Herve

    1. You can declare the varWordApp object at the class/global level, but you should not instantiate it there.

    2. Same remark about instatiating outside a method, pluse: NEVER use the keyword "new" with a Document object. Word will do "something", but it's never the right thing and it will cause unexplainable issues. New was not meant to be used by the developer. If you want a new document use the varWordApp.Documents.Add() method.

    3. When using the Documents.Add method you can directly assign the Document object being created to your object variable: varWordDoc = varWordApp.Documents.Add(). Since that's the object you want to work with, do NOT use ActiveDocument. So: Sections oSection = varWordDoc.Sections;

    4. If you haven't added a table to a Range you cannot address a table in a document object. Since you apparently want to start with a clean, new document, this makes no sense:
      Range oRange = oFooter.Range.Tables[1].Range;

    First, you must do something like:
      Word.Table oTable = varWordDoc.Tables.Add(oFooter.Range, 1, 3, Word.WdDefaultTableBehavior.wdWord8TableBehavior, Word.WdAutoFitBehavior.wdAutoFitFixed)

    Correct these things then test again. (Actually, I'm surprised that (4) isn't causing an error message - it should be!)

    Note: I'm interpreting your question literally, that Word is actually moving into the Header/Footer editing mode. However, I get the feeling that you may simply mean that you're getting a paragraph in the Header when content is put in the footer. This is by design and you cannot prevent it simply because you're adding content using code rather than as an end-user. You're activating the Header/Footer management in the section, which puts content in its headers/footers. You may need to check and adjust some Page Layout settings - take a look at the "Layout" tab in the Page Setup dialog box. It lets you adjust the distance the header/footer start from the edge of the page. You might try decreasing that setting to give the header more room before it starts impinging on the body of the document. But in any case, you should fix the points I list above as they are critical to working with the Word API.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, December 4, 2012 9:48 AM
    Moderator
  • Hi Cindy,
    Thanks a lot for yours advices.
    I have done some modification of the code :

    =========================================================================================
    Word.Document varWordDoc;//is declared now in the class cServiceReport, which fServiceReport is a function which read the rtf template

    Sections oSection = cGlobalVar.varWordApp.ActiveDocument.Sections;
    HeaderFooter oFooter = oSection.First.Footers[WdHeaderFooterIndex.wdHeaderFooterFirstPage];

    oFooter.Range.Text.CopyTo(1, destination, 0, 50);//when this is called, a return carriage still appears

    Affecting the part of rtf document into C# object works fine.
    When I am in debug mode, if I move the mouse cursor to part of function to get the information (eg, oFooter.Range.Text.CopyTo(1, destination, 0, 50); cursor placed to Text), then I get the return carriage in the rtf template.
    If I execute the function by F10, I get th return carriage in the header.

    And trying to remove it under Word2010, with removed protection of rtf formular (menu Developper, Restrict editing, Stop Protection) is not possible.

    So, I don't know if there is a problem with rtf template (bad design) or problem with the Word COM reference.
    At least, I think I will change the template form in order to avoid to go to footer.
    Kindly, Herve
    Wednesday, December 5, 2012 3:53 PM