none
"framed paragraph" Exception when trying to read sections PageSetup RRS feed

  • Question

  • hi,
    i have a method which reads all printer trays for each page.
    To do this i have to iterate through all sections an read the section.PageSetup-Property.
    this is my code:
    var sections = activeDocument.Sections;
    
    var nextPage = 0;
    
    foreach(Section section in sections)
    {
    	var range = section.Range;
    	var endPage = (int)range.Information[WdInformation.wdActiveEndPageNumber];
    	
    	for (var i = nextPage; i < endPage; i++)
    	{
    		try
    		{
    			var pageSetup = section.PageSetup;
    			list.Add((int)pageSetup.FirstPageTray);
    				
    			break;
    		}
    		catch (System.Runtime.InteropServices.COMException ex)
    		{
    		}
    	}
    		
    
    	nextPage = endPage;
    }
    

    With most of the tested doc-files it works perfect.
    But i have one document which starts with a table on the first page.
    When i try to read the PageSetup of this section i get this Exception
    "this method or property is not available because the object refers to a framed paragraph". 
    ErrorCode=-2146823683
    The problem is, that the cursor seems to be inside the table and thats the reason why i'm not able to read the Section.PageSetup.
    Is there a way to handle this problem?
    I found this "solution":
    var selection = sections.Application.Selection;
    selection.HomeKey(WdUnits.wdStory, WdMovementType.wdMove);
    selection.InsertBreak();
    

    but it doesn't work.
    any other ideas?
    thanks
    Tobias

    Friday, January 13, 2012 9:09 AM

Answers

  • hi,

     

    i finally found the reason why I was not able to read the PageSetup of the section.

    The document contains a frame which caused the problem. 

    I just opend the properties of this frame and clicked "OK" without changing anything.

    After that the text below the frame moved up (maybe just 1mm) and then everything worked fine.

    I don't know why but it's a problem of our template.dotx.

     

    Thanks anyway for your hints!!!

     

    Tobias

    Tuesday, January 17, 2012 11:09 AM

All replies

  • Hi Tobias,

    Is this happening because the table continues onto the next page of the document?

    Could you put in a test (this is pseudo code) something like ...

    If selection.information(wdWithinTable) then selection.endof(unit:=wdTable)


    Kind Regards, Rich ... http://greatcirclelearning.com
    Friday, January 13, 2012 1:46 PM
  • hi Rich,

    no, the Table (which contains the addressee information) is on the first page the first element.Its maybe 10cm height.

    Below the table there is some text (also on the first page).

    When i Press "ctrl"+"pos1/home" it jumps directly into the first row of the table and not above it because there is nothing above ;)

    i tried to jump out of the table but without success...

    I have to check your "pseudo code" :)

    Monday, January 16, 2012 12:34 PM
  • Hi Tobias

    For the pseudocode, you need the selection.get_Information method in C#...


    Cindy Meister, VSTO/Word MVP
    Monday, January 16, 2012 3:20 PM
    Moderator
  • hi Cindy,

    thanks for your hint.

    I tried it but without success...same exception appears.

    this is my code:

    var selection = section.Application.Selection;
     if (selection.get_Information(WdInformation.wdWithInTable))
     {
          selection.EndOf(WdUnits.wdTable);
           var stillWithin = selection.get_Information(WdInformation.wdWithInTable);
         //The selection is still within the table
     }
    


    Do i have to "refresh" something that the cursur really jumps out of the table?

    thanks

    Tobias

     

    Tuesday, January 17, 2012 6:39 AM
  • Hi Tobias

    If it's still in the table, then use Selection.MoveStart with an appropriate unit and count to move out of the table (possibly, Word.WdUnit.wdCharacter plus 1 (or 2)).

    Personally, I prefer to work with the Range object and, when wanting to get out of a table, collapse the Range. Something like:

    if (selection.get_Information(WdInformation.wdWithInTable))
    {
          Word.Range rng = selection.Tables[1].Range;
          object collapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
          rng.Collapse(ref collapseEnd);
          rng.Select();
     }


    Cindy Meister, VSTO/Word MVP
    Tuesday, January 17, 2012 8:14 AM
    Moderator
  • hi Cindy,

    thanks for your answer, but i think its not the table which causes this problem.

    I checked the document and found a frame (in german its a "positionsrahmen" -in english maybe position frame?) around the table...do you know how to jump out of the frame and not just out of the table?

    sorry that I didn't mention the frame (I didn't create the document;) )!

     

    Tobias

    Tuesday, January 17, 2012 9:31 AM
  • hi,

     

    i finally found the reason why I was not able to read the PageSetup of the section.

    The document contains a frame which caused the problem. 

    I just opend the properties of this frame and clicked "OK" without changing anything.

    After that the text below the frame moved up (maybe just 1mm) and then everything worked fine.

    I don't know why but it's a problem of our template.dotx.

     

    Thanks anyway for your hints!!!

     

    Tobias

    Tuesday, January 17, 2012 11:09 AM