|
-
Jun 3rd, 2005, 07:54 AM
#1
Thread Starter
Hyperactive Member
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:
Option Explicit
Public WithEvents objWord As Word.Application
Private Sub objWord_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
frmApplication.txtLetterPrinted.Text = "Yes"
End Sub
And I use this in a form -
VB Code:
Set objPrintDoc.objWord = CreateObject("Word.Application")
With objPrintDoc.objWord
.Documents.Add Template:=strDocName
.ActiveDocument.MailMerge.OpenDataSource App.Path & "\addlist.txt", ReadOnly:=True
.ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
.ActiveDocument.MailMerge.ViewMailMergeFieldCodes = False
.Visible = True
.Application.WindowState = wdWindowStateMaximize
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.
-
Jun 6th, 2005, 01:31 AM
#2
Thread Starter
Hyperactive Member
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
-
Jun 9th, 2005, 06:52 AM
#3
Thread Starter
Hyperactive Member
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
-
Jun 9th, 2005, 07:05 AM
#4
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
-
Jun 9th, 2005, 07:33 AM
#5
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
-
Jun 9th, 2005, 07:48 AM
#6
Thread Starter
Hyperactive Member
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.
-
Jun 9th, 2005, 09:39 AM
#7
Thread Starter
Hyperactive Member
Re: DocumentBeforePrint Problem.
Unfortunately, I can't do what PraDeep1210 suggests as the mail merge document has to be editable when opened.
Kev.
-
Jun 9th, 2005, 09:52 AM
#8
Re: DocumentBeforePrint Problem.
why are you useing the template anyway?
can't you just open a new document based on the template?
pete
-
Jun 9th, 2005, 09:57 AM
#9
Thread Starter
Hyperactive Member
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.
-
Jun 9th, 2005, 10:03 AM
#10
Re: DocumentBeforePrint Problem.
sorry my misunderstanding
pete
-
Jun 9th, 2005, 11:34 AM
#11
Re: DocumentBeforePrint Problem.
 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
-
Jun 10th, 2005, 03:24 AM
#12
Thread Starter
Hyperactive Member
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
-
Jun 10th, 2005, 03:48 AM
#13
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:
Public WithEvents appWord as Word.Application
Private Sub appWord_DocumentBeforePrint _
(ByVal Doc As Document, _
Cancel As Boolean)
Dim intResponse As Integer
intResponse = MsgBox("Have you checked the " _
& "printer for letterhead?", _
vbYesNo)
If intResponse = vbNo Then Cancel = True
End Sub
-
Jun 10th, 2005, 04:04 AM
#14
Thread Starter
Hyperactive Member
Re: DocumentBeforePrint Problem.
Thanks for that, but it's basically what I've got in my coding.
Kev.
-
Jun 10th, 2005, 04:14 AM
#15
Re: DocumentBeforePrint Problem.
I didn't see everything. Maybe I missed it, but do you have this?
VB Code:
Dim X As New EventClassModule
Sub Register_Event_Handler()
Set X.App = Word.Application
End Sub
-
Jun 10th, 2005, 04:17 AM
#16
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|