Consulting

Results 1 to 12 of 12

Thread: Solved: VBA Excel Close Non Active File

  1. #1

    Solved: VBA Excel Close Non Active File

    Hi all,

    This is a two part question.

    1) How can I modify the code below to close the non active file after printing
    2) How could I modify the code to print the file without opening it at all


    [VBA]Sub PrintForm()

    Dim Month As String

    'input box to put a variable (folder name) for the file path

    Month = Application.Inputbox("Please enter the file path variable")

    'puts the variable name on the macro sheet where all the files to be printed are listed

    Cells(1, 7) = Month


    rowno = 2

    'this will tell the macro if the file type is excel, word or pdf

    FileType = Trim(Cells(rowno, 2))


    Do Until Cells(rowno, 2) = ""

    If FileType = "Excel" Then

    'call the excel piece
    Inputs (rowno)

    End If

    If FileType = "PDF" Then

    'call the PDF piece (I have not included this sub)
    PrintPDFsInputBox (rowno)


    End If

    rowno = rowno + 1

    Loop




    End Sub


    Sub Inputs(ByVal x As Integer)


    'tells the macro that the macro is the active workbbook so that it can go_
    'back to the active workbook after opening other workbooks

    Dim w As Workbook
    Set w = ActiveWorkbook


    'dim all my variables, these are put in my the user on the main macro workbook

    Dim Variablez As String
    Dim MyFolder As String
    Dim MyFile As String
    Dim FileName As String
    Dim Action As String

    MyFolder = Cells(x, 3)
    FileName = Cells(x, 5)
    Action = Cells(x, 6)
    Variablez = Cells(1, 7)


    MyFile = Dir(MyFolder & Variablez & "\" & FileName)


    If MyFile <> "" And Action = "Open" Then

    Workbooks.Open MyFile

    End If

    If MyFile <> "" And Action = "Print" Then

    'THIS IS THE AREA I NEED HELP WITH
    Workbooks.Open MyFile
    Sheets.PrintOut


    'I HAD TRIED THIS TO CLOSE THE INACTIVE WORKBOOK (Myfile)
    'MyFile.Close


    End If


    If MyFile = "" Then

    MsgBox ("FileName does not exist, please check the file is saved out correctly")



    End If


    w.Activate

    End Sub[/VBA]

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    [vba]
    Dim wbk as Workbook
    Set wbk = Workbooks.Open(MyFile)
    wbk.Sheets.PrintOut
    wbk.Close False[/vba]

    for example.
    Be as you wish to seem

  3. #3
    Hi Aflatoon,

    I'm getting an error "compile error: duplicate declaration in current scope"

    I have indicated where I have put your code, perhaps I have put it in the wrong place

    [VBA]Sub Inputs(ByVal x As Integer)


    'tells the macro that the macro is the active workbbook so that it can go_
    'back to the active workbook after opening other workbooks

    Dim w As Workbook
    Set w = ActiveWorkbook

    Dim wbk As Workbook
    Set wbk = Workbooks.Open(MyFile)



    'dim all my variables, these are put in my the user on the main macro workbook

    Dim Variablez As String
    Dim MyFolder As String
    Dim MyFile As String

    'AFLATOONS CODE
    Dim wbk As Workbook
    Set wbk = Workbooks.Open(MyFile)

    Dim FileName As String
    Dim Action As String

    MyFolder = Cells(x, 3)
    FileName = Cells(x, 5)
    Action = Cells(x, 6)
    Variablez = Cells(1, 7)


    MyFile = Dir(MyFolder & Variablez & "\" & FileName)


    If MyFile <> "" And Action = "Open" Then

    Workbooks.Open MyFile

    End If

    If MyFile <> "" And Action = "Print" Then

    'THIS IS THE AREA I NEED HELP WITH
    'Workbooks.Open MyFile
    'Sheets.PrintOut

    'AFLATOONS CODE
    'Dim wbk As Workbook
    'Set wbk = Workbooks.Open(MyFile)

    wbk.Sheets.PrintOut
    wbk.Close False


    'I HAD TRIED THIS TO CLOSE THE INACTIVE WORKBOOK (Myfile)
    'MyFile.Close


    End If


    If MyFile = "" Then

    MsgBox ("FileName does not exist, please check the file is saved out correctly")



    End If


    w.Activate

    End Sub[/VBA]

  4. #4
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Why did you add:
    [vba] Dim wbk As Workbook
    Set wbk = Workbooks.Open(MyFile) [/vba]
    twice? It should only be there once.
    Be as you wish to seem

  5. #5
    Apologies Aflatoon, just saw my own silly mistake. Your code works perfectly.

  6. #6
    Hi Aflatoon,

    Last question, if I wanted to print the file without opening it could I use something similar to what you advised above. I tried a number of combinations but without success.

    Thanks

  7. #7
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    You can't print a file without opening it to the best of my knowledge. Certainly not an Excel file.
    Be as you wish to seem

  8. #8
    VBAX Mentor
    Joined
    Feb 2009
    Posts
    493
    Location
    You can print it with out opening it but then you are going to need to start using the windows API.
    -----------------------------------------
    The more you learn about something the more you know you have much to learn.

  9. #9
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Even if you use something like ShellEexcute, the file will be opened, printed and then closed - or did you have another API function in mind?
    Be as you wish to seem

  10. #10
    Hi Aflatoon,

    I am going to Mark this thread as solved. Thanks again for your help.

    Des

  11. #11
    VBAX Mentor
    Joined
    Feb 2009
    Posts
    493
    Location
    I was thinking send to printer but your right it ends up being opened by the default program anyway.
    -----------------------------------------
    The more you learn about something the more you know you have much to learn.

  12. #12
    Thank you also for your time Brian

Posting Permissions

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