Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / February 2006

Tip: Looking for answers? Try searching our database.

Excel Automation?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ThunderTek - 26 Jan 2006 13:15 GMT
Here is my code line:
Dim objXL As Object
Set objXL = CreateObject("Excel.Application")
objXL.Workbooks.Open (objXL.LibraryPath & "\AtData.XLA") '.RunAutoMacros 1
objXL.Workbooks.Open "C:\AlkyLabData.xls"
objXL.Visible = False

DoCmd.TransferSpreadsheet , , "TestingImportData2", "C:\AlkyLabData.xls", True

Me.Requery
DoCmd.GoToRecord , , acLast
objXL.Quit
Set objXL = Nothing

I am attempting to automate open Excel from Access, but in order for Excel
to do what I need....I have to install an addin.  The problem that I am
getting is that the addin is opening another instance of Excel.  After Excel
is opened and the addin installed, I then need to calculate the data on Excel
and import the data into Access.  I would like this to happen without any
user input.  Is this task even possible?
Klatuu - 26 Jan 2006 14:21 GMT
I am not sure what it is you are trying to do here, but I do see some
problems.  First, I don't know what add in you think you need.  There is no
add id I know of required for using automation with Excel, unless there is
something you are doing that is outside the norm.  You do need a reference to
the Microsoft Excel Object Library.

The other problem I see is you are trying to import a spreadsheet you
already have open for automation.  Your TransferSpreadsheet is creating
another instance of Excel which means you now have two instances open, the
one you created and the one created by the TransferSpreadsheet.

If what you are doing is manipulating C:\AlkyLabData.xls prior to importing
it, you need to close it and quit Excel before you do the TransferSpreadsheet.

> Here is my code line:
> Dim objXL As Object
[quoted text clipped - 16 lines]
> and import the data into Access.  I would like this to happen without any
> user input.  Is this task even possible?
ThunderTek - 26 Jan 2006 17:56 GMT
I tried what you have suggested already.  For the most part...everything
works, but it requires a person to log back off before Excel is actual quit.  
The problem here is this....if I take out the Transferspreadsheet line and do
not do that function.....the code still leaves an instance of Excel running.  
Which then interferes with the Transferspreadsheet command.  Basically, the
object is creating another instance somewhere in the Addin loading line.  If
I don't do the addin line, then when excel opens....it does not receive the
data from a program that imports the data into excel call "Aspen tech".  If
I'm not being clear in my explanation....please let me know.  And thanks for
the help, but problem still not solved.

> I am not sure what it is you are trying to do here, but I do see some
> problems.  First, I don't know what add in you think you need.  There is no
[quoted text clipped - 30 lines]
> > and import the data into Access.  I would like this to happen without any
> > user input.  Is this task even possible?
Klatuu - 26 Jan 2006 18:26 GMT
You have to be very careful with Excel objects and fully qualify each
refernce.  Access will create another instance if it can't figure out which
object another object belongs to.  This will cause problems with Excel
processes still running.  You will see them on the Processes pane of Task
Manager, not the Application pane.

See if these changes do any good.
Dim objXL As Object
Dim xlBook1 as Object
Dim xlBook2 as Object
Set objXL = CreateObject("Excel.Application")
Set xlBook1 = objXL.Workbooks.Open (objXL.LibraryPath
& "\AtData.XLA") '.RunAutoMacros 1
Set XlBook2 =  objXL.Workbooks.Open "C:\AlkyLabData.xls"
objXL.Visible = False
xlbook1.Close
xlbook2.Close
objXL.Quit
Set objXL = Nothing
Set xlBook1 = Nothing
Set xlBook2 = Nothing

DoCmd.TransferSpreadsheet , , "TestingImportData2", "C:\AlkyLabData.xls", True

Me.Requery
DoCmd.GoToRecord , , acLast

Also, copied from VB Editor Help is some code that makes this pretty
straightforward.  Copy this into a standard module.  I call mine
modExcelRoutines

Option Compare Database
Option Explicit

' Declare necessary API routines:
Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, _
                   ByVal lpWindowName As Long) As Long

Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
                   ByVal wParam As Long, _
                   ByVal lParam As Long) As Long

Sub GetExcel()
   Dim MyXL As Object    ' Variable to hold reference
                               ' to Microsoft Excel.
   Dim ExcelWasNotRunning As Boolean    ' Flag for final release.

' Test to see if there is a copy of Microsoft Excel already running.
   On Error Resume Next    ' Defer error trapping.
' Getobject function called without the first argument returns a
' reference to an instance of the application. If the application isn't
' running, an error occurs.
   Set MyXL = GetObject(, "Excel.Application")
   If Err.Number <> 0 Then ExcelWasNotRunning = True
   Err.Clear    ' Clear Err object in case error occurred.

' Check for Microsoft Excel. If Microsoft Excel is running,
' enter it into the Running Object table.
   DetectExcel

' Set the object variable to reference the file you want to see.
   Set MyXL = GetObject("c:\vb4\MYTEST.XLS")

' Show Microsoft Excel through its Application property. Then
' show the actual window containing the file using the Windows
' collection of the MyXL object reference.
   MyXL.Application.Visible = True
   MyXL.Parent.Windows(1).Visible = True
'     Do manipulations of your  file here.
   ' ...
' If this copy of Microsoft Excel was not running when you
' started, close it using the Application property's Quit method.
' Note that when you try to quit Microsoft Excel, the
' title bar blinks and a message is displayed asking if you
' want to save any loaded files.
   If ExcelWasNotRunning = True Then
       MyXL.Application.Quit
   End If

   Set MyXL = Nothing    ' Release reference to the
                               ' application and spreadsheet.
End Sub

Sub DetectExcel()
' Procedure dectects a running Excel and registers it.
   Const WM_USER = 1024
   Dim hWnd As Long
' If Excel is running this API call returns its handle.
   hWnd = FindWindow("XLMAIN", 0)
   If hWnd = 0 Then    ' 0 means Excel not running.
       Exit Sub
   Else
   ' Excel is running so use the SendMessage API
   ' function to enter it in the Running Object Table.
       SendMessage hWnd, WM_USER + 18, 0, 0
   End If
End Sub

> I tried what you have suggested already.  For the most part...everything
> works, but it requires a person to log back off before Excel is actual quit.  
[quoted text clipped - 41 lines]
> > > and import the data into Access.  I would like this to happen without any
> > > user input.  Is this task even possible?
ThunderTek - 01 Feb 2006 23:07 GMT
Thank you Klatuu....this is working.  Now, my next problem is that when I do
the TransferSpreadsheet command....it automatically transfers the data from
excel into a New Record in access.  How can I get this code to update the
current record rather than create a new one?

> You have to be very careful with Excel objects and fully qualify each
> refernce.  Access will create another instance if it can't figure out which
[quoted text clipped - 141 lines]
> > > > and import the data into Access.  I would like this to happen without any
> > > > user input.  Is this task even possible?
George Nicholson - 26 Jan 2006 19:20 GMT
In Excel VBA Help, i would strongly recommend looking up the entry for
"Addins Collection" object.

Per the Help entry, the proper syntax to add (and install) an add-in is:

       Addins.Add("generic.dll").Installed = True

Please don't just take the above example and try to run with it without
reading the Help entry first.

HTH,
Signature

George Nicholson

Remove 'Junk' from return address.

>I tried what you have suggested already.  For the most part...everything
> works, but it requires a person to log back off before Excel is actual
[quoted text clipped - 61 lines]
>> > any
>> > user input.  Is this task even possible?
ThunderTek - 03 Feb 2006 15:38 GMT
OK.  I have this thing working now, but when I do the Transfer it
automatically creates a new record.  Anyway to make it update a current
record and not creat a new one?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.