none
Top Margin is not working properly when using with/without Range property from HeaderFooter Interface. RRS feed

  • Question

  • Hello,

    Actually I have noticed one issue when try to apply the top margin with empty header and the top margin is not get affect, for recreate the issue  I have given the section of code which going to understand the problem.

    And for recreating the issue please comment/uncomment the below line of code from the below section of code.

    Microsoft.Office.Interop.Word.Range oHeaderFooterRange = oHeaderFooter.Range;

    Microsoft.Office.Interop.Word.Document oDocument = documents.Open(ref objFileName, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing); Microsoft.Office.Interop.Word.Application oApplication = oWordDocument.Application;

    Microsoft.Office.Interop.Word.Options oOptions = application.Options;
    WdMeasurementUnits measurementUnits = oOptions.MeasurementUnit;
    oOptions.MeasurementUnit = WdMeasurementUnits.wdPoints;

    Microsoft.Office.Interop.Word.Window oWindow = oApplication.ActiveWindow; Microsoft.Office.Interop.Word.Pane oPane = oWindow.ActivePane; Microsoft.Office.Interop.Word.View oView = oPane.View; if (oView.Type == WdViewType.wdNormalView || oView.Type == WdViewType.wdOutlineView || oView.Type == WdViewType.wdMasterView) oView.Type = WdViewType.wdPrintView; oView.SeekView = WdSeekView.wdSeekCurrentPageFooter;

    Microsoft.Office.Interop.Word.Selection oSelection = oApplication.Selection; Microsoft.Office.Interop.Word.HeaderFooter oHeaderFooter = oSelection.HeaderFooter; // below line cause the issue. Microsoft.Office.Interop.Word.Range oHeaderFooterRange = oHeaderFooter.Range;
    oView.SeekView = WdSeekView.wdSeekMainDocument; Microsoft.Office.Interop.Word.Sections sections = oDocument.Sections; long nSecCount = sections.Count; for (int iSec = 1; iSec <= nSecCount; iSec++) { Microsoft.Office.Interop.Word.Section section = sections[iSec]; Microsoft.Office.Interop.Word.PageSetup pageSetup = section.PageSetup; pageSetup.PaperSize = WdPaperSize.wdPaperA3; pageSetup.LeftMargin = 1; pageSetup.TopMargin = 1; pageSetup.RightMargin = 1; pageSetup.BottomMargin = 1; } oDocument.PrintOut(ref Background, ref missing, ref Range, ref missing, ref missing, ref missing, ref missing, ref Copies, ref missing, ref PageType, ref PrintToFile, ref Collate, ref missing, ref ManualDuplexPrint, ref PrintZoomColumn, ref PrintZoomRow, ref PrintZoomPaperWidth, ref PrintZoomPaperHeight);

    Please give me a solution of this improper behaviour.

    Thanks
    Karthikeyan


    karthikeyan



    Thursday, August 15, 2013 11:55 AM

Answers

  • Hello,

    Below code which fixes the above issues

     WdHeaderFooterIndex wdHeaderFooterIndex;
                    int bDifferentFirstPageHeaderFooter = pageSetup.DifferentFirstPageHeaderFooter;
                    if (bDifferentFirstPageHeaderFooter == 1)
                        wdHeaderFooterIndex = WdHeaderFooterIndex.wdHeaderFooterFirstPage;                
                    else
                       wdHeaderFooterIndex = WdHeaderFooterIndex.wdHeaderFooterPrimary;
    
                    Microsoft.Office.Interop.Word.Range oHeaderRange = section.Headers[wdHeaderFooterIndex].Range;
                    Microsoft.Office.Interop.Word.Range oFooterRange = section.Footers[wdHeaderFooterIndex].Range;
                    string sHeader = oHeaderRange.Text;
                    string sFooter = oFooterRange.Text;
               
                    if (sHeader.Length <= 1)
                    {
    
                        pageSetup.HeaderDistance = 0;
                        float fTopMargin = pageSetup.TopMargin;
                        pageSetup.TopMargin = -fTopMargin;
                        
                    }

    Thanks

    Karthikeyan


    Thursday, August 22, 2013 1:55 PM

All replies

  • Hi Karthikeyan,

    I failed to reproduce you issue on my lab machine (Office 2013).

    Here is my test code:

            static void Main(string[] args)
            {
                var app = new Application();
    
                Microsoft.Office.Interop.Word.Document oDocument = app.Documents.Open(@"D:\Drafts\HelloWorld.docx");
    
                Microsoft.Office.Interop.Word.Application oApplication = oDocument.Application;
    
    
                Microsoft.Office.Interop.Word.Options oOptions = oApplication.Options;
                WdMeasurementUnits measurementUnits = oOptions.MeasurementUnit;
                oOptions.MeasurementUnit = WdMeasurementUnits.wdPoints;
    
                Microsoft.Office.Interop.Word.Window oWindow = oApplication.ActiveWindow;
                Microsoft.Office.Interop.Word.Pane oPane = oWindow.ActivePane;
                Microsoft.Office.Interop.Word.View oView = oPane.View;
                if (oView.Type == WdViewType.wdNormalView || oView.Type == WdViewType.wdOutlineView || oView.Type == WdViewType.wdMasterView)
                    oView.Type = WdViewType.wdPrintView;
                oView.SeekView = WdSeekView.wdSeekCurrentPageFooter;
    
    
                Microsoft.Office.Interop.Word.Selection oSelection = oApplication.Selection;
                Microsoft.Office.Interop.Word.HeaderFooter oHeaderFooter = oSelection.HeaderFooter;
    
                Microsoft.Office.Interop.Word.Range oHeaderFooterRange = oHeaderFooter.Range;
                oView.SeekView = WdSeekView.wdSeekMainDocument;
                Microsoft.Office.Interop.Word.Sections sections = oDocument.Sections;
                long nSecCount = sections.Count;
                for (int iSec = 1; iSec <= nSecCount; iSec++)
                {
                    Microsoft.Office.Interop.Word.Section section = sections[iSec];
                    Microsoft.Office.Interop.Word.PageSetup pageSetup = section.PageSetup;
                    pageSetup.PaperSize = WdPaperSize.wdPaperA3;
                    pageSetup.LeftMargin = 1;
                    pageSetup.TopMargin = 1;
                    pageSetup.RightMargin = 1;
                    pageSetup.BottomMargin = 1;
                }
    
                oDocument.Save();
    
                app.Quit();
            }

    The top margin is affected as expected:


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, August 16, 2013 8:40 AM
    Moderator
  • Hello,

    Please try in Office 2007/2003 and see the behavior with and without below line of code.

    Microsoft.Office.Interop.Word.Range oHeaderFooterRange = oHeaderFooter.Range;

    Thanks

    karthikeyan

     
    Friday, August 16, 2013 1:00 PM
  • Hi karthikeyan,

    I tried to set the top margin to 1pt in Word 2007 manually and a warning message popped up:

    Then I click Fix, and margins will be set as below:

    Considering you will print this document out, I suggest you setting your margin greater than 12pt, otherwise the document content will be outside of the printable area of the page.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Saturday, August 17, 2013 9:15 AM
    Moderator
  • Hello Jeffery,

    Thanks,

    My problem is when I use with below code, the document layout is different and I commented the below code the document layout is different why it is happen like that?

    Microsoft.Office.Interop.Word.Range oHeaderFooterRange = oHeaderFooter.Range;

    For your reference I have attached two different images please analysis and give me solution for this issue.


    karthikeyan

    Monday, August 19, 2013 11:48 AM
  • Hi Karthik

    If you print the result, rather than just viewing it on-screen, do you see the different space at the top? Or do both pages print the same?

    From your screen shot, it appears to me that you may be surpressing the dispaly of "white space" between pages. In other words, what you're seeing is a display setting, but isn't how the document will actually print.

    If that's the case, hover your mouse pointer on the edge of the page and you should see instructions appear on how to toggle the behavior so that you always see the entire page. (I can't tell you because the commands are different in almost every version of Word. Sometimes it's a single click, sometimes a double-click, etc. It should also be somewhere in the "Options".)


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, August 21, 2013 10:54 AM
    Moderator
  • Hello,

    Printed to virtual printer it has extra white space when using the property HeaderFooter.Range but without this property it doesnot have extra white space and also I have tried with option properties such as "AutoFormatApplyHeadings" and the white space still exist.

    Thanks & Regards
    Karthikeyan.G 


    karthikeyan


    Thursday, August 22, 2013 7:04 AM
  • Hello,

    Below code which fixes the above issues

     WdHeaderFooterIndex wdHeaderFooterIndex;
                    int bDifferentFirstPageHeaderFooter = pageSetup.DifferentFirstPageHeaderFooter;
                    if (bDifferentFirstPageHeaderFooter == 1)
                        wdHeaderFooterIndex = WdHeaderFooterIndex.wdHeaderFooterFirstPage;                
                    else
                       wdHeaderFooterIndex = WdHeaderFooterIndex.wdHeaderFooterPrimary;
    
                    Microsoft.Office.Interop.Word.Range oHeaderRange = section.Headers[wdHeaderFooterIndex].Range;
                    Microsoft.Office.Interop.Word.Range oFooterRange = section.Footers[wdHeaderFooterIndex].Range;
                    string sHeader = oHeaderRange.Text;
                    string sFooter = oFooterRange.Text;
               
                    if (sHeader.Length <= 1)
                    {
    
                        pageSetup.HeaderDistance = 0;
                        float fTopMargin = pageSetup.TopMargin;
                        pageSetup.TopMargin = -fTopMargin;
                        
                    }

    Thanks

    Karthikeyan


    Thursday, August 22, 2013 1:55 PM