Results 1 to 6 of 6
  1. #1
    New Lounger
    Join Date
    Aug 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Validate the height of the text when you save (1.0)

    Hi Everyone.
    My name is Javier and Im new programming macros in word, and I need to validate the height of the text in the first page when the user save the document.
    For example, I need to validate if the numer of lines that the user enter are height < 5.5in, if the height is > 5.5 then I need to send a message to the user...
    Thanks for your help. I really need a direction to do this...
    :-D
    Javier

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Validate the height of the text when you save (1.0)

    Welcome to Woody's Lounge!

    This is a bit clunky, you could use something like this:

    Sub TestHeight()
    If Selection.Information(wdNumberOfPagesInDocument) = 1 Then
    Selection.EndKey Unit:=wdStory
    Else
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="2"
    Selection.MoveUp Unit:=wdLine, Count:=1
    End If
    If PointsToInches(Selection.Information(wdVerticalPos itionRelativeToTextBoundary)) > 5.5 Then
    MsgBox "The text on the first page takes up more then 5.5 inches.", vbExclamation
    End If
    End Sub

    Alternatively, you could specify Different First Page in the Layout tab of File | Page Setup..., and make the page footer of the first page so tall that there is room for 5.5 inches of text on page 1.

  3. #3
    New Lounger
    Join Date
    Aug 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Validate the height of the text when you save (1.0)

    Thanks for your help Hans.

    Right now, Im looking how to call the function when the user save the word document.

    :-D

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Validate the height of the text when you save (1.0)

    You'd have to use the DocumentBeforeSave event of the Application object, but that's far from easy and rather fragile. I'd opt for a non-VBA workaround if possible.

  5. #5
    New Lounger
    Join Date
    Aug 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Validate the height of the text when you save (1.0)

    Hans, I already have an example working:

    For your particular case you can use the DocumentBeforeClose application event.

    To use application events do the following:
    a. In your project add a new Class module.
    b. Rename the module to (for instance): oAppClass
    b. In this module paste the following code:

    Option Explicit
    Public WithEvents oApp As Word.Application
    Private Sub oApp_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)

    Dim response As Integer
    response = vbNo
    If Selection.Information(wdNumberOfPagesInDocument) = 1 Then
    Selection.EndKey Unit:=wdStory
    If PointsToInches(Selection.Information(wdVerticalPos itionRelativeToTextBoundary)) > 5.5 Then
    response = MsgBox("The Ad Document must be of 1 page and less of 5.5 of Height." & vbCrLf & "Do you want to modify the document before close?", vbYesNo + vbQuestion)
    End If
    Else
    response = MsgBox("The Ad Document must be of 1 page and less of 5.5 of Height." & vbCrLf & "Do you want to modify the document before close?", vbYesNo + vbQuestion)
    End If
    If response <> vbNo Then
    Cancel = True
    End If
    End Sub

    c. This class needs to be instantiated so add a new (normal) module.
    d. in this module paste the following code:

    Option Explicit
    Dim oAppClass As New oAppClass
    Public Sub AutoOpen()
    Set oAppClass.oApp = Word.Application
    End Sub

    If you have another idea how to solve the problem?
    In fact the original idea is doing all these tasks in Visual Basic 6 application, Im going to try to do the same in the form.

    Thanks

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Validate the height of the text when you save (1.0)

    This type of code, using the Document_BeforeClose or Document_BeforeSave event of the Application object, is the "standard" way of doing it if you want to use VBA.

    You should be aware that the event will apply to ALL documents when you close them.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •