Click here to Skip to main content
15,901,205 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: Run-time error Pin
DaveAuld16-May-10 23:55
professionalDaveAuld16-May-10 23:55 
GeneralRe: Run-time error Pin
MsmVc16-May-10 23:57
MsmVc16-May-10 23:57 
GeneralRe: Run-time error Pin
DaveAuld16-May-10 23:59
professionalDaveAuld16-May-10 23:59 
GeneralRe: Run-time error Pin
MsmVc17-May-10 0:10
MsmVc17-May-10 0:10 
GeneralRe: Run-time error [modified] Pin
DaveAuld17-May-10 0:43
professionalDaveAuld17-May-10 0:43 
GeneralRe: Run-time error Pin
MsmVc18-May-10 22:15
MsmVc18-May-10 22:15 
GeneralRe: Run-time error Pin
DaveAuld18-May-10 22:54
professionalDaveAuld18-May-10 22:54 
AnswerRe: Run-time error Pin
Sonhospa16-May-10 23:57
Sonhospa16-May-10 23:57 
What environment are you using? If VB.NET, then you should have a lot of compiler errors anyway...

- You declare a workbook named "xlwbook" and refer to it as "xlbook"
- Declare your i integer inside the procedure
- You declared xl twice (7 instances of Excel running!). If you Dim xl as New Excel.Application that's enough. You shouldn't create a new instance of the Excel object every time your loop runs.
- In .NET you wouldn't need a conversion class. ExcelNword = CStr(i) is enough.
- You should also rather use good old Try... Catch instead of OnError

I gave it a test in VB.NET with then enclosed changed code. Good luck Thumbs Up | :thumbsup: with your further experiments Cool | :cool:

Michael



Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1
    Dim xl As New Excel.Application
    Dim xlwbook As Excel.Workbook

    Dim ExcelFile As String
    Dim ExcelPassword As String
    Dim ExcelNword As String

    Private Sub btnStart_Click() Handles btnStart.Click

        xl.Visible = False
        ExcelFile = "D:\" + "text.xls"

        For i = 1 To 6
            Try
                ExcelNword = CStr(i)
                ExcelPassword = "ABC" + ExcelNword
                xlwbook = xl.Workbooks.Open(Filename:=ExcelFile, Password:=ExcelPassword)
                MsgBox(String.Format("Workbook could be opened with password {0}", ExcelPassword))

            Catch ex As Exception
                If Err.Number = 1004 Then
                    MsgBox(String.Format("'{0}' is the wrong password!", ExcelPassword), , "Error")
                Else
                    MsgBox(String.Format("Fehler {0}: {1}", Err.Number.ToString, Err.Description), , "Error")
                End If
            End Try
        Next i

        xl.Quit()
        xlwbook = Nothing
        xl = Nothing

    End Sub
End Class


modified on Monday, May 17, 2010 7:06 AM

AnswerRe: Run-time error Pin
William Winner17-May-10 8:10
William Winner17-May-10 8:10 
GeneralRe: Run-time error Pin
DaveAuld18-May-10 22:54
professionalDaveAuld18-May-10 22:54 
GeneralRe: Run-time error Pin
William Winner19-May-10 4:45
William Winner19-May-10 4:45 
GeneralRe: Run-time error Pin
DaveAuld19-May-10 4:48
professionalDaveAuld19-May-10 4:48 
QuestionVB.Net Firebird ADO Pin
FrankPacher16-May-10 12:23
FrankPacher16-May-10 12:23 
AnswerRe: VB.Net Firebird ADO Pin
Dalek Dave16-May-10 13:07
professionalDalek Dave16-May-10 13:07 
AnswerRe: VB.Net Firebird ADO Pin
Simon_Whale16-May-10 22:13
Simon_Whale16-May-10 22:13 
Questionhow to find text in excel with vb.net? Pin
sanyexian15-May-10 21:42
sanyexian15-May-10 21:42 
AnswerRe: how to find text in excel with vb.net? Pin
dan!sh 16-May-10 0:24
professional dan!sh 16-May-10 0:24 
GeneralRe: how to find text in excel with vb.net? Pin
Dalek Dave16-May-10 1:42
professionalDalek Dave16-May-10 1:42 
Questionshape drawing and coordinations Pin
tuolesi15-May-10 5:12
tuolesi15-May-10 5:12 
AnswerRe: shape drawing and coordinations Pin
DaveAuld15-May-10 5:31
professionalDaveAuld15-May-10 5:31 
GeneralRe: shape drawing and coordinations Pin
tuolesi15-May-10 6:37
tuolesi15-May-10 6:37 
GeneralRe: shape drawing and coordinations Pin
DaveAuld15-May-10 6:42
professionalDaveAuld15-May-10 6:42 
GeneralRe: shape drawing and coordinations Pin
tuolesi15-May-10 7:03
tuolesi15-May-10 7:03 
GeneralRe: shape drawing and coordinations Pin
tuolesi17-May-10 5:25
tuolesi17-May-10 5:25 
AnswerRe: shape drawing and coordinations Pin
Luc Pattyn15-May-10 6:39
sitebuilderLuc Pattyn15-May-10 6:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.