Home All Groups Group Topic Archive Search About

Accessing an open workbook from a VB app

Author
27 Jul 2005 5:36 PM
Jules Comeau
I am using a VB app to open a workbook called "DP_RBF" that includes a
worksheet called "Basic".

'Start the excel COM and make it visible
Set objExcel = GetObject("", "excel.application")
    objExcel.Visible = False

'Start a workbook.
Set objWorkbook = objExcel.Workbooks.Add
objWorkbook.SaveAs "DP_RBF.xls"

'Turn off the alerts, otherwise user will have to confirm my actions.
    objExcel.DisplayAlerts = False

'Ensure there is only one worksheet.
Do While objWorkbook.Worksheets.Count > 1
    Set objWorksheet =
objWorkbook.Worksheets.Item(objWorkbook.Worksheets.Count)
    objWorksheet.Delete
Loop

'Rename the remaining sheet as "Results"

Set objWorksheet = objWorkbook.Worksheets.Item("Sheet1")
objWorksheet.Name = "Basic"


Now to my problem.  With a different VB app, I want to read some of the
information from the worksheet "Basic" into variables in my VB app and do
some calcs with them.  I can't seem to figure out how to get a handle on the
worksheet from my second VB app.  I know it's something really silly but I
can't find my problem.

Anyone want to give me a hand.

Thanks.

Jules

--
PhD student
Industrial Engineering

Author
27 Jul 2005 6:26 PM
Bob Butler
"Jules Comeau" <JulesCom***@discussions.microsoft.com> wrote in message
news:582CE8A6-B8B7-4386-B26E-CB9240B020AE@microsoft.com
> I am using a VB app to open a workbook called "DP_RBF" that includes a
> worksheet called "Basic".
>
> 'Start the excel COM and make it visible
> Set objExcel = GetObject("", "excel.application")
>     objExcel.Visible = False
>
> 'Start a workbook.


objExcel.SheetsInNewWorkBook = 1


> Set objWorkbook = objExcel.Workbooks.Add
> objWorkbook.SaveAs "DP_RBF.xls"

<cut out the code to delete extra worksheets>

> 'Rename the remaining sheet as "Results"
>
> Set objWorksheet = objWorkbook.Worksheets.Item("Sheet1")
> objWorksheet.Name = "Basic"
>
>
> Now to my problem.  With a different VB app, I want to read some of
> the information from the worksheet "Basic" into variables in my VB
> app and do some calcs with them.  I can't seem to figure out how to
> get a handle on the worksheet from my second VB app.  I know it's
> something really silly but I can't find my problem.

Have you saved and closed the workbook from the first app?  If so then you
can open the workbook from the second app.  If not do you mean that you want
2 apps to share the same workbook and worksheet at the same time?  If so
then I'd look for some other option as you are going to have a *very*
difficult time ensuring that you actually have the same worksheet open,
specially if multiple Excel instances are running.

--
Reply to the group so all can participate
VB.Net: "Fool me once..."
Author
27 Jul 2005 6:42 PM
Jules Comeau
Show quote Hide quote
"Bob Butler" wrote:

> "Jules Comeau" <JulesCom***@discussions.microsoft.com> wrote in message
> news:582CE8A6-B8B7-4386-B26E-CB9240B020AE@microsoft.com
> > I am using a VB app to open a workbook called "DP_RBF" that includes a
> > worksheet called "Basic".
> >
> > 'Start the excel COM and make it visible
> > Set objExcel = GetObject("", "excel.application")
> >     objExcel.Visible = False
> >
> > 'Start a workbook.
>
>
> objExcel.SheetsInNewWorkBook = 1

thanks this is much simpler

Show quoteHide quote
> > Set objWorkbook = objExcel.Workbooks.Add
> > objWorkbook.SaveAs "DP_RBF.xls"
>
> <cut out the code to delete extra worksheets>
>
> > 'Rename the remaining sheet as "Results"
> >
> > Set objWorksheet = objWorkbook.Worksheets.Item("Sheet1")
> > objWorksheet.Name = "Basic"
> >
> >
> > Now to my problem.  With a different VB app, I want to read some of
> > the information from the worksheet "Basic" into variables in my VB
> > app and do some calcs with them.  I can't seem to figure out how to
> > get a handle on the worksheet from my second VB app.  I know it's
> > something really silly but I can't find my problem.
>
> Have you saved and closed the workbook from the first app?  If so then you
> can open the workbook from the second app.  If not do you mean that you want
> 2 apps to share the same workbook and worksheet at the same time?  If so
> then I'd look for some other option as you are going to have a *very*
> difficult time ensuring that you actually have the same worksheet open,
> specially if multiple Excel instances are running.

I guess I wasn't really clear here.  I have a VB app that has two command
buttons on the form (not a separate app).  One of the buttons opens the
spreadsheet and populates it with info which the user can change if they
wish.  The second button on the form should read that user info and use it to
do some cals.  Are you saying that it would be easier just to close the Excel
app the first time while saving the file and reopen it again when I need the
info in the worksheet?

Thanks for your help!!

Jules

Show quoteHide quote
> --
> Reply to the group so all can participate
> VB.Net: "Fool me once..."
>
>