Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Does anyone know the code for launching Excel from within Access ? 2

Status
Not open for further replies.

DaleWatson123321

Programmer
Jun 12, 2001
1,788
0
0
CA
I have developed code in Access which exports data to an Excel file, but I would like to be able to add a line of code which will LAUNCH the Excel file (from within Access), and then return to Access once the Excel "Workbook Open" code has completed its task.

Does anyone know the Command for this ...launching Excel (and a specific file) from within Access ???

Your help would be very much appreciated. Thanks !!!

...Dale Watson dwatson@bsi.gov.mb.ca
 
Dale

Here's the code I use for launching Word. I guess if you replace Word with Excel throughout that should do the trick.

On Error GoTo Merge_Err
Dim objWord As Object
Set objWord = GetObject("Word.Application")

****
Do things here. You'll have to find out how to give commands to Excel. Here's an example where I start a new document based on a template and put things into bookmarked places in a Word document:
****

With objWord
.Visible = True
.Documents.Add ("Confirmation letter.dot")
.ActiveDocument.Bookmarks("Fname1").Select
.selection.Text = (CStr(Me!txtContactFname))
End With

Set objWord = Nothing
AppActivate "Microsoft Word"

Merge_Err:
If Err.Number = 429 Then
Set objWord = CreateObject("Word.Application")
Resume Next
End If

The error (429) means that Word isn't open, so the CreateObject method conjures up a new instance of Word.
I imagine that you would want .Documents.Open rather than Add to open an existing file.

Hope that's helpful.

Regards

Paul
 
One more thing, Dale, on the password front. I was right about the InputBox - there's no way of obscuring the password.

If this is important, create a pop-up form with a text box on it for the password and set its InputMask property to Password. Then anything typed will appear as asterisks.

Regards

Paul
 
Dale,
I had the exact same question some time ago and found the code below which is almost verbatim. I'm afraid I cannot remember the source, but it may have been in Microsoft's own help manual. qQuery and gFileName are variables I set in another part of my code.

------------------------------------------------
Function Excel_Output_BuildOutput()
On Error GoTo Excel_Output_BuildOutput_Err

With CodeContextObject
DoCmd.OutputTo acQuery, gQuery, "MicrosoftExcel(*.xls)", gFileName, False
DoCmd.Beep
MsgBox "Excel Output Complete", vbInformation, "Finished"
End With


Excel_Output_BuildOutput_Exit:
Exit Function

Excel_Output_BuildOutput_Err:
MsgBox Error$
Resume Excel_Output_BuildOutput_Exit

End Function
-----------------------------------------------
Function 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. Note the comma used as the first argument
' placeholder.
Set MyXL = GetObject(, "Excel.Application")
If Err.Number <> 0 Then ExcelWasNotRunning = True
Err.Clear ' Clear Err object in case error occurred.

' Check for Excel. If 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(gFileName)

' 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

' If Microsoft Excel was not already running when you started,
' close it using the Application property's Quit method.
' When you try to quit Excel, the Excel title bar blinks and
' Excel displays a message 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 Function
------------------------------------------------
Function 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(&quot;XLMAIN&quot;, 0)
If hWnd = 0 Then ' 0 means Excel not running.
Exit Function
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 Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top