Microsoft Office Forums

Microsoft Office Forums (https://www.msofficeforums.com/)
-   Word VBA (https://www.msofficeforums.com/word-vba/)
-   -   Macro for Print (https://www.msofficeforums.com/word-vba/6615-macro-print.html)

sivakl2001 03-23-2011 03:05 AM

Macro for Print
 
i want to run macro when document send to printer
i tried FilePrint()
but my macro executed when the user hit the Print and doesn't show the Print Window to select options
then i use FilePrintDefault()
this macro executed when the user print through the Default printer if he did't print throught default printer the macro doesn't executed
so can i know how to execute the macro when the user click "Ok" button to send to Printer

macropod 03-23-2011 07:27 PM

Hi sivakl2001,

To intercept the File|Print command and display the print dialogue, use something along the lines of:
Code:

Sub FilePrint()
With Application.Dialogs(wdDialogFilePrint)
  If .Show = -1 Then
    MsgBox "Your document has been sent to the printer"
  End If
End With
End Sub


sivakl2001 03-23-2011 10:03 PM

hi macropod
i need to put my company name in the print out of the all word document page
tats why i need this macro
ur coding working fine but it print twice
this is my code

Code:


Sub FilePrint()
'
' FilePrint Macro
' Prints the active document
'
  ' Dialogs(wdDialogFilePrint).Show
With Application.Dialogs(wdDialogFilePrint)
  If .Show = -1 Then
    Dim oDoc As Document
Dim strPath As String
Dim oSection As Section
Dim oHeader As HeaderFooter
Dim oRng As Range
Dim intPos As Integer
Dim strCompany As String
Set oDoc = ActiveDocument
oDoc.Save
strCompany = "Test Company"
Set oSection = oDo c.Sections(1)
For Each oHeader In oSection.Headers
    If oHeader.Exists Then
        Set oRng = oHeader.Range
        With oRng
            If Len(oRng) > 1 Then
                .InsertParagraphBefore
                .Start = oHeader.Range.Start
                .End = oHeader.Range.Paragraphs(1).Range.End
            End If
            .Font.Name = "Arial"
            .Font.Bold = False
            .Font.Italic = False
            .Font.Size = "12"
            .ParagraphFormat.Alignment = wdAlignParagraphRight
            .Text = strCompany
        End With
        Exit For
    End If
Next oHeader
strPath = oDoc.FullName
Options.PrintProperties = False
oDoc.PrintOut Background:=False
oDoc.Close wdDoNotSaveChanges
Documents.Open strPath
  End If
End With
End Sub


macropod 03-23-2011 11:45 PM

Hi sivakl2001,

Try it this way:
Code:

Sub FilePrint()
'
' FilePrint Macro
' Prints the active document
'
Dim oDoc As Document
Dim strPath As String
Dim oSection As Section
Dim oHeader As HeaderFooter
Dim oRng As Range
Dim intPos As Integer
Const strCompany As String = "Test Company"
Set oDoc = ActiveDocument
With Application.Dialogs(wdDialogFilePrint)
  oDoc.Save
  strPath = oDoc.FullName
  Set oSection = oDoc.Sections(1)
  For Each oHeader In oSection.Headers
    If oHeader.Exists Then
      Set oRng = oHeader.Range
      With oRng
        If Len(oRng) > 1 Then
          .InsertParagraphBefore
          .Start = oHeader.Range.Start
          .End = oHeader.Range.Paragraphs(1).Range.End
        End If
        With .Font
          .Name = "Arial"
          .Bold = False
          .Italic = False
          .Size = "12"
        End With
        .ParagraphFormat.Alignment = wdAlignParagraphRight
        .Text = strCompany
      End With
      Exit For
    End If
  Next oHeader
  Options.PrintProperties = False
  If .Show <> -1 Then Exit Sub
  oDoc.Close wdDoNotSaveChanges
End With
Documents.Open strPath
End Sub


sivakl2001 03-24-2011 08:08 PM

Hi Macropod can i create installer for this macro
can u guide me hw to create simple installer for Word Macro

macropod 03-24-2011 10:36 PM

Hi sivakl2001,

That's outside my field of expertise, sorry.

macropod 03-24-2011 11:25 PM

Hi sivakl2001,

I see you've been posting about the same question at:
http://social.msdn.microsoft.com/For...orddev/threads

For cross-posting etiquette, please see:
http://www.excelguru.ca/node/7

I don't appreciate finding that I've spent my time helping you to solve a problem only to find that you've had someone else spending their time on the same problem and you haven't bothered to tell either party about the help you've already been given.


All times are GMT -7. The time now is 12:27 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft