Need To Open File In Read-Only

ranjith2523

Board Regular
Joined
Apr 29, 2014
Messages
137
Office Version
  1. 365
Hi Experts,

I wrote the below coding to open a file based on wildcard file name match, the file is password protected file and i would like to open it as Read-Only, getting Compile or Syntax error when i try to add "ReadOnly:=True".

This method works ("Workbooks.Open Filename:="C:\DE6_AFTERREP3\SP_Files\FY21_TS_Price_contractual.XLSX", ReadOnly:=True") as expected but my expectation is to open the file based on wild card file name match.

Can someone help me to incorporate "Readonly = True" in below coding. Thanks for all your help in advance.


VBA Code:
Dim myFile As String
Dim myPath As String


myPath = "C:\DE6_AFTERREP3\SP_Files\"
myFile = Dir(myPath & "*TS_Price_contractual.xlsx", vbNormal)

If Len(myFile) = 0 Then
MsgBox "Agreement file not found...", vbExclamation
Exit Sub
End If

targetfile = myPath & myFile
Set wb = Workbooks.Open(targetfile) ReadOnly:=True
Set ws = wb.Sheets("FY_TS_Price_contractual")

wb.Sheets("FY21_Log").Cells.Copy ThisWorkbook.Sheets("Agreement_SP").Range("A1")

wb.Close

Regards,
Ranjith
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I found a solution :) Please ignore !!! and sorry if anyone started work on it.



VBA Code:
Set wb = Workbooks.Open(Filename:=myPath & myFile, UpdateLinks:=False, ReadOnly:=True)
 
Upvote 0

Forum statistics

Threads
1,215,056
Messages
6,122,907
Members
449,096
Latest member
dbomb1414

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