Vba word print from to

title keywords f1_keywords ms.prod api_name ms.assetid ms.date ms.localizationpriority

Application.PrintOut method (Word)

vbawd10.chm158335424

vbawd10.chm158335424

word

Word.Application.PrintOut

f795218e-cd49-f3ac-c03d-9a9424361392

06/08/2017

medium

Application.PrintOut method (Word)

Prints all or part of the specified document.

Syntax

expression.PrintOut (Background, Append, Range, OutputFileName, From, To, Item, Copies, Pages, PageType, PrintToFile, Collate, FileName, ActivePrinterMacGX, ManualDuplexPrint, PrintZoomColumn, PrintZoomRow, PrintZoomPaperWidth, PrintZoomPaperHeight)

expression Required. A variable that represents an Application object.

Parameters

Name Required/Optional Data type Description
Background Optional Variant Set to True to have the macro continue while Microsoft Word prints the document.
Append Optional Variant Set to True to append the specified document to the file name specified by the OutputFileName argument. False to overwrite the contents of OutputFileName.
Range Optional Variant The page range. Can be any WdPrintOutRange constant.
OutputFileName Optional Variant If PrintToFile is True, this argument specifies the path and file name of the output file.
From Optional Variant The starting page number when Range is set to wdPrintFromTo.
To Optional Variant The ending page number when Range is set to wdPrintFromTo.
Item Optional Variant The item to be printed. Can be any WdPrintOutItem constant.
Copies Optional Variant The number of copies to be printed.
Pages Optional Variant The page numbers and page ranges to be printed, separated by commas. For example, «2, 6-10» prints page 2 and pages 6 through 10.
PageType Optional Variant The type of pages to be printed. Can be any WdPrintOutPages constant.
PrintToFile Optional Variant True to send printer instructions to a file. Make sure to specify a file name with OutputFileName.
Collate Optional Variant When printing multiple copies of a document, True to print all pages of the document before printing the next copy.
FileName Optional Variant The path and file name of the document to be printed. If this argument is omitted, Word prints the active document. (Available only with the Application object.)
ActivePrinterMacGX Optional Variant This argument is available only in Microsoft Office Macintosh Edition. For additional information about this argument, consult the language reference Help included with Microsoft Office Macintosh Edition.
ManualDuplexPrint Optional Variant True to print a two-sided document on a printer without a duplex printing kit. If this argument is True, the PrintBackground and PrintReverse properties are ignored. Use the PrintOddPagesInAscendingOrder and PrintEvenPagesInAscendingOrder properties to control the output during manual duplex printing. This argument may not be available to you, depending on the language support (U.S. English, for example) that you have selected or installed.
PrintZoomColumn Optional Variant The number of pages you want Word to fit horizontally on one page. Can be 1, 2, 3, or 4. Use with the PrintZoomRow argument to print multiple pages on a single sheet.
PrintZoomRow Optional Variant The number of pages you want Word to fit vertically on one page. Can be 1, 2, or 4. Use with the PrintZoomColumn argument to print multiple pages on a single sheet.
PrintZoomPaperWidth Optional Variant The width to which you want Word to scale printed pages, in twips (20 twips = 1 point; 72 points = 1 inch).
PrintZoomPaperHeight Optional Variant The height to which you want Word to scale printed pages, in twips (20 twips = 1 point; 72 points = 1 inch).

Example

This example prints the current page of the active document.

ActiveDocument.PrintOut Range:=wdPrintCurrentPage

This example prints all the documents in the current folder. The Dir function is used to return all file names that have the file name extension «.doc».

adoc = Dir("*.DOC") 
Do While adoc <> "" 
 Application.PrintOut FileName:=adoc 
 adoc = Dir() 
Loop

This example prints the first three pages of the document in the active window.

ActiveDocument.ActiveWindow.PrintOut _ 
 Range:=wdPrintFromTo, From:="1", To:="3"

This example prints the comments in the active document.

If ActiveDocument.Comments.Count >= 1 Then 
 ActiveDocument.PrintOut Item:=wdPrintComments 
End If

This example prints the active document, fitting six pages on each sheet.

ActiveDocument.PrintOut PrintZoomColumn:=3, _ 
 PrintZoomRow:=2

This example prints the active document at 75% of actual size.

ActiveDocument.PrintOut _ 
 PrintZoomPaperWidth:=0.75 * (8.5 * 1440), _ 
 PrintZoomPaperHeight:=0.75 * (11 * 1440)

See also

Application Object

[!includeSupport and feedback]

  • Remove From My Forums
  • Question

  • Hi, I am using the follwing code to to print a Word doc to a pdf file, using VBA and a free pdf converter…

        Application.ActivePrinter = «CutePDF Writer»
        Application.PrintOut

    After running this code it prompts for a file and path name. Is there any way I can use VBA to automatically enter the file and path name?

    Many Thanks

Answers

  • Set the Background property of the PrintOut Method to False to prevent the next command from being executed until the printing is complete.

    — Hope this helps.

    Doug Robbins — Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    «ddunn10» wrote in message news:20b8e17f-7fec-4988-a4a8-2ae4e9829535@communitybridge.codeplex.com…

    Thanks for your reply, I managed to get it to work using a different pdf converter called PDF Creator (which allows you to specify a location in the options so it prints there automatically)

    The next bit I am stuck on is the way the code runs, I want to use the saved pdf file to attach it to an email. But the problem is that the printing of the pdf doesn’t start until the end of the sub.

    Sub SubName()  Application.PrintOut

      ‘Code for attaching the saved file to an email

    End Sub

    I’ve tried using sleep after Application.PrintOut to wait until the file is printed, but this doesn’t work, it just seems to wait until the end of the sub until it does the printing!

    Is there a way to wait until the pdf is printed until my second part of my code is run?

    Thanks for any advice


    Doug Robbins — Word MVP dkr[atsymbol]mvps[dot]org

    • Marked as answer by

      Tuesday, July 6, 2010 5:30 AM

I want to print a Word document, mydocument.docx, from a button in an Excel sheet. Both are in the same folder.

I don’t want users to see the Word document. They just click the button in Excel.

I can create a button in Excel and make it open an empty vb. This is as much as I know. If you can explain this in steps that would be so awesome.

Community's user avatar

asked Sep 14, 2009 at 5:34

nickjohn's user avatar

You can use the Word automation object model to gain programmatic access to Word.

In almost all cases, you’d be following these steps:

  1. Create the Word application object.
  2. Open a document.
  3. Do something with the document.
  4. Close the document.
  5. Quit the Word application.

Here is what the basic VBA code looks like:

' Step 1
Dim objWord
Set objWord = CreateObject("Word.Application")
' Hidden window!
objWord.Visible = False
' Save the original printer, otherwise you will reset the system default!
Dim previousPrinter
Set previousPrinter = objWord.ActivePrinter
objWord.ActivePrinter = "My Printer Name"

' Step 2
Dim objDoc
Set objDoc = objWord.Documents.Open("C:TestSomeDocument.docx")

' Step 3 -- in this case, print out the document without any prompts
objDoc.PrintOut
' Restore the original printer
objWord.ActivePrinter = previousPrinter

' Step 4
objDoc.Close

' Step 5
objWord.Quit

answered Sep 14, 2009 at 6:24

bobbymcr's user avatar

bobbymcrbobbymcr

23.6k3 gold badges55 silver badges66 bronze badges

4

Prints all or part of the specified document.

expression.PrintOut(Background, Append, Range, OutputFileName, From, To, Item, Copies, Pages, PageType, PrintToFile, Collate, FileName, ActivePrinterMacGX, ManualDuplexPrint, PrintZoomColumn, PrintZoomRow, PrintZoomPaperWidth, PrintZoomPaperHeight)

expression    Required. An expression that returns one of the above objects.

Background   Optional Variant. Set to True to have the macro continue while Microsoft Word prints the document.

Append   Optional Variant. Set to True to append the specified document to the file name specified by the OutputFileName argument. False to overwrite the contents of OutputFileName.

Range   Optional Variant. The page range. Can be any WdPrintOutRange
constant.

wdPrintAllDocument
wdPrintCurrentPage
wdPrintFromTo
wdPrintRangeOfPages
wdPrintSelection

OutputFileName   Optional Variant. If PrintToFile is True, this argument specifies the path and file name of the output file.

From   Optional Variant. The starting page number when Range is set to wdPrintFromTo.

To   Optional Variant. The ending page number when Range is set to wdPrintFromTo.

Item   Optional Variant. The item to be printed. Can be any WdPrintOutItem
constant.

wdPrintAutoTextEntries
wdPrintComments
wdPrintDocumentContent
wdPrintDocumentWithMarkup
wdPrintEnvelope
wdPrintKeyAssignments
wdPrintMarkup
wdPrintProperties
wdPrintStyles

Copies   Optional Variant. The number of copies to be printed.

Pages   Optional Variant. The page numbers and page ranges to be printed, separated by commas. For example, «2, 6-10» prints page 2 and pages 6 through 10.

PageType   Optional Variant. The type of pages to be printed. Can be any WdPrintOutPages
constant.

wdPrintAllPages
wdPrintEvenPagesOnly
wdPrintOddPagesOnly

PrintToFile   Optional Variant. True to send printer instructions to a file. Make sure to specify a file name with OutputFileName.

Collate   Optional Variant. When printing multiple copies of a document, True to print all pages of the document before printing the next copy.

FileName   Optional Variant. The path and file name of the document to be printed. If this argument is omitted, Word prints the active document. (Available only with the Application object.)

ActivePrinterMacGX   Optional Variant. This argument is available only in Microsoft Office Macintosh Edition. For additional information about this argument, consult the language reference Help included with Microsoft Office Macintosh Edition.

ManualDuplexPrint   Optional Variant. True to print a two-sided document on a printer without a duplex printing kit. If this argument is True, the PrintBackground
and PrintReverse
properties are ignored. Use the PrintOddPagesInAscendingOrder
and PrintEvenPagesInAscendingOrder
properties to control the output during manual duplex printing. This argument may not be available to you, depending on the language support (U.S. English, for example) that you’ve selected or installed.

PrintZoomColumn   Optional Variant. The number of pages you want Word to fit horizontally on one page. Can be 1, 2, 3, or 4. Use with the PrintZoomRow argument to print multiple pages on a single sheet.

PrintZoomRow   Optional Variant. The number of pages you want Word to fit vertically on one page. Can be 1, 2, or 4. Use with the PrintZoomColumn argument to print multiple pages on a single sheet.

PrintZoomPaperWidth   Optional Variant. The width to which you want Word to scale printed pages, in twips (20 twips = 1 point; 72 points = 1 inch).

PrintZoomPaperHeight   Optional Variant. The height to which you want Word to scale printed pages, in twips (20 twips = 1 point; 72 points = 1 inch).

ShowPrintOut method as it applies to the Envelope object.

Prints an envelope without adding the envelope to the active document.

expression.PrintOut(ExtractAddress, Address, AutoText, OmitReturnAddress, ReturnAddress, ReturnAutoText, PrintBarCode, PrintFIMA, Size, Height, Width, FeedSource, AddressFromLeft, AddressFromTop, ReturnAddressFromLeft, ReturnAddressFromTop, DefaultFaceUp, DefaultOrientation, PrintEPostage, Vertical, RecipientNamefromLeft, RecipientNamefromTop, RecipientPostalfromLeft, RecipientPostalfromTop, SenderNamefromLeft, SenderNamefromTop, SenderPostalfromLeft, SenderPostalfromTop)

expression    Required. An expression that returns an Envelope object.

ExtractAddress   Optional Variant. True to use the text marked by the «EnvelopeAddress» bookmark (a user-defined bookmark) as the recipient’s address.

Address   Optional Variant. A string that specifies the recipient’s address (ignored if ExtractAddress is True).

AutoText   Optional Variant. The name of the AutoText entry that includes a recipient’s address.

OmitReturnAddress   Optional Variant. True to omit the return address.

ReturnAddress   Optional Variant. A string that specifies the return address.

ReturnAutoText   Optional Variant. The name of the AutoText entry that includes a return address.

PrintBarCode   Optional Variant. True to add a POSTNET bar code. For U.S. mail only.

PrintFIMA   Optional Variant. True to add a Facing Identification Mark (FIM-A) for use in presorting courtesy reply mail. For U.S. mail only.

Size   Optional Variant. A string that specifies the envelope size. The string should match one of the sizes listed on the left side of the Envelope size box in the Envelope Options dialog box (for example, «Size 10»).

Height   Optional Variant. The height of the envelope (in points) when the Size argument is set to «Custom size.»

Width   Optional Variant. The width of the envelope (in points) when the Size argument is set to «Custom size.»

FeedSource   Optional Variant. True to use the FeedSource
property of the Envelope
object to specify which paper tray to use when printing the envelope.

AddressFromLeft   Optional Variant. The distance (in points) between the left edge of the envelope and the recipient’s address.

AddressFromTop   Optional Variant. The distance (in points) between the top edge of the envelope and the recipient’s address.

ReturnAddressFromLeft   Optional Variant. The distance (in points) between the left edge of the envelope and the return address.

ReturnAddressFromTop   Optional Variant. The distance (in points) between the top edge of the envelope and the return address.

DefaultFaceUp   Optional Variant. True to print the envelope face up; False to print it face down.

DefaultOrientation   Optional Variant. The orientation of the envelope. Can be any WdEnvelopeOrientation
constant.

wdLeftPortrait
wdCenterPortrait
wdRightPortrait
wdLeftLandscape
wdCenterLandscape
wdRightLandscape
wdLeftClockwise
wdCenterClockwise
wdRightClockwise

PrintEPostage   Optional Variant. True to print postage using an Internet e-postage vendor.

Vertical   Optional Variant. True prints text vertically on the envelope. Used for Asian-language envelopes.

RecipientNamefromLeft   Optional Variant. The position of the recipient’s name, measured in points, from the left edge of the envelope. Used for Asian-language envelopes.

RecipientNamefromTop   Optional Variant. The position of the recipient’s name, measured in points, from the top edge of the envelope. Used for Asian-language envelopes.

RecipientPostalfromLeft   Optional Variant. The position of the recipient’s postal code, measured in points, from the left edge of the envelope. Used for Asian-language envelopes.

RecipientPostalfromTop   Optional Variant. The position of the recipient’s postal code, measured in points, from the top edge of the envelope. Used for Asian-language envelopes.

SenderNamefromLeft   Optional Variant. The position of the sender’s name, measured in points, from the left edge of the envelope. Used for Asian-language envelopes.

SenderNamefromTop   Optional Variant. The position of the sender’s name, measured in points, from the top edge of the envelope. Used for Asian-language envelopes.

SenderPostalfromLeft   Optional Variant. The position of the sender’s postal code, measured in points, from the left edge of the envelope. Used for Asian-language envelopes.

SenderPostalfromTop   Optional Variant. The position of the sender’s postal code, measured in points, from the top edge of the envelope. Used for Asian-language envelopes.

ShowPrintOut method as it applies to the MailingLabel object.

Prints a label or a page of labels with the same address.

expression.PrintOut(Name, Address, ExtractAddress, LaserTray, SingleLabel, Row, Column, PrintEPostageLabel, Vertical)

expression    Required. An expression that returns a MailingLabel
object.

Name   Optional Variant. The mailing label name.

Address   Optional Variant. The text for the label address.

ExtractAddress   Optional Variant. True to use the text marked by the «EnvelopeAddress» bookmark (a user-defined bookmark) as the label text. If this argument is specified, Address and AutoText are ignored.

LaserTray   Optional Variant. The laser printer tray to be used. Can be any WdPaperTray
constant.

wdPrinterAutomaticSheetFeed
wdPrinterDefaultBin
wdPrinterEnvelopeFeed
wdPrinterFormSource
wdPrinterLargeCapacityBin
wdPrinterLargeFormatBin
wdPrinterLowerBin
wdPrinterManualEnvelopeFeed
wdPrinterManualFeed
wdPrinterMiddleBin
wdPrinterOnlyBin
wdPrinterPaperCassette
wdPrinterSmallFormatBin
wdPrinterTractorFeed
wdPrinterUpperBin

SingleLabel   Optional Variant. True to print a single label; False to print an entire page of the same label.

Row   Optional Variant. The label row for a single label. Not valid if SingleLabel is False.

Column   Optional Variant. The label column for a single label. Not valid if SingleLabel is False.

PrintEPostageLabel   Optional Variant. True to print postage using an Internet e-postage vendor.

Vertical   Optional Variant. True prints text vertically on the label. Used for Asian-language mailing labels.

Example

ShowAs it applies to the Application, Document, and Window objects.

This example prints the current page of the active document.

ActiveDocument.PrintOut Range:=wdPrintCurrentPage
				

This example prints all the documents in the current folder. The Dir function is used to return all file names that have the file name extension «.doc».

adoc = Dir("*.DOC")
Do While adoc <> ""
    Application.PrintOut FileName:=adoc
    adoc = Dir()
Loop
				

This example prints the first three pages of the document in the active window.

ActiveDocument.ActiveWindow.PrintOut _
    Range:=wdPrintFromTo, From:="1", To:="3"
				

This example prints the comments in the active document.

If ActiveDocument.Comments.Count >= 1 Then
    ActiveDocument.PrintOut Item:=wdPrintComments
End If
				

This example prints the active document, fitting six pages on each sheet.

ActiveDocument.PrintOut PrintZoomColumn:=3, _
    PrintZoomRow:=2
				

This example prints the active document at 75% of actual size.

ActiveDocument.PrintOut _
    PrintZoomPaperWidth:=0.75 * (8.5 * 1440), _
    PrintZoomPaperHeight:=0.75 * (11 * 1440)
				

ShowAs it applies to the Envelope object.

This example prints an envelope using the user address as the return address and a predefined recipient address.

recep = "Don Funk" & vbCr & "123 Skye St." & vbCr & _
    "OurTown, WA 98107"
ActiveDocument.Envelope.PrintOut Address:=recep, _
    ReturnAddress:=Application.UserAddress, _
    Size:="Size 10", PrintBarCode:=True
				

ShowAs it applies to the MailingLabel object.

This example prints a page of Avery 5664 mailing labels, using the specified address.

addr = "Jane Doe" & vbCr & "123 Skye St." _
    & vbCr & "OurTown, WA 98107"
Application.MailingLabel.PrintOut Name:="5664", Address:=addr
				

The following code will print out a word document.

'---------------------------------------------------------------------------------------
' Procedure : PrintDoc
' Author    : CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Print a Word Document
' Copyright : The following may be altered and reused as you wish so long as the
'             copyright notice is left unchanged (including Author, Website and 
'             Copyright).  It may not be sold/resold or reposted on other sites (links
'             back to this site are allowed).
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' strDoc - The path and filename of the document to be printed
' intCopies - The number of copies to be printed
'
' Usage:
' ~~~~~~~~~~~~~~~~
' PrintDoc("c:managementevaluation.doc",1)
'---------------------------------------------------------------------------------------
Function PrintDoc(strDoc As String, intCopies As Integer)
   Dim WordObj As Object

   Set WordObj = CreateObject("Word.Application")

   WordObj.Documents.Open strDoc
   WordObj.PrintOut Background:=False, Copies:=intCopies
   WordObj.Documents.Close SaveChanges:=wdDoNotSaveChanges
   WordObj.Quit

   Set WordObj = Nothing
 End Function

Понравилась статья? Поделить с друзьями:
  • Vba word paragraphformat alignment
  • Vba word выделить все таблицы в word
  • Vba word number of words
  • Vba word выделенный текст в переменную
  • Vba word no spacing