Solved

calling excel files from visual basic

Posted on 2002-06-30
13
350 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to LOAD up a .NET Framework website with data? 2 59
MsgBox 4 71
Problem to With line 4 76
Automatic Email Reminder 4 76
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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

726 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