Is this correct to close the workbook and sheet

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

Is this correct to close the workbook and sheet

Post by sal21 »

Code: Select all

Private Sub PRINT_REPORT_SCORTE()

    Set XLAPP = CreateObject("Excel.Application")
    Set XLWB = XLAPP.Workbooks.Open(FileName:=STRWB1, Editable:=True)
    Set WS = XLWB.Worksheets("SCORTE")
    WS.Range("B2:M65536").ClearContents

    CONTA = 0
    RIGA = 2
    COLONNA = 0

    With WS
    
        .Application.ScreenUpdating = False

        For K = 1 To Me.LVBAR.ListItems.Count

            .Cells(RIGA, COLONNA + 1) = Me.LVSCORTE.ListItems(K).Text
            .Cells(RIGA, COLONNA + 2) = Me.LVSCORTE.ListItems(K).ListSubItems(1).Text
            .Cells(RIGA, COLONNA + 3) = Me.LVSCORTE.ListItems(K).ListSubItems(2).Text
            .Cells(RIGA, COLONNA + 4) = Me.LVSCORTE.ListItems(K).ListSubItems(3).Text
            .Cells(RIGA, COLONNA + 5) = Me.LVSCORTE.ListItems(K).ListSubItems(4).Text
            .Cells(RIGA, COLONNA + 6) = Me.LVSCORTE.ListItems(K).ListSubItems(5).Text
            .Cells(RIGA, COLONNA + 7) = Me.LVSCORTE.ListItems(K).ListSubItems(6).Text
            .Cells(RIGA, COLONNA + 8) = Me.LVSCORTE.ListItems(K).ListSubItems(7).Text
            .Cells(RIGA, COLONNA + 9) = Me.LVSCORTE.ListItems(K).ListSubItems(8).Text

            RIGA = RIGA + 1
            COLONNA = 0
            CONTA = CONTA + 1
            
            DoEvents

        Next K

        .Application.ScreenUpdating = True
        'IMPOSTAZIONI PAGINA DI STAMPA
        .PageSetup.CenterHeader = "REPORT SCORTE DEL: " & Format(Date, "DD/MM/YYYY")
        .PageSetup.LeftFooter = "REPORT STAMPATO IL:  " & Format(Date, "DD/MM/YYYY")
        .PageSetup.RightFooter = "PRODOTTI NR:  " & CONTA
        .PageSetup.PaperSize = vbPRPSA4
        'IMPOSTAZIONI PAGINA DI STAMPA
        
    End With

    XLAPP.Activesheet.PrintOut Copies:=1, Collate:=True

    Set WS = Nothing
    XLWB.Close SaveChanges:=False
    Set XLWB = Nothing
    XLAPP.Quit
    Set XLAPP = Nothing

End Sub
i'm not sure to the end part of this code... to close workbook and sheet.

Set WS = Nothing
XLWB.Close SaveChanges:=False
Set XLWB = Nothing
XLAPP.Quit
Set XLAPP = Nothing


why?
to the end of code, when i open the workbook, appear a windows in image.
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78446
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Is this correct to close the workbook and sheet

Post by HansV »

The code looks OK. Perhaps you should wait a few seconds before opening the workbook.

Do you have a specific reason for using an existing workbook in the code, instead of creating a new one?
Best wishes,
Hans