Solved

calling excel files from visual basic

Posted on 2002-06-30
13
349 Views
Last Modified: 2013-11-25
i have about 25 master excel files, which i want to call from a single vb form. also whenever , i call one of these files, only a copy of that files should be activated, so that even if i make changes, it will not affect the master file. after doing some changes, i want to save the modified file as a different name.

0
Comment
Question by:kamal3965
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 7121054
Hi kamal3965,

Save all your excel master files as excel templates
then let your program load them all up in a combo or listbox
and then use a bit of code like

Private Sub Command1 Click()

  Dim xlApp As Excel.Application
  Dim xlBook As Excel.Workbook
  Dim xlSheet As Excel.Worksheet
 
  Set xlApp = CreateObject("Excel.Application")
  Set xlBook = xlApp.Workbooks.Open(App.Path & "\my.xlt")
  Dim i As Integer
  For i = 1 To 10
    xlBook.Worksheets("My Sheet").Copy  
          before:=xlBook.Worksheets("My Sheet")
    Set xlSheet = xlBook.ActiveSheet
    xlSheet.Range("A1").Offset(1, 0).Value = i
  Next i
 
  xlApp.Application.DisplayAlerts = False
  xlBook.Worksheets("My Sheet").Activate
  xlApp.ActiveWindow.SelectedSheets.Delete
 
  xlBook.Worksheets.Add before:=xlBook.Worksheets("My Sheet (2)")
 
  xlBook.SaveAs FileName:=App.Path & "\my.xls", FileFormat  
            :=xlNormal, Password:="", WriteResPassword:="",  
            ReadOnlyRecommended:=False, CreateBackup:=False
           
  xlBook.Close
  xlApp.Close
 
  xlApp.Quit
  Set xlApp = Nothing
  Set xlBook = Nothing
  Set xlSheet = Nothing

End Sub

HAGD:O)Bruintje
0
 

Author Comment

by:kamal3965
ID: 7121133
as i am new to visual basic how do i let my program load excel templates in a combo or listbox

0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7122498
If you have all your 25 files in one folder, Dir$ function would populate your combo/list box in an easy way.

dim ffile as string
const MYFOLDER as string="c:\mytemplates\"

ffile=dir$(MYFOLDER & "*.xlt",vbarchive) ' or *.xls
do while ffile<>""
   combox1.additem MYFOLDER & ffile
   ffile=dir$()
loop
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7122502
And instead of .Open use .Add method. This will ensure that you open a copy not "real" one.
0
 

Author Comment

by:kamal3965
ID: 7145347
how do you execute a excel file, which i am selecting a list/combo box.

i.e. i should execute only that excel file which i am selecting in list box/combo box


0
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 50 total points
ID: 7146017
As bruintje already told you but instead of .Open, use .Add method.

Dim xlApp As Excel.Application
 Dim xlBook As Excel.Workbook
 Dim xlSheet As Excel.Worksheet
 
 Set xlApp = CreateObject("Excel.Application")
 Set xlBook = xlApp.Workbooks.Add(combobox1.text)
...
....
....
'more code
......
....
0
 

Author Comment

by:kamal3965
ID: 7159123
Thanks everyone who have responded to my query
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7159382
That's fine. did you get all working OK?
0
 

Author Comment

by:kamal3965
ID: 7161254
actually i am not able to open the excel files when i double click an excel file in a list box.
0
 
LVL 44

Expert Comment

by:bruintje
ID: 7170233
what did you put in the eventhandler on the double click?
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7170255
As Bruintje right pointed, don't forget to add the full path to items in combo as i already told you (see my code above)
...
...
...
ffile=dir$(MYFOLDER & "*.xlt",vbarchive) ' or *.xls
...
...
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7970943
Hi kamal3965,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Split points between: bruintje and Richie_Simonetti

kamal3965, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 

Expert Comment

by:SpideyMod
ID: 8019468
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange

bruintje points for you at:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20529302.html
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dialogbox API leak? 18 96
VB6 - Compare and highlight cell not the same 3 51
Please explain "Multi-Tenant Services" 5 86
Need a complete list of ASP.NET page load events 3 46
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question