the folder condition is necessary in Outloook VBS

Status
Not open for further replies.

Simpler45

New Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
help to solve a problem:
the condition if there is no folder ("Globality") it is necessary is created if there is a folder ("Globality") it is removed and again is created

Dim objXls
Dim i, j
Dim myNameSpace
Dim myFolder, myWorkFolder
Dim myOutlook
Dim myItems
Dim myNewFolder
Set objXls = CreateObject("Excel.Application")
objXls.Workbooks.Open "C:\Data.xls"
'укажите путь и имя существующего файла
objXls.Application.Visible = False
Set myOutlook = CreateObject("Outlook.Application")
Set myNameSpace = myOutlook.GetNamespace("MAPI")
Set myFolder = myNamespace.GetDefaultFolder(10)
Set myNewFolder = myFolder.Folders.Add("Globality")
j = objXls.ActiveSheet.UsedRange.Rows.Count
For i = 1 To j
Set myItems = myNewFolder.Items.Add("IPM.Contact")
With myItems
.FullName = objXls.ActiveSheet.Range("A" & i).Value & " " & _
objXls.ActiveSheet.Range("B" & i).Value & " " & _
objXls.ActiveSheet.Range("C" & i).Value
.Birthday = objXls.ActiveSheet.Range("D" & i).Value
.Email1Address = objXls.ActiveSheet.Range("E" & i).Value
.Save
End With
Next
objXls.quit
Set objXls = Nothing
Set myOutlook = Nothing
Set myNameSpace = Nothing
Set myFolder = Nothing
Set myNewFolder = Nothing
 
instead of
Set myNewFolder = myFolder.Folders.Add("Globality")

Try this - use on error resume next to keep the macro moving and set the folder. If it errors, create the folder. if you want it deleted and added again, you'll use the same method - attempt to delete it (myNewFolder.Delete) and if it errors, keep moving and create it. (or, check for and delete the contents of the folder after you check to see if it exists.)

On Error Resume Next
Set myNewFolder = myFolder.Folders("Globality")
If myNewFolder Is Nothing Then
Set myNewFolder = myFolder.Folders.Add("Globality")
End If
 
thank you very much all turned
help still it is necessary that each time at script start, the folder ("Globality") was cleared of contacts, and contacts from document.xls were added then
 
you can either use myNewFolder.delete right after you first set it then instead of checking for it 'if mynewfolder is nothing' , jump right to Adding it.

or loop through the folder and delete the contents - something like this
Set myNewFolder = myFolder.Folders("Globality")
for i = myNewFolder.Items.Count to 1 step -1
myNewFolder.Items(i).Delete
next
 
help there was one more question.
at creation of the folder ("Birthday") to add in the folder description - date of change of the Birthday.xls file, At the subsequent script performance if date of the description of the folder ("Birthday") in Outlook and date of change of the Birthday.xls file - different, is created the new folder with the description of date of change of the new Birthday.xls file if they identical that anything doesn't occur.

Dim objXls
Dim i, j
Dim myNameSpace
Dim myFolder, myWorkFolder
Dim myOutlook
Dim myItems
Dim myNewFolder
Set objXls = CreateObject("Excel.Application")
objXls.Workbooks.Open "\\SRVDC0802\Install\birthday.xls"
'укажите путь и имя существующего файла
objXls.Application.Visible = False
Set myOutlook = CreateObject("Outlook.Application")
Set myNameSpace = myOutlook.GetNamespace("MAPI")
Set myFolder = myNamespace.GetDefaultFolder(10)
On Error Resume Next
Set myNewFolder = myFolder.Folders("Birthday")
If myNewFolder Is Nothing Then
Set myNewFolder = myFolder.Folders.Add("Birthday")
End If
Set myNewFolder = myFolder.Folders("Birthday")
for i = myNewFolder.Items.Count to 1 step -1
myNewFolder.Items(i).Delete
next
j = objXls.ActiveSheet.UsedRange.Rows.Count
For i = 1 To j
Set myItems = myNewFolder.Items.Add("IPM.Contact")
With myItems
.FullName = objXls.ActiveSheet.Range("A" & i).Value & " " & _
objXls.ActiveSheet.Range("B" & i).Value & " " & _
objXls.ActiveSheet.Range("C" & i).Value
.Birthday = objXls.ActiveSheet.Range("D" & i).Value
.Email1Address = objXls.ActiveSheet.Range("E" & i).Value
.Save
End With
Next
objXls.quit
Set objXls = Nothing
Set myOutlook = Nothing
Set myNameSpace = Nothing
Set myFolder = Nothing
Set myNewFolder = Nothing
 
The folder description field should be .Description

Set myNewFolder = myFolder.Folders.Add("Birthday")
myNewFolder.Description = "The Description"
 
Create file found property, as now you can link with "The Description"

Function ShowFileInfo(filespec)
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
ShowFileInfo = "Created: " & f.DateCreated
End Function
 
Oh, yeah, you need to use the FSO - I missed the part where you wanted to use the date of the XLS sheet as the description.
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
M Search message, then (1) Jump to folder & (2) Select message that you searched for Outlook VBA and Custom Forms 2
G Search Folders and Jump to Folder Outlook VBA and Custom Forms 2
X Run macro automatically when a mail appears in the sent folder Using Outlook 5
P Can't add custom field to custom Outlook form, it always adds to the Folder instead Outlook VBA and Custom Forms 2
P Yahoo/IMAP folder rename by Outlook desktop 365 Using Outlook 0
A Outlook 2019 folder counter Using Outlook 0
A Search folder and move the email Outlook VBA and Custom Forms 0
N Reply to Outlook messages by moving messages to a specific Outlook folder Outlook VBA and Custom Forms 1
T How to find or display the sub-folder name for an Archive Search Using Outlook 10
A Outlook 365 (OutLook For Mac)Move "On My Computer" Folder Items From Old To New Mac Computer Using Outlook 3
P Search folder: all emails sent to or from a domain Using Outlook 1
V Folder Properties - Gmail account can't switch Using Outlook 5
Victor_50 Outlook 2019 Jump to folder from search folder Outlook VBA and Custom Forms 0
HarvMan Outlook 365 - Rule to Move an Incoming Message to Another Folder Using Outlook 4
S Email Macros to go to a SHARED Outlook mailbox Draft folder...NOT my personal Outlook Draft folder Using Outlook 2
I Help with Smart Folder + Query Builder on IMAP Using Outlook 0
S Paperclip icon shows without attachment in email under Sent folder Using Outlook 0
Kika Melo Outlook Calendar deleted appointments not in Deleted Items folder Using Outlook 3
K vba code to auto download email into a specific folder in local hard disk as and when any new email arrives in Inbox/subfolder Outlook VBA and Custom Forms 0
Z Outlook 365 Automatically assign categories to incoming mail in a shared folder Round Robin Outlook VBA and Custom Forms 1
G Adding a contact to a specific folder Using Outlook 0
Witzker Outlook 2019 Macro GoTo user defined search folder Outlook VBA and Custom Forms 6
Rupert Dragwater Duplicate email in Folder Using Outlook 7
S Adding a recipient's column to Sent folder in Outlook 2010 Outlook VBA and Custom Forms 1
L "Insert Pictures" Button-Wrong Folder Using Outlook 5
C Outlook 365 Copy/Save Emails in Folder Outside Outlook to Show Date Sender Recipient Subject in Header Using Outlook 0
CWM550 Outlook 365 Hey Diane! MS 365 Biz Standard and "Potential Spam" addressed to others coming to my JUNK folder? Using Outlook 2
J Quick steps delete original email and move reply/sent email to folder Using Outlook 2
A manual rule sends mail to wrong folder Using Outlook 5
richardwing Auto forward email that is moves into a specific outlook folder Outlook VBA and Custom Forms 5
D This folder up to date vs all folders up to date Using Outlook 1
G Automatically delete messages in the synchronization folder Outlook VBA and Custom Forms 3
wayneame Changing the Form Used by Existing Task Items in a Folder Outlook VBA and Custom Forms 4
S Need code to allow defined starting folder and selection from there to drill down Outlook VBA and Custom Forms 10
P Emails assigned with a certain category (within a shared inbox) to be copied to a specific folder. Outlook VBA and Custom Forms 2
M Saving emails using Visual Basic - Selecting folder with msoFileDialogFolderPicker Outlook VBA and Custom Forms 6
B Search and Find Email by Folder Name Outlook VBA and Custom Forms 2
S Folder Pane Colour Categories Using Outlook 6
Victor.Ayala Automated way to check the option "Show this folder as an email Address Book" Outlook VBA and Custom Forms 2
NVDon Create new Move To Folder list Outlook VBA and Custom Forms 0
M Extract "Date sent" from emails (saved to folder using drag and drop) Outlook VBA and Custom Forms 1
O Cannot expand the folder. The set of folders cannot be opened. You do not have permission to log on. Using Outlook 1
F Jump to Inbox folder when click on Favorite Using Outlook 8
P Print attachments automatically and move the mail to an existing folder called "Ted" Outlook VBA and Custom Forms 4
lcarpay Stay in the mail folder pane after ctrl-1 Using Outlook 1
T Macro to move reply and original message to folder Outlook VBA and Custom Forms 6
F VBA to move email from Non Default folder to Sub folders as per details given in excel file Outlook VBA and Custom Forms 11
bhamberg Shortcuts in Folder Pane (Outlook 2016) Using Outlook 19
G VBA to save selected Outlook msg with new name in selected network Windows folder Outlook VBA and Custom Forms 1
P Posts in Folder No Longer Group by Conversation Column After Search Using Outlook 0

Similar threads

Back
Top