I create an Excel file on the client machine with certain formatting and data included. The user can then add data to this file and save it. Later, that file is read and the data uploaded to a server. I want to limit what the user can do in the file so I want to add a procedure when the file is created. I can't find a way to have the VBA code added to the file when it is created.
Here's the code that creates the file:
' Launch Excel
Dim app
set app = createobject("Excel.Applic
ation")
' Make it visible
app.Visible = True
' Add a new workbook
app.workbooks.add
Const xlEdgeBottom = 9
Const xlThin = 2
Const xlDouble = -4119
Const xlThick = 4
app.Cells(1,1).Borders(xlE
dgeBottom)
.Weight = xlThick
app.Cells(1,1).Borders(xlE
dgeBottom)
.ColorInde
x = 5
app.Cells(1,1).Borders(xlE
dgeBottom)
.LineStyle
= xlDouble
app.Cells(1,2).Borders(xlE
dgeBottom)
.Weight = xlThick
app.Cells(1,2).Borders(xlE
dgeBottom)
.ColorInde
x = 5
app.Cells(1,2).Borders(xlE
dgeBottom)
.LineStyle
= xlDouble
app.Cells(1,3).Borders(xlE
dgeBottom)
.Weight = xlThick
app.Cells(1,3).Borders(xlE
dgeBottom)
.ColorInde
x = 5
app.Cells(1,3).Borders(xlE
dgeBottom)
.LineStyle
= xlDouble
app.Cells(2,1).Borders(xlE
dgeBottom)
.Weight = xlThick
app.Cells(2,1).Borders(xlE
dgeBottom)
.ColorInde
x = 5
app.Cells(2,1).Borders(xlE
dgeBottom)
.LineStyle
= xlDouble
app.Cells(2,2).Borders(xlE
dgeBottom)
.Weight = xlThick
app.Cells(2,2).Borders(xlE
dgeBottom)
.ColorInde
x = 5
app.Cells(2,2).Borders(xlE
dgeBottom)
.LineStyle
= xlDouble
app.Cells(2,3).Borders(xlE
dgeBottom)
.Weight = xlThick
app.Cells(2,3).Borders(xlE
dgeBottom)
.ColorInde
x = 5
app.Cells(2,3).Borders(xlE
dgeBottom)
.LineStyle
= xlDouble
app.Cells(1, 1).Value = "USER ID: "
app.Cells(1, 1).Font.Bold = TRUE
app.Cells(1, 1).Font.Size = 10
app.Cells(1, 1).Font.ColorIndex = 5
app.Cells(1, 1).Interior.ColorIndex = 6
app.Worksheets(1).Cells(1,
1).Font.Name = "Verdana"
app.Cells(1, 2).Value = "TITLE: "
app.Cells(1, 2).Font.Bold = TRUE
app.Cells(1, 2).Font.Size = 10
app.Cells(1, 2).Font.ColorIndex = 5
app.Cells(1, 2).Interior.ColorIndex = 6
app.Worksheets(1).Cells(1,
2).Font.Name = "Verdana"
' more code after this that adds data and does formatting
</script>
Here's the procedure: (this works when it is in the file)
Private Sub Worksheet_SelectionChange(
ByVal Target As Range)
Dim rng As Range
Dim celle As Range
Set rng = Range(Sheets("Sheet1").[A3
], Sheets("Sheet1").[C50])
If ActiveCell.Value <> "" Then
Exit Sub
End If
For Each celle In rng
If celle = "" Then
celle.Select
Exit Sub
End If
Next celle
End Sub
Start Free Trial