Results 1 to 16 of 16

Thread: DocumentBeforePrint Problem.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2003
    Location
    Out there somewhere!
    Posts
    386

    DocumentBeforePrint Problem.

    Hello I am opening a word document and capturing the use of the print button with the following Code.

    I have the class -

    VB Code:
    1. Option Explicit
    2.  
    3. Public WithEvents objWord As Word.Application
    4.  
    5. Private Sub objWord_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
    6.  
    7.     frmApplication.txtLetterPrinted.Text = "Yes"
    8.    
    9. End Sub

    And I use this in a form -

    VB Code:
    1. Set objPrintDoc.objWord = CreateObject("Word.Application")
    2.    
    3.     With objPrintDoc.objWord
    4.         .Documents.Add Template:=strDocName
    5.         .ActiveDocument.MailMerge.OpenDataSource App.Path & "\addlist.txt", ReadOnly:=True
    6.         .ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
    7.         .ActiveDocument.MailMerge.ViewMailMergeFieldCodes = False
    8.        
    9.         .Visible = True
    10.         .Application.WindowState = wdWindowStateMaximize
    11.  
    12.     End With

    When i Was opening a .doc file, this worked fine, but I am now opening a .dot file and it has stopped working. Does anybody know why and can I fix it?


    Kev.

    "Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. " - Abert Einstein
    Last edited by Kev; Jun 3rd, 2005 at 08:31 AM.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2003
    Location
    Out there somewhere!
    Posts
    386

    Re: DocumentBeforePrint Problem.

    No ideas anyone?


    Kev.

    "I don't want to achieve immortality through my work... I want to achieve it through not dying." - Woody Allen

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2003
    Location
    Out there somewhere!
    Posts
    386

    Re: DocumentBeforePrint Problem.

    Help...please!



    Kev.

    "The advantage of a bad memory is that one enjoys several times the same good things for the first time. " - Friedrich Nietzsche

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: DocumentBeforePrint Problem.

    I don't know the actual answer - why it works with doc and not with dot files.
    I'v experienced this problem myself with MailMerge. The workaround I did was to save my file as doc file (instead of dot), open it as readonly whenever required and close it without saving.

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: DocumentBeforePrint Problem.

    Private Sub objWord_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
    may be if it is:- byval doc as template

    i am only guessing here, but seems like that could have something to do with it

    pete

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2003
    Location
    Out there somewhere!
    Posts
    386

    Re: DocumentBeforePrint Problem.

    Unfortunately not.

    I tried and I get a compile error - Procedure Declaration dose not match description of envent procedure having the same name.


    Kev.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2003
    Location
    Out there somewhere!
    Posts
    386

    Re: DocumentBeforePrint Problem.

    Unfortunately, I can't do what PraDeep1210 suggests as the mail merge document has to be editable when opened.


    Kev.

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: DocumentBeforePrint Problem.

    why are you useing the template anyway?
    can't you just open a new document based on the template?

    pete

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2003
    Location
    Out there somewhere!
    Posts
    386

    Re: DocumentBeforePrint Problem.

    That is what my code is doing. It opens up a new document based on the template. I'm not physically opening up the actual template.

    Kev.

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: DocumentBeforePrint Problem.

    sorry my misunderstanding

    pete

  11. #11
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: DocumentBeforePrint Problem.

    Quote Originally Posted by Kev
    Unfortunately, I can't do what PraDeep1210 suggests as the mail merge document has to be editable when opened.


    Kev.
    Sorry Kev, You got me wrong.
    By Read Only, I just meant that I won't be able to do a "Save". You can do everything else including a "Save As...". I just meant that wen I will close, I won't save it.
    THere is practically no difference in opening a document as such and opening it based on a template.
    I'll suggest you give it a try.

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2003
    Location
    Out there somewhere!
    Posts
    386

    Re: DocumentBeforePrint Problem.

    Sorry for the misunderstandong PraDeep1210.

    I have done what you said, still doesn't work!?

    I now have a the following class

    Code:
    Option Explicit
    
    Public WithEvents objWord As Word.Application
    
    Private Sub objWord_DocumentBeforePrint(ByVal Doc As Word.Document, Cancel As Boolean)
    
        frmApplication.txtLetterPrinted.Text = "YES"
        
    End Sub
    
    Private Sub objWord_DocumentOpen(ByVal Doc As Word.Document)
    
        With Doc
            .MailMerge.OpenDataSource App.Path & "\" & g_strUserName & "addlist.txt", ReadOnly:=True
            .MailMerge.SuppressBlankLines = True
            .MailMerge.MainDocumentType = wdFormLetters
            .MailMerge.ViewMailMergeFieldCodes = False
        End With
        
    End Sub
    I and I run i from the follwing code -
    Code:
        Set objPrintDoc.objWord = CreateObject("Word.Application")
        
        With objPrintDoc.objWord
            .Documents.Open strDocName, , True
            
            .Visible = True
            .Application.WindowState = wdWindowStateMaximize
    
        End With
    It now opens up a doc in read only. Now when the document is opening it goes into the DocumentOpen event no problem, but it doesn't go into the DocumentBeforePrint event when printing.

    Can anyone tell me why!? I'm banging my head off a wall.


    Kev.

    "It's a small world, but I wouldn't want to paint it"- Steven Wright

  13. #13
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: DocumentBeforePrint Problem.

    This might help you, from Word itself.

    Declare the Object Variable
    Before you can write procedures for the events of the Application object, you must create a new class module and declare an object of type Application with events. For example, assume that a new class module is created and called EventClassModule. The new class module contains the following code.

    Public WithEvents App As Word.Application
    Write the Event Procedures
    After the new object has been declared with events, it appears in the Object drop-down list box in the class module, and you can write event procedures for the new object. (When you select the new object in the Object box, the valid events for that object are listed in the Procedure drop-down list box.) Select an event from the Procedure drop-down list box; an empty procedure is added to the class module.

    Private Sub App_DocumentChange()

    End Sub
    Initialize the Declared Object
    Before the procedure will run, you must connect the declared object in the class module (App in this example) with the Application object. You can do this with the following code from any module.

    Dim X As New EventClassModule
    Sub Register_Event_Handler()
    Set X.App = Word.Application
    End Sub
    Run the Register_Event_Handler procedure. After the procedure is run, the App object in the class module points to the Microsoft Word Application object, and the event procedures in the class module will run when the events occur.

    Private Sub object_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)

    object An object of type Application declared with events in a class module. For more information about using events with the Application object, see Using Events with the Application Object.

    Doc The document that's being printed.

    Cancel False when the event occurs. If the event procedure sets this argument to True, the document isn't printed when the procedure is finished.
    Example
    This example prompts the user for a yes or no response before printing any document. This code must be placed in a class module, and an instance of the class must be correctly initialized in order to see this example work; see Using Events with the Application Object for directions on how to accomplish this.
    VB Code:
    1. Public WithEvents appWord as Word.Application
    2.  
    3. Private Sub appWord_DocumentBeforePrint _
    4.         (ByVal Doc As Document, _
    5.         Cancel As Boolean)
    6.  
    7.     Dim intResponse As Integer
    8.  
    9.     intResponse = MsgBox("Have you checked the " _
    10.         & "printer for letterhead?", _
    11.         vbYesNo)
    12.  
    13.     If intResponse = vbNo Then Cancel = True
    14. End Sub

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2003
    Location
    Out there somewhere!
    Posts
    386

    Re: DocumentBeforePrint Problem.

    Thanks for that, but it's basically what I've got in my coding.


    Kev.

  15. #15
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: DocumentBeforePrint Problem.

    I didn't see everything. Maybe I missed it, but do you have this?

    VB Code:
    1. Dim X As New EventClassModule
    2. Sub Register_Event_Handler()
    3.   Set X.App = Word.Application
    4. End Sub

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2003
    Location
    Out there somewhere!
    Posts
    386

    Re: DocumentBeforePrint Problem.

    I have, code wise -

    Dim objPrintDoc as new clsPrintDoc (- which is the name of the class.)


    Set objPrintDoc.objWord = createobject("Word.Application")

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