Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

calling excel files from visual basic

Posted on 2002-06-30
13
Medium Priority
?
356 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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 200 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

647 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