Results 1 to 4 of 4

Thread: Word Events

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    UK
    Posts
    70

    Word Events

    I am using the following code to intercept the print event in word all works fine until you have more than one document open with the macro in then you get the message twice. Does anyone have any bright ideas how to stop this happening.


    Thanks in advance (fingers crossed)


    VB Code:
    1. Private Sub App_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
    2.        
    3.     usrType = "HKEY_CURRENT_USER"
    4.     location = regget(usrType & "\SOFTWARE\M\User Location")
    5.    
    6.     If Len(location) = 0 Then
    7.         location = regget("HKEY_LOCAL_MACHINE\SOFTWARE\M\Station Location")
    8.     End If
    9.  
    10.     If Left(LCase(location), 6) = "london" Then
    11.         msg = "Only print the draft copy?" & vbCrLf & vbCrLf
    12.         msg = msg & "Yes" & vbTab & "- Print only the draft copy." & vbCrLf
    13.         msg = msg & "No" & vbTab & "- Print both copies." & vbCrLf
    14.         msg = msg & "Cancel" & vbTab & "- Print using your word setting."
    15.    
    16.         clicked = MsgBox(msg, vbYesNoCancel)
    17.            
    18.         If clicked = vbYes Or clicked = vbNo Then
    19.             'Cancel the existing print job
    20.             Cancel = True
    21.            
    22.             If clicked = vbNo Then
    23.                 ' Config the tray as needed for client copy
    24.                 Doc.PageSetup.FirstPageTray = wdPrinterLowerBin
    25.                 Doc.PageSetup.OtherPagesTray = wdPrinterUpperBin
    26.        
    27.                 ' Print the client copy
    28.                 Doc.PrintOut Copies:=1
    29.             End If
    30.    
    31.             ' Config the trays as needed for the file copy
    32.             Doc.PageSetup.FirstPageTray = wdPrinterLargeCapacityBin
    33.             Doc.PageSetup.OtherPagesTray = wdPrinterLargeCapacityBin
    34.            
    35.             'Print the file copy
    36.             Doc.PrintOut Copies:=1
    37.         End If
    38.     End If
    39.  
    40. End Sub

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    How are you calling this App_DocumentBeforePrint procedure?

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    UK
    Posts
    70
    I never call it its runs like the Document_Open events in this case before the user prints this event is called.


    This is the code to register the event
    VB Code:
    1. Dim X As New PrinterModule
    2.  
    3. Sub Register_Event_Handler()
    4.  
    5.     Set X.App = Word.Application
    6.    
    7. End Sub

  4. #4
    Hyperactive Member -=XQ=-'s Avatar
    Join Date
    Mar 2002
    Location
    Liverpool, England, UK
    Posts
    278
    You could create a global variable thats an integer when the code gets executed detect how many documents are open and then run your code and set the global variable to the remander of documents left.... then each time you get further calls just keep reducing the global variable and skip the code with an if... else... block. Make sure you reduce the global variable to 0 and then your code will work only the onetime needed.

    Hope that helps

    Example:
    Code:
    public intCount as integer
    
    public sub....
    
       if intCount = 0 then
          intCount = Documents.Count
          .... execute code
       else
          intCount = intCount - 1
       end if
    end sub
    See ya later,

    -=XQ=-

    "Reality is merely an illusion, albeit a very persistent one. "
    - Albert Einstein (1879-1955)
    This is the coolest site ever!!!

Posting Permissions

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



Click Here to Expand Forum to Full Width