Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

how to open a excel file in vb 6.0

Hi there,

does anyone know how i can handle the following problem?

In form1 I open my standard excel document (standard.XLS), put some new textlines in it and save it under another name (for example: Document1.XLS). So far so good... But in form2 I want to open the just saved file (Document1.XLS) by clicking on a command button , and again edit some textlines (out of textboxes in VB) and save it again as Document1.xls.

The next time I start my program I want to edit standard.XLS again and save it as document2.XLS or something like that open it in form2 etc, etc, etc.

Simply said:

The thing is that I always open the standard document but save it under another name. in form two i want to open the new made document for edit.

Who can help me?

thanks, Soontjes


0
soontjes1
Asked:
soontjes1
1 Solution
 
vinnyd79Commented:
not sure if this is what you are looking for,but shellexecute api will open anyfile with it's default app.

' declarations
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, _
    ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, _
    ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long


Private Sub Command1_Click()
ShellExecute hwnd, "Open", "C:\Somefile.xls", "", App.Path, 1
End Sub
0
 
soontjes1Author Commented:
Well cause i'm not must of a VBer i dont know how to place it in my code.. Do you? Here is a part of the code so far:

in the command button in form 1:

Private Sub Picture4_Click()
 Set xlApp = CreateObject("Excel.Application")
 
  name1 = "c:\documents and settings\Standaard.Xls"
 
 With xlApp
  .Workbooks.Open (name1)
 
  xlApp.Sheets(1).Cells(1, 2).Select
  xlApp.ActiveCell.Value = Text1.Text
 
 o = Val(Project.Text)
 
  xlApp.Sheets(1).Cells(7, 2).Select
  xlApp.ActiveCell.Value = o  
 
  xlApp.Sheets(1).Cells(5, 2).Select
  xlApp.ActiveCell.Value = Text3.Text

 End With
 
  name2 = "c:\documents and settings\VNr_" & Val(o) & ".XLS"
  xlApp.ActiveWorkbook.SaveAs FileName:=name2, FileFormat:=xlNormal, ReadOnlyRecommended:=False
  xlApp.DisplayAlerts = False
  xlApp.ActiveWorkbook.Close
  xlApp.Quit
 Set xlApp = Nothing

Now it is closed an saved under a new name.


In form 2 i want to open that file, but how must i implante your code then??

Thanks a lot!

0
 
SteinerCommented:
I'm not sure if I got your problem right, but why don't you just use a global variable for the document name?
How are form1 and form2 started? I guess they are in the same project and started one after another?
If that's so, create a module with the follwing line:
Public sWbName as String

In the line between "name2 = ..." and "xlApp.ActiveWorkbook.SaveAs" in form1 write
"sWbName=name2"

To open the same Excel file in Form2 use the same approach as you use in Form1, but instead of
name1="C:\documents and settings\standaard.xls"   use:
name1=sWbName
Also you'll have to modify your save routine, since you want to save it under the same name. So just use xlApp.ActiveWorkbook.Save.


So your code for form2 could look like:

Set xlApp = CreateObject("Excel.Application")

 name1 = sWbName

With xlApp
 .Workbooks.Open (name1)

 xlApp.Sheets(1).Cells(1, 2).Select
 xlApp.ActiveCell.Value = Text1.Text
 
o = Val(Project.Text)
 
 xlApp.Sheets(1).Cells(7, 2).Select
 xlApp.ActiveCell.Value = o  

 xlApp.Sheets(1).Cells(5, 2).Select
 xlApp.ActiveCell.Value = Text3.Text

End With

 xlApp.ActiveWorkbook.Save
 xlApp.DisplayAlerts = False
 xlApp.ActiveWorkbook.Close
 xlApp.Quit
Set xlApp = Nothing
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
soontjes1Author Commented:
Thanks, it works great!!!
0
 
SteinerCommented:
So why grade B?
0
 
soontjes1Author Commented:
0
 
SteinerCommented:
If you have a question to my solution, why didn't you post that here????
A grade B looks terrible in my profile...
0
 
archekryptCommented:
open it in a data control or connect to it through a provider, SQL, mySQL or just as a XLS!!!

cool!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now