none
How to set margin of word document using c# RRS feed

  • Question

  • Hi I tried to set margin of word document like this 

    oWord.ActiveDocument.PageSetup.TopMargin = (float)100;                oWord.ActiveDocument.PageSetup.BottomMargin  = (float)100;
    oWord.ActiveDocument.PageSetup.RightMargin  = (float)100;
    oWord.ActiveDocument.PageSetup.LeftMargin = (float)100;

    But something going wrong.

    I want to set margin Like word 2007 => Page Layout  =>  Margins  =>  Narrow

    (Top       0.5"         Bottom         0.5"

    Left        0.5"         Right             0.5")

       

    • Moved by Lisa Zhu Monday, June 25, 2012 9:59 AM Office issue (From:Visual C# Language)
    Friday, June 22, 2012 9:17 AM

Answers

  • Hi Bhagawat,

    The below is my full code after adaptation. I've defined FilePath as object.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Word = Microsoft.Office.Interop.Word;
    using System.Windows.Forms;
    
    namespace ConsoleApplication_Word_SetMargin
    {
        class Program
        {
            static void Main(string[] args)
            {
                Word._Application myWD = new Word.Application();
                myWD.Visible = true;
    
                object FilePath = @"D:\Narrow.docx";
                object oMissing = Type.Missing;
    
                Word._Document myDoc = myWD.Documents.Open(ref FilePath, 
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
                    ref oMissing, ref oMissing, ref oMissing,  ref oMissing, 
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
                    ref oMissing, ref oMissing, ref oMissing);
                
                float TopMargin = myDoc.PageSetup.TopMargin; 
                float BottomMargin = myDoc.PageSetup.BottomMargin;
                float RightMargin = myDoc.PageSetup.RightMargin;
                float LeftMargin = myDoc.PageSetup.LeftMargin;
                
                MessageBox.Show(String.Format("TopMargin: {0}\nBottomMargin: {1}\nRightMargin: {2}\nLeftMargin: {3}\n",TopMargin, BottomMargin, RightMargin, LeftMargin));
            }
        }
    }

    In order to make the code compiled successfully, please check:
    1. Whether Narrow.docx has been created before running the code.
    2. Whether Office.Interop has been referenced to the right version. (Office 2007 <--> 12; Office 2010 <--> 14)

    If the error or exception still occur, please tell me your Office/VS/.Net versions.

    Hope it helps.

    Best regards,
    Quist



    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, June 26, 2012 10:59 AM
    Moderator
  • Hi Bhagawat,

    Thank you for your last reply.

    Still I've no idea what kind of issue you are facing. Could you please describe you issue more detailed?

    If you mean that your code doesn't perform the same effect as manually set. Then you can create a Word document named Narrow.docx set the Margins to Narrow, and then use code below to get the exact value of margins.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Word = Microsoft.Office.Interop.Word;
    using System.Windows.Forms;
    
    namespace ConsoleApplication_Word_SetMargin
    {
        class Program
        {
            static void Main(string[] args)
            {
                Word._Application myWD = new Word.Application();
                myWD.Visible = true;
    
                string FilePath = @"D:\Narrow.docx";
    
                Word._Document myDoc = myWD.Documents.Open(FilePath);
                
                float TopMargin = myDoc.PageSetup.TopMargin; 
                float BottomMargin = myDoc.PageSetup.BottomMargin;
                float RightMargin = myDoc.PageSetup.RightMargin;
                float LeftMargin = myDoc.PageSetup.LeftMargin;
                
                MessageBox.Show(String.Format("TopMargin: {0}\nBottomMargin: {1}\nRightMargin: {2}\nLeftMargin: {3}\n",TopMargin, BottomMargin, RightMargin, LeftMargin));
            }
        }
    }

    If I haven't understood your issue correctly, please feel free to let me know.

    Best regards,
    Quist


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, June 26, 2012 7:17 AM
    Moderator

All replies

  • Anybody have any idea....
    Monday, June 25, 2012 9:27 AM
  • Bhagawat,

    Try this forum, it's specific for development for Word


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Monday, June 25, 2012 9:46 AM
  • Hi Bhagawat,

    Thank you for posting in the MSDN Forum.

    I've tried your code, however I cannot reproduce your scenario when I set the Margin to (float)100. Have you tried Try Catch to see what the exception that caused your scenario is?
    Until now, I've only get exception when I set the margin to a large number, for example 500.

    Hope it helps.

    Best regards,
    Quist


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    Monday, June 25, 2012 11:54 AM
    Moderator
  • Thanks  Quist Zhang,

    It doesn't throw any exception. I have also post this thread to word for Developers.

     

    Tuesday, June 26, 2012 5:41 AM
  • Hi Bhagawat,

    Thank you for your last reply.

    Still I've no idea what kind of issue you are facing. Could you please describe you issue more detailed?

    If you mean that your code doesn't perform the same effect as manually set. Then you can create a Word document named Narrow.docx set the Margins to Narrow, and then use code below to get the exact value of margins.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Word = Microsoft.Office.Interop.Word;
    using System.Windows.Forms;
    
    namespace ConsoleApplication_Word_SetMargin
    {
        class Program
        {
            static void Main(string[] args)
            {
                Word._Application myWD = new Word.Application();
                myWD.Visible = true;
    
                string FilePath = @"D:\Narrow.docx";
    
                Word._Document myDoc = myWD.Documents.Open(FilePath);
                
                float TopMargin = myDoc.PageSetup.TopMargin; 
                float BottomMargin = myDoc.PageSetup.BottomMargin;
                float RightMargin = myDoc.PageSetup.RightMargin;
                float LeftMargin = myDoc.PageSetup.LeftMargin;
                
                MessageBox.Show(String.Format("TopMargin: {0}\nBottomMargin: {1}\nRightMargin: {2}\nLeftMargin: {3}\n",TopMargin, BottomMargin, RightMargin, LeftMargin));
            }
        }
    }

    If I haven't understood your issue correctly, please feel free to let me know.

    Best regards,
    Quist


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, June 26, 2012 7:17 AM
    Moderator
  • Hi Bhagawat, 

    I tried your codes and it is the same as Quist Zhang describes- No error or exception captured unless we set its two large. If you may, please post more code for us to figure out how it is going. I am just thinking that it may be your other part of codes is not right.

    Best Regards

    Tuesday, June 26, 2012 7:34 AM
  • Hi see Images..

    How to set margin like the above image.

    I tried like this

     oWord.ActiveDocument.PageSetup.TopMargin = (float)30;
     oWord.ActiveDocument.PageSetup.BottomMargin = (float)30;
     oWord.ActiveDocument.PageSetup.RightMargin = (float)30;
     oWord.ActiveDocument.PageSetup.LeftMargin = (float)30;


    but not exactly set the margins(It's approximately)

      As Quist Zhang suggest the code to find out margins like 

    Word._Document myDoc = myWD.Documents.Open(FilePath);

    but it gives me an error of (1+ arguments) 

    • Edited by Bhagawat Tuesday, June 26, 2012 9:26 AM
    Tuesday, June 26, 2012 9:21 AM
  • Hi Bhagawat,

    Thank you for your last reply.

    Please replace the snippet of code that cause the exception with the code below.

    Word._Document myDoc = myWD.Documents.Open(FilePath, 
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
        Type.Missing, Type.Missing, Type.Missing,  Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
        Type.Missing, Type.Missing, Type.Missing);

    Besides, based on my research when margin set to narrow, the value should be 36.

    Hope it helps.

    Best regards,
    Quist


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, June 26, 2012 9:48 AM
    Moderator
  • Hello Quist Zhang,

    I tried code that you are provided but it gives me an error

    Error 2 The best overloaded method match for 'Microsoft.Office.Interop.Word.Documents.Open(ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object)' has some invalid arguments

    and 

    Argument '1': cannot convert from 'string' to 'ref object'

    Argument '2' must be passed with the 'ref' keyword

    So I tried like 

      object oMissing = System.Reflection.Missing.Value;
    
      Word._Document myDoc = myWD.Documents.Open(FilePath, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
         ref oMissing, ref oMissing, ref oMissing, ref oMissing,
         ref oMissing, ref oMissing, ref oMissing, ref oMissing,
         ref oMissing, ref oMissing, ref oMissing);

    but still gets error.  



    Tuesday, June 26, 2012 10:04 AM
  • Hi Bhagawat,

    The below is my full code after adaptation. I've defined FilePath as object.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Word = Microsoft.Office.Interop.Word;
    using System.Windows.Forms;
    
    namespace ConsoleApplication_Word_SetMargin
    {
        class Program
        {
            static void Main(string[] args)
            {
                Word._Application myWD = new Word.Application();
                myWD.Visible = true;
    
                object FilePath = @"D:\Narrow.docx";
                object oMissing = Type.Missing;
    
                Word._Document myDoc = myWD.Documents.Open(ref FilePath, 
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
                    ref oMissing, ref oMissing, ref oMissing,  ref oMissing, 
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
                    ref oMissing, ref oMissing, ref oMissing);
                
                float TopMargin = myDoc.PageSetup.TopMargin; 
                float BottomMargin = myDoc.PageSetup.BottomMargin;
                float RightMargin = myDoc.PageSetup.RightMargin;
                float LeftMargin = myDoc.PageSetup.LeftMargin;
                
                MessageBox.Show(String.Format("TopMargin: {0}\nBottomMargin: {1}\nRightMargin: {2}\nLeftMargin: {3}\n",TopMargin, BottomMargin, RightMargin, LeftMargin));
            }
        }
    }

    In order to make the code compiled successfully, please check:
    1. Whether Narrow.docx has been created before running the code.
    2. Whether Office.Interop has been referenced to the right version. (Office 2007 <--> 12; Office 2010 <--> 14)

    If the error or exception still occur, please tell me your Office/VS/.Net versions.

    Hope it helps.

    Best regards,
    Quist



    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, June 26, 2012 10:59 AM
    Moderator
  • Hey Hi Quist Zhang Thanks wonderful help. Now i get exact margin. Thanks. I have one question can i continue with this thread or start new thread

    Q. How to add line in between two columns and set spacing in between two columns. I have tried like this but when i set

    LineBetween =1 then it gives me an error like

    Value doesn't fall within the expected range.

     oDoc.PageSetup.TextColumns.LineBetween = 0;
    oDoc .PageSetup .TextColumns .Spacing =(float)0.5;

     Thanks in advance.

    Tuesday, June 26, 2012 11:44 AM
  • Hi Bhagawat,

    Glad to see that you have solved your issue. Please start a new thread, and I will follow it.

    Best regards,
    Quist


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, June 27, 2012 1:43 AM
    Moderator