Link to home
Start Free TrialLog in
Avatar of Foffaf Foffaf
Foffaf FoffafFlag for Indonesia

asked on

VB code to Create Excel file from Outlook 2007 part 3

Why is this code only creating VBA folder but nothing inside? How do you fix it?


Note:
This question is related to these question

VB code to Create Excel file from Outlook 2007 part 2 - https://www.experts-exchange.com/questions/24163884/VB-code-to-Create-Excel-file-from-Outlook-2007-part-2.html?anchorAnswerId=23699562#a23699562

VB code to Create Excel file from Outlook 2007 part 1 - https://www.experts-exchange.com/questions/24139919/VB-code-to-Create-Excel-file-from-Outlook-2007.html

Sub CreateExcel2()
    Dim objExcel As Object
    Dim sExcelFilePath As String
    Dim objFSO As Object
    Dim objFolder As Object
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    sExcelFilePath = "e:\temp\VBA\test.xlsx"
    
    On Error Resume Next
    
    Set objFolder = objFSO.GetFolder(objFSO.GetParentFolderName(sExcelFilePath))
    
    On Error Resume Next
    
    If TypeName(objFolder) Then
        Set objFolder = objFSO.CreateFolder(objFSO.GetParentFolderName(sExcelFilePath))
    End If
    
    objExcel.Workbooks.Add
    objExcel.ActiveWorkbook.SaveAs sExcelFilePath
    
    Set objFSO = Nothing
    Set objFolder = Nothing
End Sub

Open in new window

Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

YOu have on error resume next active.

When you create the excel workbook you use objexcel which is undefined therefore there is no workbook to save.

Try as attached

Chris
Sub CreateExcel2()
    Dim objExcel As Object
    Dim sExcelFilePath As String
    Dim objFSO As Object
    Dim objFolder As Object
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    sExcelFilePath = "c:\deleteme\test.xlsx"
    Set objExcel = CreateObject("excel.application")
    
    On Error Resume Next
    
    Set objFolder = objFSO.GetFolder(objFSO.GetParentFolderName(sExcelFilePath))
    
    On Error Resume Next
    
    If TypeName(objFolder) Then
        Set objFolder = objFSO.CreateFolder(objFSO.GetParentFolderName(sExcelFilePath))
    End If
    
    objExcel.Workbooks.Add
    objExcel.ActiveWorkbook.SaveAs sExcelFilePath
    
    Set objExcel = Nothing
    Set objFSO = Nothing
    Set objFolder = Nothing
End Sub

Open in new window

0ffa,

Any update?

chris_bottomley
Avatar of Foffaf Foffaf

ASKER

chris_bottomley,

I tried the code, when I ran it seems like the program stuck for a minute. I check on the path, its correctly created the file for me, but I can't open the file unless I do 'end task' the excel through Task Manager. So it seems the code doesn't exit or close the excel properly.

Sorry for replying this late, please bear with me coz I only can try this programming stuff on my spare time. Thx.
ASKER CERTIFIED SOLUTION
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The code is working Chris :)