borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Errors with text files)Next Topic (smoothening of Font) New Topic New Poll Post Reply
AndreaVB Forum : VB General : VB6: Open Excel
Poster Message
Longbow2000
Level: Graduate

Registered: 11-01-2005
Posts: 9

icon VB6: Open Excel


1) How do you open an Excel file which is password protected, from VB6. The password is known.

2) Can you copy Excel Macro Code to say (a VB6 CommandButton) and run the code from VB6? The results from the “macro” will be entered into the opened Excel file.

Some code or examples will be highly appreciated.

Thanx in advance.

11-01-2005 at 08:35 AM
View Profile Send Email to User Show All Posts | Quote Reply
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1156
icon Re: VB6: Open Excel

Hi

Here the code for a password protected workbook


Private Sub Command1_Click()

    Dim xlapp As Excel.Application
    
    Set xlapp = New Excel.Application
    
    
    xlapp.Workbooks.Open "c:book1.xls", Password:="password"
    xlapp.Visible = True

End Sub

if you have password protected modifications then use

xlapp.Workbooks.Open "c:book1.xls", Password:="password", WriteResPassword:="password"


With your second option can you can convert the macro code to run in the vb environment with a few tweaks.

Here's a simple macro to put 1 in a1 2 in b1 and sum them in c1

    Range("A1").Select
    ActiveCell.FormulaR1C1 = "1"
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "2"
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "=(RC[-2]+RC[-1])"


and here is a vb version

    Dim xlsheet As Excel.Worksheet
    
    Set xlsheet = xlapp.ActiveSheet
    
    With xlsheet
    
        .Cells(1, 1) = "1"
        .Cells(1, 2) = "2"
        .Cells(1, 3) = .Cells(1, 1) + .Cells(1, 2)
        
    End With


Steve


[Edited by steve_w on 12-01-2005 at 05:30 PM GMT]

12-01-2005 at 05:28 PM
View Profile Send Email to User Show All Posts | Quote Reply
Longbow2000
Level: Graduate

Registered: 11-01-2005
Posts: 9
icon Re: VB6: Open Excel

Thanx, thats 100%

13-01-2005 at 06:15 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB General : VB6: Open Excel
Previous Topic (Errors with text files)Next Topic (smoothening of Font) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder