Error 1004 - Excel cannot access "filename". The document may be read-only or encrypted

tigerdel

Board Regular
Joined
Oct 13, 2015
Messages
145
Office Version
  1. 365
Platform
  1. Windows
Good morning experts
I am trying to get a macro to open the users Onedrive folder and allow them the option of which file to open but I keep getting the error in the picture.
The folder is neither read-only or encrypted
The code I tried is:
Workbooks.Open ("C:\Users\" & Environ("UserName") & "\OneDrive\SIT")
Am I missing something??
Many thanks for looking
1596711523380.png
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Just a thought - do you need the file extension, too?
Workbooks.Open ("C:\Users\" & Environ("UserName") & "\OneDrive\SIT.xlsx")

One other thing; the user needs to be logged into OneDrive, to access files, if I remember correctly.
Not sure whether this is the case on a network, though.
 
Upvote 0
OK I guess I am approaching this all wrong
What I am trying to do is, have a button that when click will go to a specific folder in the users OneDrive and allow them to open a csv file
I already have the code to pull the data from the csv file into a specific sheet but I want the button to go to where the csv file is stored so they don't need to go searching for the folder each time
 
Upvote 0
This should work; the only thing I can't be sure about is the path to the folder - especially concerning the username. This is because I have an odd file setup, whereby my OneDrive folder isn't stored under my username, so you may need to tweak this a little.
VBA Code:
Sub opn()
Dim op_fl As String

    With Application.FileDialog(msoFileDialogOpen)
         .InitialFileName = "C:\Users\" & Environ("UserName") & "\OneDrive\SIT"
         .Filters.Clear
        .Filters.Add "CSV Files", "*.csv"
        If .Show = -1 Then
            op_fl = .SelectedItems(1)
        End If
    End With
    Workbooks.Open op_fl
End Sub
You just need to bolt it onto a command button.

HTH
 
Upvote 0
Hurrah!
Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,580
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top