Bharath
Bharath

Reputation: 15

Excel vba open all word document in a folder and print by getting number of copies from user

I am new to Macro

By googling I coded this and I have changed some part for my use. Problem is Runtime error is coming. And I don't know how to print all word documents in folder both .doc and .docx

My Requirement

  1. Want to print all word document in folder A (both .doc and .docx).
  2. Print active document ( Number of copies want to be get from User ).
  3. Close active document.
  4. Repeat 2 and 3 for all document in folder A

My code will get page number to print from case selected by the user

case 1 will print 1st two pages one by one. case 2 will print 3rd to reset of the pages. case 3 will print full document.

In my office duplex is default printer setup to print. But I will be using letter head. I need this macro to solve my issue. I tried simplex macro code to print but its not working.

Sub prnt()

Dim c As Integer
Dim i As Integer
Dim strName As String

'get print type
strName = InputBox(Prompt:="Choose Your Option" & vbNewLine & "" & vbNewLine & "1. Letter Head" & vbNewLine & "2. A4 Sheet" & vbNewLine & "3. Comp Plan" & vbNewLine & "", _
          Title:="ENTER YOUR PRINT TYPE", Default:="Your Choice here")

    If strName = "Your Choice here" Or strName = vbNullString Then
        MsgBox "Sorry...! Choose Correct option"
        Exit Sub
    Else
        'case to choose option
        Select Case strName

            Case "1"

                Dim file
                Dim path As String
                Dim ans As String

                'get number of copies from user
                c = InputBox("Please enter number of copies")
                ans = MsgBox("Are you sure you want to print " & _
                    c & "?", _
                    vbQuestion + vbYesNo, "Print pages")

                If ans = vbNo Then
                    Exit Sub
                Else
                    'path to the folder
                    path = "E:\print\"
                    file = Dir(path & "*.docx")
                    Do While file  ""
                        Documents.Open Filename:=path & file
                        For i = 1 To 2  'loop 2 pages
                            ActiveDocument.PrintOut , Copies:=c, Range:=wdPrintRangeOfPages, Pages:=i
                        Next 
                        ActiveDocument.Close
                        ' set file to next in Dir
                        file = Dir()
                    Loop 
                End If

            Case "2"
            Case "3"
            Case Else
                MsgBox "Sorry...! Choose Correct option"

        End Select

    End If

End Sub

Upvotes: 0

Views: 2664

Answers (1)

Maciej Los
Maciej Los

Reputation: 8591

There's bad programming practice to work on strings instead of numbers.

See this:

Sub Test()
Dim noofcopies As Integer

noofcopies = GetNumberOfCopies()

MsgBox noofcopies

End Sub


Function GetNumberOfCopies() As Integer
Dim iRetVal As Integer

On Error GoTo Err_GetNumberOfCopies

iRetVal = CInt(InputBox("Enter no. of copies to print" & vbCr & vbCr & _
                "Enter proper integer value between 1 and 3" & vbCr & _
                "0 (zero) equals to Cancel", "No. of copies", "1"))

If iRetVal > 3 Then iRetVal = 3

Exit_GetNumberOfCopies:
    GetNumberOfCopies = iRetVal
    Exit Function

Err_GetNumberOfCopies:
    Err.Clear
    Resume 0

End Function

Use the same logic to get print option ;)

Upvotes: 0

Related Questions