Outlook open Excel

cb711

Board Regular
Joined
Dec 18, 2009
Messages
107
I found this code online and it works great. I want it to open an excel file at the end. For some reason it will not try to open. The red text is where my problem is. Any help would be great.

Here is the link to the script http://www.vbaexpress.com/kb/getarticle.php?kb_id=522
Code:
'###############################################################################
 '### Module level Declarations
 'expose the items in the target folder to events
Option Explicit
Dim WithEvents TargetFolderItems As Items
 'set the string constant for the path to save attachments
Const FILE_PATH As String = "Y:\"
 
 '###############################################################################
 '### this is the Application_Startup event code in the ThisOutlookSession module
Private Sub Application_Startup()
     'some startup code to set our "event-sensitive" items collection
    Dim ns As Outlook.NameSpace
     '
    Set ns = Application.GetNamespace("MAPI")
    Set TargetFolderItems = ns.Folders.Item( _
    "Personal Folders").Folders.Item("Temp").Items
 
End Sub
 
 '###############################################################################
 '### this is the ItemAdd event code
Sub TargetFolderItems_ItemAdd(ByVal Item As Object)
     'when a new item is added to our "watched folder" we can process it
    Dim olAtt As Attachment
    Dim i As Integer
 
    If Item.Attachments.Count > 0 Then
        For i = 1 To Item.Attachments.Count
            Set olAtt = Item.Attachments(i)
             'save the attachment
            olAtt.SaveAsFile FILE_PATH & olAtt.FileName
            Item.Delete
        Next
    End If
 
    Set olAtt = Nothing
 
 
End Sub
 '###############################################################################
 '### print routine
[COLOR=red]Sub Openexcel()[/COLOR]
 
 
[COLOR=red] Dim xlApp As Excel.Application[/COLOR]
[COLOR=red] Dim wb As Excel.Workbook[/COLOR]
 
[COLOR=red]  'in the background, create an instance of xl then open, print, quit[/COLOR]
[COLOR=red] Set xlApp = New Excel.Application[/COLOR]
[COLOR=red] Set wb = xlApp.Workbooks.Open("C:\SQL\File.xls")[/COLOR]
[COLOR=red] wb.Visible = True[/COLOR]
 
[COLOR=red] 'xlApp.Quit[/COLOR]
 
[COLOR=red]  'tidy up[/COLOR]
[COLOR=red] 'Set wb = Nothing[/COLOR]
[COLOR=red] 'Set xlApp = Nothing[/COLOR]
 
 
[COLOR=red]End Sub[/COLOR]
 '###############################################################################
 '### this is the Application_Quit event code in the ThisOutlookSession module
Private Sub Application_Quit()
 
    Dim ns As Outlook.NameSpace
    Set TargetFolderItems = Nothing
    Set ns = Nothing
 
 
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
I cannot see that you are actually calling the "Openexcel" routine in the code you have posted.
If you are calling it from somewhere else, try setting a break-point in the code and then step through it.
If it is called and when the end of the code is reached, the file closes try adding the following:
Code:
xlApp.UserControl = True
 
Upvote 0
also, .visible isn't a workbook level property.

replace
Code:
wb.visible = true
with
Code:
xlapp.visible = true
 
Upvote 0
This is pretty interesting, I have no problem opening an excel document using office 97 software. It seems like I have the trouble in outlook 2000. If I create a new mail message and insert an attachment I am unable to view the attachment. I also get an error stating "The system cannot find the file specified"." Excel opens but doesn't display the page or a blank page. There is no problem when I try the same with word. It's kind of weird because we are using terminal server here and this is the only session that I am having problems with. Please let me know if anyone has run into this problem.


 
Upvote 0

Forum statistics

Threads
1,225,531
Messages
6,185,482
Members
453,297
Latest member
alvintranvcu123

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top