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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I open an excel document through a VB form?

Status
Not open for further replies.

PleaseGiveHelp

Programmer
Oct 29, 2002
131
US
I created a form with the following code:

Dim FileToOpen As String

Private Sub Form_Load()

FileToOpen = Application.GetOpenFilename

If FileToOpen = "False" Or FileToOpen = "" Then
MsgBox "Ensure file is chosen correctly"
Else
Workbooks.OpenText FileToOpen
End If

End Sub

BUT IT DOESN"T OPEN MY FILE! Through task manager I see that Excel is open, but alas, no file. What is wrong?
 
PGH,

1. in Tools/References set a reference to the Excel Object Library

2. in your code
[tt]
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(&quot;c:\vb4\MYTEST.XLS&quot;)
[/tt]
see the Help for GetObject to continue

:)


Skip,

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
It looks as if the GetObject can only be used if I know the file name that will be chosen and unfortunately I do not. THats why I wanted to bring up the file manager to have the user pick the file they want to bring up (it will be a .csv file) adn then open THAT file...
 
Ok so this part of my program is now working which is a PLUS. This is my code:

Private Sub Form_Load()

'Dim MyXL As Object ' Variable to hold reference to MS Excel.
Dim MyXL As Excel.Application
Set MyXL = New Excel.Application

On Error Resume Next
Set MyXL = GetObject(, &quot;Excel.Application&quot;)

If Err.Number = 429 Then
Set appExcel = CreateObject(&quot;Excel.Application&quot;)
End If


FileToOpen = Application.GetOpenFilename

If FileToOpen = &quot;False&quot; Or FileToOpen = &quot;&quot; Then
MsgBox &quot;Ensure file is chosen correctly&quot;
Else
Workbooks.Open FileName:=FileToOpen
Application.Visible = True
End If



'Set MyXL = Nothing
End Sub

Finally, Excel opens with the file I've chosen. Now, I need to have my application, upon opening the tile, to create and execute a macro. I have the macro code but I need for the file that is opened to execute it (however the file will not have the macro so I need to have the program create it, if this makes sense). Is this possible?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top