HKG
HKG

Reputation: 3

excel .open method parameters in VBA

I'm having problems assigning parameters to my .open methon in VBA for Excel. Below code is what I have, all I need is to open specified workbook as read-only and not editable.

How do I set up the parameters to work, Please and Thank You!

Dim source As Workbook
Dim projekt, BOM As String
Dim count As Integer
Dim LastBOM As Long

LastBOM = Ark1.Range("I" & Rows.count).End(xlUp).Row

For count = 16 To LastBOM
BOM = Range("I" & count) 'List of Excel workbooks in active workbook
projekt = "[Path to file on drive]\" & BOM & ".xlsx"

Workbooks.Open (projekt) '<= This works and open above specified workbook 
                         '   but should be read only and not editable

Workbooks.Open(projekt,ReadOnly:=True,Editable:=False) '<= None of these work
Workbooks.Open(projekt,,True,,,,,,,False,,,,,)
Workbooks.Open(projekt,True,False)
Workbooks.Open("projekt",True,False)
Workbooks.Open(Filename:="projekt",True,False)
Workbooks.Open(Filename:=projekt,ReadOnly:=True,Editable:=False)
Workbooks.Open(Filename:="projekt",ReadOnly:=True,Editable:=False)
Next count

Upvotes: 0

Views: 999

Answers (1)

Pᴇʜ
Pᴇʜ

Reputation: 57683

Reading the documentation is always a good idea: Workbooks.Open Method (Excel) and use the ReadOnly parameter:

Workbooks.Open FileName:=projekt, ReadOnly:=True, Editable:=False

Also remove the parentheses if you don't need to return a value. Or return the workbook into a variable using parentheses:

Dim OpenedWorkbook As Workbook
Set OpenedWorkbook = Workbooks.Open(FileName:=projekt, ReadOnly:=True, Editable:=False)

I recommend the last option because you then can easily access the opened workbook eg by using:

OpenedWorkbook.Worksheets("Sheet1").Range("A1")

Upvotes: 2

Related Questions