WHY the workbook is locked!

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

WHY the workbook is locked!

Post by sal21 »

When i open the workbook from Excel! ... is loked REPORT_ESTERO.xls

My code:

Code: Select all

Private Sub Command2_Click()

'REGIONE

    Dim DATANOW As String

    Me.Text1.SetFocus

    Screen.MousePointer = vbHourglass
    DoEvents

    If Me.LNRC.Caption > "" Then

        Me.LAZIONI.Caption = "ESPORT REGIONI .XLS. ATTENDERE PREGO!"
        DoEvents

        DATANOW = Format(CDate(Now), "DD-MM-YYYY")

        Set OBJXL = CreateObject("Excel.Application")
        Set WBXL = OBJXL.Workbooks.Open(STRPATHXLS & "REPORT_ESTERO.xls")
        OBJXL.Visible = False

        Set WSXL = WBXL.Sheets("REGIONI")

        Dim R As Long, C As Long, NUMC As Integer

        OBJXL.ScreenUpdating = False

        With Me.MSFlexGrid1

            NUMC = .cols - 5

            For R = 2 To .rows - 1

                For C = 0 To NUMC

                    WSXL.cells(R, C + 1).Value = .TextMatrix(R, C)
                    DoEvents

                Next C

            Next R

        End With

        OBJXL.ScreenUpdating = True

        If Dir(STRPATHXLS & "REPORT_REGIONI-" & ANNO & "-" & DATANOW & ".xls") <> "" Then
            Kill ("REPORT_REGIONI-" & ANNO & "-" & DATANOW & ".xls")
        End If

        OBJXL.DisplayAlerts = False
        WBXL.Sheets("PROVINCE").Delete
        OBJXL.DisplayAlerts = True

        Dim answer As Integer
        answer = MsgBox("VUOI ANCHE STAMPARE?", vbQuestion + vbYesNo)
        If answer = vbYes Then
            Me.LAZIONI.Caption = "STAMPA IN CORSO..."
            DoEvents
            WBXL.Sheets("REGIONI").PrintOut
        End If

        WBXL.SaveAs STRPATHXLS & "REPORT_REGIONI-" & ANNO & "-" & DATANOW & ".xls"
        WBXL.Close SaveChanges:=False

        Set WBXL = Nothing
        Set WSXL = Nothing

        OBJXL.Quit
        Set OBJXL = Nothing

        Me.LAZIONI.Caption = "FINE ESPORT!"
        DoEvents
        Sleep (1500)
        Me.LAZIONI.Caption = ""

        Me.Text1.SetFocus

    Else

        Beep
        Me.LAZIONI.Caption = "NESSUN DATO DA ESPORTARE!"
        DoEvents
        Sleep (1500)
        Me.LAZIONI.Caption = ""
        Me.Text1.SetFocus

    End If

    Screen.MousePointer = vbDefault
    DoEvents

End Sub
You do not have the required permissions to view the files attached to this post.

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

Re: WHY the workbook is locked!

Post by HansV »

What happens if double-click REPORT_ESTERO.xls in File Explorer? Do you get the same message?
Best wishes,
Hans

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

Re: WHY the workbook is locked!

Post by sal21 »

HansV wrote:
30 Apr 2024, 18:40
What happens if double-click REPORT_ESTERO.xls in File Explorer? Do you get the same message?
Yes!

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

Re: WHY the workbook is locked!

Post by HansV »

So the problem is not your code. Either the workbook is already open, or its author has set it to be read-only. You can change the line

Code: Select all

        Set WBXL = OBJXL.Workbooks.Open(STRPATHXLS & "REPORT_ESTERO.xls")
to

Code: Select all

    Set WBXL = OBJXL.Workbooks.Open(Filename:=STRPATHXLS & "REPORT_ESTERO.xls", ReadOnly:=True)
But if you edit the workbook, you'll have to save it under a different name.
Best wishes,
Hans

User avatar
SpeakEasy
5StarLounger
Posts: 601
Joined: 27 Jun 2021, 10:46

Re: WHY the workbook is locked!

Post by SpeakEasy »

Got any unexpected instances of Excel showing in Task Manager?