Let’s say you have a Microsoft Word document and you want to write a Macro that will:
- Start upon opening
- Prompt the user for input
- Put the inputted data in specific places on your document
- Print the document.
Here is the Macro Code:
Private Sub Document_Open()
Dim custpartno As String
Dim ourpartno As String
Dim rev As String
Dim releaseqty As String
Dim ponumber As String
custpartno = InputBox(“Enter Customer Part Number:”)
If Len(custpartno) = 0 Then Exit Sub ‘ user canceled
ActiveDocument.Bookmarks(“CustomerPartNumber”).Range.Text = custpartno
ourpartno = InputBox(“Enter Our Part Number:”)
If Len(ourpartno) = 0 Then Exit Sub ‘ user canceled
ActiveDocument.Bookmarks(“OurPartNumber”).Range.Text = ourpartno
rev = InputBox(“Enter Revision:”)
If Len(rev) = 0 Then Exit Sub ‘ user canceled
ActiveDocument.Bookmarks(“revision”).Range.Text = rev
qty = InputBox(“Enter Quantity:”)
If Len(qty) = 0 Then Exit Sub ‘ user canceled
ActiveDocument.Bookmarks(“Quantity”).Range.Text = releaseqty
ponumber = InputBox(“Enter Purchase Order:”)
If Len(ponumber) = 0 Then Exit Sub ‘ user canceled
ActiveDocument.Bookmarks(“PurchaseOrder”).Range.Text = ponumber
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
MsgBox “Press Any Key to Exit.”
Application.Quit SaveChanges:=False
End Sub