LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Reading excel data into another aplication

Hi,

I want to import a range of data from excel into Microsoft project using
VBA. As I do not have Project at home I am experimenting with reading excel
data into word.

I am using the GetObject to check if Excel is open, if not it errors and I
open it.
I then use the Excel open dialog to select a file
See code below.
Once the code finishes running Excel does not remain open.
Also when Excel is not open the GetObject function in my Public Sub
OOpenExcel does not error out as expected and never runs the " If apExcel Is
Nothing Then"

I was thinking that the macro should ask the user to open the relevant
spreadsheet and select the relevant range.
If there is an easier way to do this I would appreciate some advice.

Any assistance will be appreciated.

Regards.

Sean



' ------------------------------------------------------------
' Purpose: Opens Spreadsheet
' ------------------------------------------------------------
' Author: Sean Bartleet
' Modules used: fnOpenExcel
' fnGetFileName
' Forms Used: None
' Class Modules used: None
' References used (External DLL files)(Tools, references):
' Microsoft Excel 8.0 object library
' Used by modules: For forms, list modules that use this form
' Revision History:
' ------------------------------------------------------------
'
Option Explicit
Public apExcel As Excel.Application

Sub OOpenSpreadsheet()
Dim wbExcel As Excel.Workbook
Dim sErrorMessage As String
Dim vPathFileName As Variant
Dim sFileName As String
Dim sPathName As String

Debug.Print "================"
Debug.Print "Start OOpenSpreadsheet sub"

vPathFileName = GGetFileName
Debug.Print "File Name = " & vPathFileName

'Check if this file is open, if not, open it
On Error Resume Next
apExcel.Windows("" & vPathFileName & "").Activate
If Err.Number < 0 Then
Debug.Print "Error activating " & vPathFileName
Err.Clear
Debug.Print "Try to activate " & vPathFileName
Set wbExcel = GetObject(vPathFileName)
End If
' apExcel.Windows("" & sFileName & "").Visible = True
Dim apMSWord As Object
Set apMSWord = GetObject(, "Word.Application")
Debug.Print "End OOpenSpreadsheet sub"
Debug.Print "XXXXXXXXXXXXXXXXXXXXXXXXXX"
End Sub

' ------------------------------------------------------------
' Purpose: Gets a filename using excel file open dialog
' ------------------------------------------------------------
' Author: Sean Bartleet
' Modules used: fnOpenExcel
' Forms Used: None
' Class Modules used: None
' References used (External DLL files)(Tools, references):
' Microsoft Excel 8.0 object library
' Used by OpenSpreadsheet
' ------------------------------------------------------------
'
Function GGetFileName() As String
Debug.Print "GGetFileName Hello"
' Establish an instance of Excel
OOpenExcel
apExcel.Visible = True
GGetFileName = apExcel.GetOpenFilename("Excel files (*.xls), *.xls")
' apExcel.Application.Visible = True
Debug.Print "GGetFileName Goodby"
End Function

' ------------------------------------------------------------
' Purpose: Opens Excel and
' assigns the application (Excel) to an object
' ------------------------------------------------------------
' Author: Sean Bartleet
' Modules used: None
' Forms Used: None
' Class Modules used: None
' References used (External DLL files)(Tools, references):
' Microsoft Excel 8.0 object library
' Used by modules: OpenSpreadsheet
' ------------------------------------------------------------
'
'Public apExcel As Excel.Application
Public Sub OOpenExcel()
Dim ExcelWasRunning As Boolean
Debug.Print "OpenExcel Hello"
' Try assign Excel to an object, if it fails, Excel is not open,
therefore open.
' If it succeeds then you have an Excel object which you can work with.
On Error Resume Next
Set apExcel = GetObject(, "Excel.Application")
If apExcel Is Nothing Then
Debug.Print "apExcel Is Nothing"
Err.Clear
ExcelWasRunning = False
Set apExcel = GetObject("", "Excel.Application")
apExcel.Application.Visible = True
Debug.Print "Excel not running an instance was created "
If Err.Number < 0 Then
Err.Clear
MsgBox "Error: Could not establish a connection to Excel. " _
& Err.Description
Set apExcel = Nothing
Exit Sub
End If
Else
ExcelWasRunning = True
Debug.Print "Excel was running"
End If
Debug.Print "OpenExcel Godbye"
End Sub


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading data arrays from multiple data files in excel Hankjam[_2_] Excel Discussion (Misc queries) 0 February 7th 08 08:29 PM
Reading data in onther Excel document Ernest Excel Worksheet Functions 1 August 24th 05 01:34 PM
How do I open an aplication and run a file in that aplication use. Cozy Excel Programming 1 March 28th 05 10:41 AM
MS Excel data reading in MS Access Paul Excel Discussion (Misc queries) 1 January 27th 05 11:58 AM
reading data from com port into excel spreadsheet Neil K Excel Programming 2 September 25th 04 06:36 PM


All times are GMT +1. The time now is 12:07 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"