Advertisement

05.29.2008 at 02:13PM PDT, ID: 23442984
[x]
Attachment Details

Create file on client with vba procedure in new file

Asked by bkittle in Microsoft Excel Spreadsheet Software, VB Script

Tags: Microsoft, Excel, 2003

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.Application")
         
          ' 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(xlEdgeBottom).Weight = xlThick
            app.Cells(1,1).Borders(xlEdgeBottom).ColorIndex = 5
            app.Cells(1,1).Borders(xlEdgeBottom).LineStyle = xlDouble
            
            app.Cells(1,2).Borders(xlEdgeBottom).Weight = xlThick
            app.Cells(1,2).Borders(xlEdgeBottom).ColorIndex = 5
            app.Cells(1,2).Borders(xlEdgeBottom).LineStyle = xlDouble
            
            app.Cells(1,3).Borders(xlEdgeBottom).Weight = xlThick
            app.Cells(1,3).Borders(xlEdgeBottom).ColorIndex = 5
            app.Cells(1,3).Borders(xlEdgeBottom).LineStyle = xlDouble


            app.Cells(2,1).Borders(xlEdgeBottom).Weight = xlThick
            app.Cells(2,1).Borders(xlEdgeBottom).ColorIndex = 5
            app.Cells(2,1).Borders(xlEdgeBottom).LineStyle = xlDouble
            
            app.Cells(2,2).Borders(xlEdgeBottom).Weight = xlThick
            app.Cells(2,2).Borders(xlEdgeBottom).ColorIndex = 5
            app.Cells(2,2).Borders(xlEdgeBottom).LineStyle = xlDouble
            
            app.Cells(2,3).Borders(xlEdgeBottom).Weight = xlThick
            app.Cells(2,3).Borders(xlEdgeBottom).ColorIndex = 5
            app.Cells(2,3).Borders(xlEdgeBottom).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
[+][-]05.29.2008 at 02:32PM PDT, ID: 21673476

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.29.2008 at 03:26PM PDT, ID: 21673871

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.29.2008 at 03:31PM PDT, ID: 21673893

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.29.2008 at 04:32PM PDT, ID: 21674190

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.29.2008 at 04:37PM PDT, ID: 21674221

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.29.2008 at 04:51PM PDT, ID: 21674272

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Excel Spreadsheet Software, VB Script
Tags: Microsoft, Excel, 2003
Sign Up Now!
Solution Provided By: matthewspatrick
Participating Experts: 2
Solution Grade: B
 
 
[+][-]05.30.2008 at 11:44AM PDT, ID: 21680374

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628