Search code examples
excelvbams-accessms-access-2010database-administration

I have some VBA that opens a spreadsheet and imports data from Access, how do I lock the original and automatically "Save As"?


The code below is attached to a command button and opens an Excel Workbook then imports a dozen (mostly redacted in code below) fields from an Access Form. The trouble I have is that users:

(1) Use the button once.

(2) Hit "Save" instead of "Save As".

(3) Use the button a second time.

(3) Complain that the workbook has old data on it.

Within the code below, how can I:

(A) Prevent users from editing the master copy of the workbook.

(B) Automatically present the "Save As" dialogue box upon opening.

Private Sub cmd1_Click()
Dim appExcel As Excel.Application
Dim wbook As Excel.Workbook
Dim wsheet As Excel.Worksheet

Set appExcel = New Excel.Application
appExcel.Visible = True
Set wbook = appExcel.Workbooks.Open("F:\Network Folder\MySpreadsheet.xlsx")
Set wsheet = wbook.Worksheets("Sheet1")

With wsheet
.Cells(10, 1).Value = txt1
.Cells(10, 2).Value = txt2   
End With
End Sub

I've tried the solutions offered here but no luck so far.


Solution

  • Try this:

    Set wbook = appExcel.Workbooks.Open(Filename:="F:\Network Folder\MySpreadsheet.xlsx", ReadOnly:=True)