Broken Folder Loop - Outlook 2007 (.oft files)

walu22

Member
Outlook version
Outlook 2007
Email Account
Hello,

I cobbled together the following code (which is designed to loop through a template folder, open each file, and save it as a draft) from examples online. It's presented below.

Code:
Public Sub CreateFromTemplateLoop()
   Dim MyItem As Outlook.MailItem
   Dim myOlApp As Outlook.Application
   Dim MyFolder As String
   Dim MyFile As String
   MyFolder = "C:\Users\walu22\Documents\I'm Going Nuts Here!"
   MyFile = Dir(MyFolder & "\*.oft")
   Do While MyFile <> ""
   Set myOlApp = CreateObject("Outlook.Application")
   Set MyItem = myOlApp.CreateItemFromTemplate(MyFile, _
           myOlApp.Session.GetDefaultFolder(olFolderDrafts))
   MyItem.Display
   MyItem.Save
   MyItem.Close olPromptForSave
   MyFile = Dir
   Loop 
 
End Sub
It worked perfectly the first time I used it, but ever since I restarted my outlook, it's been yelling at me. Here's the error message it returns:

http://farm8.staticflickr.com/7316/10614702404_c993e9e2d8.jpg

2147287038 (80030002)

Cannot open file. The file may not exist, you may not have permission to open it, or it may be open in another program.

I have no clue what the problem as all the files open fine when doing a single macro. For example, this works okay.

Code:
Public Sub SIMIncomeGrowth() 
 
Set msg = Application.CreateItemFromTemplate("C:\Users\walu22\Documents\I'm Going Nuts Here!\Income1.oft") 
 
msg.Display 
 
End Sub
Any guidance would be appreciated.

Cheers,

walu22
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Hmmm. It get that error immediately.

Ok - to test it, i added debug.print to see what myfile is -
MyFile = Dir(MyFolder & "*.oft")
Debug.Print MyFile
Do While MyFile <> ""



only the file name, not the entire path was printed. When i used the folder path in create from template, it worked:

CreateItemFromTemplate(MyFolder & MyFile,

So... on the restart outlook doesn't know where to look - you need to pass the full path it to.
 

walu22

Member
Outlook version
Outlook 2007
Email Account
Hmmm. It get that error immediately.

Ok - to test it, i added debug.print to see what myfile is -

MyFile = Dir(MyFolder & "*.oft")

Debug.Print MyFile

Do While MyFile <> ""

only the file name, not the entire path was printed. When i used the folder path in create from template, it worked:

CreateItemFromTemplate(MyFolder & MyFile,

So... on the restart outlook doesn't know where to look - you need to pass the full path it to.
Worked like a charm! Thank you much, Diane. :D

Here's the code for those interested:

Code:
Public Sub CreateFromTemplateLoop()
   Dim MyItem As Outlook.MailItem
   Dim myOlApp As Outlook.Application
   Dim MyFolder As String
   Dim MyFile As String
   MyFolder = "C:\Users\walu22\AppData\Roaming\Microsoft\Templates\Individual Statements\"
   MyFile = Dir(MyFolder & "\*.oft")
   Do While MyFile <> ""
   Set myOlApp = CreateObject("Outlook.Application")
   Set MyItem = myOlApp.CreateItemFromTemplate(MyFolder & MyFile, _
           myOlApp.Session.GetDefaultFolder(olFolderDrafts))
   MyItem.Display
   MyItem.Save
   MyItem.Close olPromptForSave
   MyFile = Dir
   Loop 
 
End Sub
 
Top