Runtime error 91 ??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Smigger
    New Member
    • Nov 2007
    • 5

    #1

    Runtime error 91 ??

    Afternoon,

    I'm having a little trouble with the code listed below, in Access 2003 which is opening and editing an existing Excel 2003 workbook.

    The section of code works the first time round, however, on the second pass the Runtime error 91 comes to haunt me at the line highlighted in bold below.

    The idea of the script is to update a standard customer questionaire (xls file) with some basic details from a database and then attach the now updated personalized excel file and mails it to the user

    Sub ControlOutlook( )

    Dim objOutlook As New Outlook.Applica tion
    Dim objEmail As Outlook.MailIte m
    Dim strLtrContent As String
    Dim ExcelApp As New Excel.Applicati on
    Dim ExcelFile As New Excel.Workbook
    Dim ExcelSheet As New Excel.Worksheet
    Dim SheetName As String
    Dim rsSmartData As New ADODB.Recordset

    rsSmartData.Act iveConnection = CurrentProject. Connection
    rsSmartData.Ope n "SmartData"

    'For each record in the Smart Data table send and e-mail

    Do While Not rsSmartData.EOF

    ' mail construct goes here

    strLtrContent = "Dear" & " " & rsSmartData("Cu stomer Name") & ":" & vbCrLf & vbCrLf
    strLtrContent = strLtrContent & "You have been selected to take part in our customer satisfaction survey, "
    strLtrContent = strLtrContent & "Would you please take the time to compelete the attached form. "
    strLtrContent = strLtrContent & vbCrLf & vbCrLf & "Please do NOT reply to the sender of this e-mail"
    strLtrContent = strLtrContent & vbCrLf & vbCrLf & "Sincerly," & vbCrLf & vbCrLf
    strLtrContent = strLtrContent & "Distribute d Provisioning"

    ' This section is designed to open up the standard customer questionnaire
    ' and save the details per line item within this loop

    ExcelApp.Visibl e = True
    Set ExcelFile = ExcelApp.Workbo oks.Open("S:\Ad min- BF\ESCQ\Questio naire\ECSQ.xls" )
    Set ExcelSheet = ExcelApp.Worksh eets("Answers")

    With ExcelSheet

    ActiveSheet.Unp rotect ("****")

    .Range("C2").Va lue = rsSmartData("Jo b ref")
    .Range("D2").Va lue = rsSmartData("Un ique Job Ref")
    .Range("F2").Va lue = rsSmartData("Ca talogue Description")
    .Range("G2").Va lue = rsSmartData("Pr oduct Name")

    ActiveSheet.Pro tect ("****")

    End With

    Excel.Applicati on.DisplayAlert s = False
    ExcelFile.Save
    ExcelFile.Close True
    ExcelApp.Quit

    Set ExcelSheet = Nothing
    Set ExcelFile = Nothing
    Set ExcelApp = Nothing

    ' Prepares the mail for sending

    Set objEmail = objOutlook.Crea teItem(olMailIt em)
    objEmail.Recipi ents.Add rsSmartData("Re cipient ID")
    objEmail.Subjec t = "Order IT Customer Satisfaction Survey. "
    objEmail.Body = strLtrContent
    objEmail.Attach ments.Add ("S:\Admin- BF\ESCQ\Questio naire\ECSQ.xls" )

    objEmail.Send

    rsSmartData.Mov eNext

    Loop

    End Sub
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Originally posted by Smigger
    Afternoon,

    I'm having a little trouble with the code listed below, in Access 2003 which is opening and editing an existing Excel 2003 workbook.

    The section of code works the first time round, however, on the second pass the Runtime error 91 comes to haunt me at the line highlighted in bold below.

    The idea of the script is to update a standard customer questionaire (xls file) with some basic details from a database and then attach the now updated personalized excel file and mails it to the user

    Sub ControlOutlook( )

    Dim objOutlook As New Outlook.Applica tion
    Dim objEmail As Outlook.MailIte m
    Dim strLtrContent As String
    Dim ExcelApp As New Excel.Applicati on
    Dim ExcelFile As New Excel.Workbook
    Dim ExcelSheet As New Excel.Worksheet
    Dim SheetName As String
    Dim rsSmartData As New ADODB.Recordset

    rsSmartData.Act iveConnection = CurrentProject. Connection
    rsSmartData.Ope n "SmartData"

    'For each record in the Smart Data table send and e-mail

    Do While Not rsSmartData.EOF

    ' mail construct goes here

    strLtrContent = "Dear" & " " & rsSmartData("Cu stomer Name") & ":" & vbCrLf & vbCrLf
    strLtrContent = strLtrContent & "You have been selected to take part in our customer satisfaction survey, "
    strLtrContent = strLtrContent & "Would you please take the time to compelete the attached form. "
    strLtrContent = strLtrContent & vbCrLf & vbCrLf & "Please do NOT reply to the sender of this e-mail"
    strLtrContent = strLtrContent & vbCrLf & vbCrLf & "Sincerly," & vbCrLf & vbCrLf
    strLtrContent = strLtrContent & "Distribute d Provisioning"

    ' This section is designed to open up the standard customer questionnaire
    ' and save the details per line item within this loop

    ExcelApp.Visibl e = True
    Set ExcelFile = ExcelApp.Workbo oks.Open("S:\Ad min- BF\ESCQ\Questio naire\ECSQ.xls" )
    Set ExcelSheet = ExcelApp.Worksh eets("Answers")

    With ExcelSheet

    ActiveSheet.Unp rotect ("****")

    .Range("C2").Va lue = rsSmartData("Jo b ref")
    .Range("D2").Va lue = rsSmartData("Un ique Job Ref")
    .Range("F2").Va lue = rsSmartData("Ca talogue Description")
    .Range("G2").Va lue = rsSmartData("Pr oduct Name")

    ActiveSheet.Pro tect ("****")

    End With

    Excel.Applicati on.DisplayAlert s = False
    ExcelFile.Save
    ExcelFile.Close True
    ExcelApp.Quit

    Set ExcelSheet = Nothing
    Set ExcelFile = Nothing
    Set ExcelApp = Nothing

    ' Prepares the mail for sending

    Set objEmail = objOutlook.Crea teItem(olMailIt em)
    objEmail.Recipi ents.Add rsSmartData("Re cipient ID")
    objEmail.Subjec t = "Order IT Customer Satisfaction Survey. "
    objEmail.Body = strLtrContent
    objEmail.Attach ments.Add ("S:\Admin- BF\ESCQ\Questio naire\ECSQ.xls" )

    objEmail.Send

    rsSmartData.Mov eNext

    Loop

    End Sub
    The problem seems to be that your are Protecting/Unprotecting the Worksheet within the context of the With...End With Construct:
    [CODE=vb]
    With ExcelSheet
    ActiveSheet.Unp rotect ("****")

    .Range("C2").Va lue = rsSmartData("Jo b ref")
    .Range("D2").Va lue = rsSmartData("Un ique Job Ref")
    .Range("F2").Va lue = rsSmartData("Ca talogue Description")
    .Range("G2").Va lue = rsSmartData("Pr oduct Name")

    ActiveSheet.Pro tect ("****")
    End With[/CODE]
    Which may evaluate to:
    [CODE=vb]ExcelSheetActiv eSheet.Unprotec t ("****")
    ExcelSheetActiv eSheet.Protect ("****")[/CODE]
    which is of course, meaningless.

    Comment

    • Smigger
      New Member
      • Nov 2007
      • 5

      #3
      Thanks i've tried moving the code as below but obtain the same error, if I unprotect the file being opened and disable the unprotect / protect code within this module the script runs as required ??? Unfortunately I really need the protection on this document, any thoughts?

      Code:
      Activesheet.Unprotect("****")
      
      With ExcelSheet
       
        .Range("C2").Value = rsSmartData("Job ref")
        .Range("D2").Value = rsSmartData("Unique Job Ref")
        .Range("F2").Value = rsSmartData("Catalogue Description")
        .Range("G2").Value = rsSmartData("Product Name")
       
      End With
      
      Activesheet.Protect("****")

      Comment

      • FishVal
        Recognized Expert Specialist
        • Jun 2007
        • 2656

        #4
        Hi, Smigger.

        The code is executed in Access module. Am I right? If so, then you should refer to Excel.Applicati on explicitely.
        [code=vb]
        ExcelApp.Active sheet.Unprotect ("****")
        [/code]
        or even better if you have object variable referenced to target Worksheet, then it is more reliable to run Unprotect method of it.
        [code=vb]
        With ExcelSheet
        .Activesheet.Un protect("****")
        .Range("C2").Va lue = rsSmartData("Jo b ref")
        .Range("D2").Va lue = rsSmartData("Un ique Job Ref")
        .Range("F2").Va lue = rsSmartData("Ca talogue Description")
        .Range("G2").Va lue = rsSmartData("Pr oduct Name")
        .Activesheet.Pr otect("****")
        End With
        [/code]

        Regards,
        Fish.

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          Originally posted by Smigger
          Thanks i've tried moving the code as below but obtain the same error, if I unprotect the file being opened and disable the unprotect / protect code within this module the script runs as required ??? Unfortunately I really need the protection on this document, any thoughts?

          Code:
          Activesheet.Unprotect("****")
          
          With ExcelSheet
           
            .Range("C2").Value = rsSmartData("Job ref")
            .Range("D2").Value = rsSmartData("Unique Job Ref")
            .Range("F2").Value = rsSmartData("Catalogue Description")
            .Range("G2").Value = rsSmartData("Product Name")
           
          End With
          
          Activesheet.Protect("****")
          I think FishVal gave you some excellant advice. I tested the following subset of your code and it works flawlessly. Just parallel this code, and you shouold be OK.
          [CODE=vb]
          Dim ExcelApp As New Excel.Applicati on
          Dim ExcelFile As New Excel.Workbook
          Dim ExcelSheet As New Excel.Worksheet
          Dim SheetName As String


          ExcelApp.Visibl e = True
          Set ExcelFile = ExcelApp.Workbo oks.Open("C:\Te st\Test.xls")
          Set ExcelSheet = ExcelApp.Worksh eets("Sheet1")

          ExcelSheet.Unpr otect Password:="TheP rotector"

          With ExcelSheet
          .Range("C2").Va lue = "Job ref"
          .Range("D2").Va lue = "Unique Job Ref"
          .Range("F2").Va lue = "Catalogue Description"
          .Range("G2").Va lue = "Product Name"
          End With

          ExcelSheet.Prot ect Password:="TheP rotector"

          Excel.Applicati on.DisplayAlert s = False
          ExcelFile.Save
          ExcelFile.Close True
          ExcelApp.Quit

          Set ExcelSheet = Nothing
          Set ExcelFile = Nothing
          Set ExcelApp = Nothing
          [/CODE]

          Comment

          • Smigger
            New Member
            • Nov 2007
            • 5

            #6
            Yes you absolutely right this is being run in an Access Module.

            I'd already previously tried the code indicated within your response and shown below; however, I am still getting the same runtime error message.

            Code:
            Excel.Application.Activesheet.Unprotect("password")
            
            With ExcelSheet
              
              .Range("C2").Value = rsSmartData("Job ref")
              .Range("D2").Value = rsSmartData("Unique Job Ref")
              .Range("F2").Value = rsSmartData("Catalogue Description")
              .Range("G2").Value = rsSmartData("Product Name")
            
            End With
            
            Excel.Application.Activesheet.Protect("password")
            Below is the sum of the code within the access module I'm using at the moment, as mentioned before, if the protection lines 31 and 42 are removed everthing works as expected......

            Code:
            Sub ControlOutlook()
            
            Dim objOutlook As New Outlook.Application
            Dim objEmail As Outlook.MailItem
            Dim strLtrContent As String
            Dim ExcelApp As New Excel.Application
            Dim ExcelFile As New Excel.Workbook
            Dim ExcelSheet As New Excel.Worksheet
            Dim rsSmartData As New ADODB.Recordset
            
            rsSmartData.ActiveConnection = CurrentProject.Connection
            rsSmartData.Open "SmartData"
            
            'For each record in the Smart Data table send and e-mail
            
            Do While Not rsSmartData.EOF
            
            strLtrContent = "Dear" & " " & rsSmartData("Customer Name") & ":" & vbCrLf & vbCrLf
            strLtrContent = strLtrContent & "You have been selected to take part in our customer satisfaction survey, "
            strLtrContent = strLtrContent & "Would you please take the time to compelete the attached form. "
            strLtrContent = strLtrContent & vbCrLf & vbCrLf & "Please do NOT reply to the sender of this e-mail"
            strLtrContent = strLtrContent & vbCrLf & vbCrLf & "Sincerly," & vbCrLf & vbCrLf
            strLtrContent = strLtrContent & "Distributed Provisioning"
            
            ' Excel call code here
            
            ExcelApp.Visible = True
            Set ExcelFile = ExcelApp.Workbooks.Open("S:\Admin- BF\ESCQ\Questionaire\ECSQ.xls")
            Set ExcelSheet = ExcelApp.Worksheets("Answers")
                    
            Excel.Application.ActiveSheet.Unprotect ("password")
                    
                With ExcelSheet
            
                    .Range("C2").Value = rsSmartData("Order ID")
                    .Range("D2").Value = rsSmartData("Orderline ID")
                    .Range("F2").Value = rsSmartData("Product Description")
                    .Range("G2").Value = rsSmartData("Product Name")
            
                End With
             
            Excel.Application.ActiveSheet.Protect ("password")
             
            Excel.Application.DisplayAlerts = False
            ExcelFile.Save
            ExcelFile.Close True
            ExcelApp.Quit
             
            Set ExcelSheet = Nothing
            Set ExcelFile = Nothing
            Set ExcelApp = Nothing
            
            ' Excel call code here
            
            Set objEmail = objOutlook.CreateItem(olMailItem)
            objEmail.Recipients.Add rsSmartData("Recipient ID")
            objEmail.Subject = "Order IT Customer Satisfaction Survey. "
            objEmail.Body = strLtrContent
            objEmail.Attachments.Add ("S:\Admin- BF\ESCQ\Questionaire\ECSQ.xls")
            
            objEmail.Send
            
            rsSmartData.MoveNext
            
            Loop
            
            End Sub

            Comment

            • Smigger
              New Member
              • Nov 2007
              • 5

              #7
              All resolved now, many thanks for your asistances.

              The problem was an oversite on my part, I see now that the protection code should have read as below, without the Activesheet scripting

              Code:
                  With ExcelSheet
              
                      .Unprotect ("password")
                      .Range("C2").Value = rsSmartData("Order ID")
                      .Range("D2").Value = rsSmartData("Orderline ID")
                      .Range("F2").Value = rsSmartData("Product Description")
                      .Range("G2").Value = rsSmartData("Product Name")
                      .Protect ("password")
              
                  End With

              Comment

              Working...