0

Scenario

I have a file that I am opening as ReadOnly using the following code.

Set wbRead = Workbooks.Open(FilePath, ReadOnly:=True)

Here the FilePath is a variable that tells the file location of that file

Problem

The issue I am facing is, if the user run macro second time without closing this already opened readonly file, it is giving runtime error due to having similar file name opened

What I need

Is there any way whereby excel can open a file as readonly, but the opened file shows some random name? Eg: Actual file name is A. But when excel open it as readonly, it open as A123? 123 is like a random number.

5
  • No. The only way this could be done would be to actually rename the file before you open it, which you won't be able to do because Excel has it open. But you're asking the wrong question anyway. Your question should be How can I stop my users from running my macro twice while the file is still open? Commented Oct 15, 2018 at 2:49
  • Would you really want them to be able to open the same file twice? Commented Oct 15, 2018 at 3:01
  • @KenWhite Users need to run the macro as many times as they want. Currently they have to save the read only file to somewhere and close it before they run second time which they don't prefer. So the question is correct. Just that if there is a way to do it. Commented Oct 15, 2018 at 3:27
  • @TimWilliams Yes. Not only twice. As many times as they want Commented Oct 15, 2018 at 3:27
  • Copy the file to the temp folder under a random name, and open the copy. Commented Oct 15, 2018 at 4:29

2 Answers 2

2

Abother Solution would be to always (open or not) use Workbooks.Add to create a new copy of your file. Excel will automatically prompt you to save under a new name when you close:

Set wbRead = Workbooks.Add(FilePath)
Sign up to request clarification or add additional context in comments.

Comments

1

If the file is already open, make a copy in the temp folder under a different name, and open it from there.

Sub OpenFile()

    Const fPath As String = "C:\users\tim\desktop\tmp.xlsm"

    Dim fso, wb As Workbook, fName, p
    Set fso = CreateObject("scripting.filesystemobject")

    p = fPath
    fName = fso.getfilename(p)

    On Error Resume Next
    Set wb = Workbooks(fName)
    On Error GoTo 0

    If Not wb Is Nothing Then
        p = fso.GetSpecialFolder(2) & "\" & Round(Rnd() * 1000, 0) & "_" & fName
        fso.copyfile fPath, p
    End If
    Workbooks.Open p


End Sub

2 Comments

I tried this code, but it is not working. It works only when I change If Not wb Is Nothing to If wb is Nothing. By the way if use this method, will some objects be saved in memory? Because my application is a memory hungry type and I need to make sure memory is not using un-necessarily.
Files will only be in memory as long as they’re open

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.