简体   繁体   中英

Why is Workbook.Open() triggered twice on Workbook.SaveAs

I just need my users to use the automatically saved copy of my file on their desktop when the file is located on SharePoint.

It looks like the Workbook.Open is triggered on SaveAs, as it execute the same code twice. I want it to close the SharePoint file - and reopen the new file from users desktop, but it seams to respond with the same path.

I have tried this in ThisWorkbook code:

`

Private Sub Workbook_Open()

MsgBox ThisWorkbook.Path
If Left(ThisWorkbook.Path, 2) <> "C:" Then
MsgBox "This workbook will now be saved on you desktop. Please use it from your desktop location."

    ThisWorkbook.SaveAs Filename:="C:\Users\" & Environ$("Username") & _
    "\Desktop\" & ThisWorkbook.Name, _
    FileFormat:=xlOpenXMLWorkbookMacroEnabled

    ThisWorkbook.Close

End If

End Sub

`

When using SaveAs (correct me if I'm wrong) on ThisWorkbook, it'll get you straight in the newly created file, instead of making a copy of itself and saving it in the same path. That's at least how I understood it since the MsgBoxes weren't triggered so it wasn't like the Workbook_Open got triggered again.

My workaround to not have it continue the code on it as follows, however when their C is virtually on the sharepoint (as I figured out mine is), the "C:" check is automatically True since your path will be from the sharepoint onwards (I noticed this with the second commented MsgBox):

Private Sub Workbook_Open()

    Dim cFileName As String, cFileExists As String
    Dim wb As Workbook
'    MsgBox ThisWorkbook.Path
'    MsgBox Left(ThisWorkbook.Path, 2)
    If Left(ThisWorkbook.Path, 2) <> "C:" Then 'put your full path of original file here
        MsgBox "This workbook will now be saved on you desktop. Please use it from your desktop location."
        cFileName = "C:\Users\" & Environ("Username") & "\Dekstop\" & ThisWorkbook.Name
        cFileExists = Dir(cFileName)
        If cFileExists = "" Then 'check if it exists already
            Set wb = ActiveWorkbook
            wb.SaveCopyAs cFileName
        Else
            MsgBox "This file already exists"
        End If
        ThisWorkbook.Close
    End If
    
End Sub

This way you can't have it open after running the code however. What you can also try is: https://stackoverflow.com/a/19846141/19353309

Edit:

Private Sub Workbook_Open()

    Dim cFileName As String, cFileExists As String
    Dim wb As Workbook
    If InStr(1, ActiveWorkbook.Path, Environ("Username")) > 0 Then Exit Sub 'if it's not original file, exit sub immediately, just don't put this file in a path with your name in it :p
    'I would have used "C:\Users\" & Environ("Username") but this did not work with my copy.Path being part of the sharepoint even when the file is saved on C:\
    cFileName = "C:\Users\" & Environ("Username") & "\Desktop\" & Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 5) & " - " & Environ("Username") & ".xlsm" 'this way both files can be open (can't have two workbooks with the same name open)
    Set wb = ActiveWorkbook
    cFileExists = Dir(cFileName)
    If cFileExists = "" Then 'copy does not exist yet
        MsgBox "This workbook will now be saved on you desktop. Please use it from your desktop location."
        wb.SaveCopyAs cFileName
    Else 'copy exists
        MsgBox ("File already exists, opening this version")
    End If
    Workbooks.Open Filename:=cFileName
    wb.Close
    
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM