none
Script for prevent create a new page RRS feed

  • Question

  • Hi everyone, 

    I need a script that prevent, when the end of the first is reach, to create a new blank page, in my case I need to have just the first page.

    It is possible to do with vbs code?

    Thursday, February 20, 2020 1:19 PM

All replies

  • As far as I know, you can only really prevent this as follows (and I'm not even completely sure about that):

     1. lay out your document as a single page that contains a textbox that occupies the whole page area between the margins. 

     2. type some text into the text box and select it

     3. Use the Developer tab, Restrict Editing option to check "Allow only this type of editing in the document", select "No changes (Readonly) from the dropdown

     4. Under exceptions, check "Everyone", then click the Start Enforcing Protection button. Provide a suitable password (and make sure you remember it).

    Then you should find that the options to insert breaks of any kind are no long available. However, doing everything inside a textbox creates an unfamiliar environment for the user where there are probably plenty of other things they won't be able to do either. So my guess is that you will choose not to go down that route. But if you do, all you really need to do is save the .docx that you created and create any new documents by making a copy of that.


    Peter Jamieson

    Thursday, February 20, 2020 4:26 PM
  • Unfortunatly in my case I have form fields so I can't use the textbox
    Friday, February 21, 2020 8:11 AM
  • what kind of form fields? Content controls, legacy form fields, or what? If you “protect the document for forms”, is the problem then that you cannot stop people from entering too much text in a field (and then it spills over onto the next page)? 


    Peter Jamieson

    Friday, February 21, 2020 8:28 AM
  • I use form fields to populate some variables, but the rest of the page is freely editable, but i'd like to remain only in a single page
    Friday, February 21, 2020 10:12 AM
  • The only way I can see to do that without some VBA is 

     a. use content controls instead of legacy form fields

     b. don't use "protect for forms"

     c. use the approach I suggested before, but allow editing in each of the content controls as well as the text box.

    NB, one thing users can always do with a text box is add more text at the bottom, at which point they get "invisible text"


    Peter Jamieson

    Friday, February 21, 2020 12:13 PM
  • One could use a table with fixed dimensions and 'around' wrapping as an alternative - but not with formfields in the table, and insert a Section break immediately after the last formfield/static text, so that the table's Section is can be left unprotected.

    Regardless, whatever approach one takes, determined users can force such a document to span more than one page. Perhaps the OP should consider using a fillable PDF - which can be given the required restrictions.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, February 22, 2020 8:47 AM
  • If there is a solution that involve some VBA code is also accettable because i can't change so much the basic document
    Tuesday, February 25, 2020 5:07 PM
  • Fillable PDF is not an option becase users can always modify other part of text that are not module fields
    Tuesday, February 25, 2020 5:08 PM
  • Fillable PDF is not an option becase users can always modify other part of text that are not module fields
    That is simply not true when the appropriate restrictions are applied.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, February 26, 2020 2:15 AM
  • If there is a solution that involve some VBA code is also accettable because i can't change so much the basic document
    VBA cannot guarantee what you require. Even if one assumes it's possible with VBA, all a user need do to circumvent that is to disable macros when opening the document. 

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, February 26, 2020 2:18 AM
  • Personally, I would avoid using VBA because it introduces complication and, as Paul says, users can in any case circumvent anything you do if they really want to. IMO it is better if possible to rely on some combination of user training/information, e.g. prepopulate the document with a message that tells them not to let the document spill over onto two pages. In addition, if you have a multistep process where document creation is only the first step and your script does all further processing, you could consider having your script verify, after the  that the document only has one page before doing that processing, and have some kind of process for dealing with rejected documents.

    That said, if you really want to try a VBA approach, I suggest you start at https://wordmvp.com/FAQs/MacrosVBA/AppClassEvents.htm

    A simple example of some event code for a class module follows, but you may also need to do things such as ensure that the code only fires for the documents you want (e.g. by checking what their doc.AttachedTemplate is).I haven't checked how these events interact with the Forms Protection you need.

    Public WithEvents app As Application
    
    Const msg As String = "The document now contains more than one page. Please delete some content so that it only contains a single page."
    
    Private Sub app_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
    If Doc.Content.Information(wdNumberOfPagesInDocument) > 1 Then
      MsgBox msg
      Cancel = True
    End If
    End Sub
    
    Private Sub app_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
    If Doc.Content.Information(wdNumberOfPagesInDocument) > 1 Then
      MsgBox msg
      Cancel = True
    End If
    End Sub
    
    Private Sub app_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
    If Doc.Content.Information(wdNumberOfPagesInDocument) > 1 Then
      MsgBox msg
      Cancel = True
    End If
    End Sub
    
    Private Sub app_WindowSelectionChange(ByVal Sel As Selection)
    If Sel.Document.Content.Information(wdNumberOfPagesInDocument) > 1 Then
      MsgBox msg
    End If
    End Sub






    Peter Jamieson

    Wednesday, February 26, 2020 9:57 AM