Results 1 to 11 of 11
  1. #1
    4 Star Lounger
    Join Date
    Feb 2002
    Location
    Auckland, New Zealand
    Posts
    543
    Thanks
    56
    Thanked 0 Times in 0 Posts
    MSO 2003 xpp sp3

    Hi

    I am wanting to have a macro run in Word to create a header and insert specific text when File/Print is selected.
    cheers

    Phil Carter

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Quote Originally Posted by bonriki View Post
    I am wanting to have a macro run in Word to create a header and insert specific text when File/Print is selected.
    You can intercept printing by naming a macro FilePrint. There are some fine points to it, but generally speaking, you can display the dialog, capture the user's instructions, update the document, then execute the user's instructions. (You may also need to intercept the FilePrintDefault toolbar button -- printing that bypasses the Print dialog).

    Regarding the changes you want to make, it's always a little tricky not ruining the existing contents of a header or footer when editing them in code. Or did you mean you want to insert some content at the beginning of the "body" of the document? Can you describe your requirements in a little more detail? You could, for example, upload "before and after" documents showing what the code should do.

  3. #3
    4 Star Lounger
    Join Date
    Feb 2002
    Location
    Auckland, New Zealand
    Posts
    543
    Thanks
    56
    Thanked 0 Times in 0 Posts
    Jefferson hi

    I have managed to find some code to do what I want. WE are not worried about other content in the header as there should not be any.

    FYI the code I have used

    In the project explorer "ThisDocument" I have put the code below into the code window.

    Code:
    Private Sub Document_Open()
    
    	Call Register_Event_Handler
    
    End Sub
    Inserted a new blank module and inserted the code below
    Code:
    Dim X As New Class1
    Public Sub Register_Event_Handler()
        Set X.App = Word.Application
    End Sub
    
    Inserted a Class Module and inserted the code below.
    
    Public WithEvents App As Word.Application
    
    Private Sub App_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
    
        If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
            ActiveWindow.Panes(2).Close
        End If
        If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
            ActivePane.View.Type = wdOutlineView Then
            ActiveWindow.ActivePane.View.Type = wdPrintView
        End If
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
        Selection.Font.Size = 8
        Selection.TypeText Text:= _
            "Document uncontrolled when printed: Printed on: "
        Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
            "PRINTDATE  \@ ""dddd, d MMMM yyyy"" ", PreserveFormatting:=True
        Selection.TypeText Text:=", "
        Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
            "PRINTDATE  \@ ""h:mm am/pm"" ", PreserveFormatting:=True
        ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
        Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
            wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
            ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _
            False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
            PrintZoomPaperHeight:=0
    
    End Sub
    For some reason it prints 2 copies of the test document!

    Thanks for replying though
    cheers

    Phil Carter

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    This is definitely not the simplest approach, but it might be best in your case.

    Quote Originally Posted by bonriki View Post
    For some reason it prints 2 copies of the test document!
    Your code contains this method: Application.PrintOut. However, you are not settings Cancel = True, so Word goes ahead and prints following the user's instructions in the Print dialog.

    I think the best solution is to comment out Application.PrintOut and just let Word print normally, especially if the user might be printing only a few pages of the document or even a selection.

    What happens on the second print? Does the header get replaced or does the content get doubled?

  5. #5
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    hi Bonriki,

    Why don't you simply add the necessary text & field coding to the document (and it's template, so that future documents get created with it)?
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  6. #6
    4 Star Lounger
    Join Date
    Feb 2002
    Location
    Auckland, New Zealand
    Posts
    543
    Thanks
    56
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jscher2000 View Post
    This is definitely not the simplest approach, but it might be best in your case.


    Your code contains this method: Application.PrintOut. However, you are not settings Cancel = True, so Word goes ahead and prints following the user's instructions in the Print dialog.

    I think the best solution is to comment out Application.PrintOut and just let Word print normally, especially if the user might be printing only a few pages of the document or even a selection.

    What happens on the second print? Does the header get replaced or does the content get doubled?
    Jefferson you're right I commentdout that line of code. It just printed 2 copies
    cheers

    Phil Carter

  7. #7
    4 Star Lounger
    Join Date
    Feb 2002
    Location
    Auckland, New Zealand
    Posts
    543
    Thanks
    56
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by macropod View Post
    hi Bonriki,

    Why don't you simply add the necessary text & field coding to the document (and it's template, so that future documents get created with it)?
    Paul hi

    Mostly the document will be viewed on the internet, go here, Promapp, and hover over the Definitions folder and click the link. This shows the document but we need to show that it will be uncontrolled if printed.
    cheers

    Phil Carter

  8. #8
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Quote Originally Posted by bonriki View Post
    Paul hi

    Mostly the document will be viewed on the internet, go here, Promapp, and hover over the Definitions folder and click the link. This shows the document but we need to show that it will be uncontrolled if printed.
    Hi Phil,

    That's OK as far as it goes, but why not incorporate the info in the standard footer, where the alert can be seen before the document is printed? Alternatively, if you only want the message to appear on printed versions, use either of the following field codes in the header:
    {IF{PRINTDATE \@ yyyyMMddHHmm}= {DATE \@ yyyyMMddHHmm} "Document uncontrolled when printed. Printed on: {PRINTDATE \@ "DDDD, D MMMM YYYY, hh:mm am/pm"}"}
    or:
    {IF{PRINTDATE \@ yyyy}> 0 "Document uncontrolled when printed. Printed on: {PRINTDATE \@ "DDDD, D MMMM YYYY, hh:mm am/pm"}"}
    The first version generates a field that displays on the electronic file for up to a minute after printing, after which a field update will hide it again; the second version leaves a permanent message on the electronic file. In either case, it seems to me that it would be preferable to move the 'Document uncontrolled when printed.' outside the field code, so that it always appears in the header.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  9. #9
    4 Star Lounger
    Join Date
    Feb 2002
    Location
    Auckland, New Zealand
    Posts
    543
    Thanks
    56
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by macropod View Post
    Hi Phil,

    That's OK as far as it goes, but why not incorporate the info in the standard footer, where the alert can be seen before the document is printed? Alternatively, if you only want the message to appear on printed versions, use either of the following field codes in the header:
    {IF{PRINTDATE \@ yyyyMMddHHmm}= {DATE \@ yyyyMMddHHmm} "Document uncontrolled when printed. Printed on: {PRINTDATE \@ "DDDD, D MMMM YYYY, hh:mm am/pm"}"}
    or:
    {IF{PRINTDATE \@ yyyy}> 0 "Document uncontrolled when printed. Printed on: {PRINTDATE \@ "DDDD, D MMMM YYYY, hh:mm am/pm"}"}
    The first version generates a field that displays on the electronic file for up to a minute after printing, after which a field update will hide it again; the second version leaves a permanent message on the electronic file. In either case, it seems to me that it would be preferable to move the 'Document uncontrolled when printed.' outside the field code, so that it always appears in the header.
    Hi again Paul

    I think it is OK as it is. The nature of the software is that it will not save altered documents back to the library where they were extracted from.

    If the documents require amending they will need to be uploaded again
    cheers

    Phil Carter

  10. #10
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Quote Originally Posted by bonriki View Post
    Hi again Paul

    I think it is OK as it is. The nature of the software is that it will not save altered documents back to the library where they were extracted from.

    If the documents require amending they will need to be uploaded again
    Hi Phil,

    But doesn't the same risk exist if the user simply downloads the document from the library (eg via File|Save As)? If they then work from their saved copy, regardless of whether the document gets printed, surely the same risk exists? Also, for the macro solution to work, you need to persuade the user to enable macros for these documents - with field coding, there's nothing that relies on the user's response.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  11. #11
    4 Star Lounger
    Join Date
    Feb 2002
    Location
    Auckland, New Zealand
    Posts
    543
    Thanks
    56
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by macropod View Post
    Hi Phil,

    But doesn't the same risk exist if the user simply downloads the document from the library (eg via File|Save As)? If they then work from their saved copy, regardless of whether the document gets printed, surely the same risk exists? Also, for the macro solution to work, you need to persuade the user to enable macros for these documents - with field coding, there's nothing that relies on the user's response.
    Paul hi

    Your right there is risk that users may use File|Save As but we seem to have convinced our users not to do that. All the users computers have a group template, similar to normal.dot, that is sets the parameters for Word to run under including setting the secuurity level to Medium. We have many macro driven forms letters etc so they are all conditioned to hit Enable Macros.
    cheers

    Phil Carter

Posting Permissions

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