none
Value out range while trying to set pagesetup.outer margin in vb.net RRS feed

  • Question

  • Hi,

       I am trying to set the LeftMargin using vb.net for a document which is having more than 500 pages.

    I am getting the "value out of range" exception when i try to set the pagemargin using the following code.

    doc.PageSetup.LeftMargin = app.InchesToPoints(0.5)
    doc.PageSetup.RightMargin = app.InchesToPoints(0.5)

    When i try for blank document the exception is not thrown.

    Attaching the link of the document where i can reproduce the issues

    https://1drv.ms/w/s!AkmlUDcbhtXiibFecZX5V6csfFZPFw

    But when i set margins using word UI directly it comes perfectly.Using this API i am not able to set.

    I wanted to know when this exception will occur?Why this occuring?How can we fix this exception?

    Regards,

    Prem Latha P

    Sunday, March 19, 2017 7:32 AM

All replies

  • Hi,

       I am trying to set the LeftMargin using vb.net for a document which is having more than 500 pages.

    I am getting the "value out of range" exception when i try to set the pagemargin using the following code.

    doc.PageSetup.LeftMargin = app.InchesToPoints(0.5)
    doc.PageSetup.RightMargin = app.InchesToPoints(0.5)

    When i try for blank document the exception is not thrown.

    I wanted to know when this exception will occur?Why this occuring?How can we fix this exception?

    Regards,

    Prem Latha P

    Monday, March 13, 2017 7:08 PM
  • Hi Prem-,

    This forum discusses and asks questions about .NET Framework Base Classes, Since your issue is more related to word for developer, we'll move it to word for developer for suitable support.

    Thanks for your understanding and cooperation.

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 14, 2017 1:53 AM
  • Hello Prem,

    You may try to test the code with other documents to check if the issue is related to specific file. And you could try to set the margin manually for this document to see if it would work. Besides, I suggest you share the document here to reproduce your issue. You could upload the file into OneDrive and share the link here. Please visit Share OneDrive files and folders

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 14, 2017 6:54 AM
    Moderator
  • Hi

    Attaching the link of the document where i can reproduce the issues

    https://1drv.ms/w/s!AkmlUDcbhtXiibFecZX5V6csfFZPFw

    Regards,

    Prem latha P

    Friday, March 17, 2017 12:26 PM
  • Any update on this?
    Sunday, March 19, 2017 7:30 AM
  • Hi Prem,

    I try to test that document on my side and I find that I can reproduce the issue on this particular document with the code below.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim objWord As Word.Application
    
            Dim worddoc As Word.Document
    
    
            If objWord Is Nothing Then
    
                objWord = CreateObject("Word.Application")
    
            Else
    
                objWord = GetObject(, "Word.Application")
    
            End If
    
    
    
            worddoc = objWord.Documents.Open("C:\Users\v-padee\Desktop\demodoc.docx")
            objWord.Visible = True
    
    
            worddoc.PageSetup.LeftMargin = objWord.InchesToPoints(1)
            worddoc.PageSetup.RightMargin = 0.5
            worddoc.Save()
    
    
    
    
            worddoc.Close()
    
            worddoc = Nothing
    
            objWord.Application.Quit()
    
            objWord = Nothing
    
    
    
        End Sub

    I also try to make a test with VBA code on the same document and I got the same result as error.

    I try to make a same test with another document that is created by me , I use the same above mentioned code to test.

    I find that on my document code is working correctly , it is not giving any error.

    so it looks like there is something wrong with your document.

    it is possible that something got corrupted in that.

    I want to ask you that, how you create this document?

    is it created by the user interface or by a code?

    if you created it using code then you need to check the code and try to correct the issue.

    I suggest you to copy the content of that document to new document and try to set the margin of new document.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 20, 2017 7:33 AM
    Moderator