|
-
Mar 28th, 2011, 10:44 AM
#1
Thread Starter
New Member
Microsoft Office VB Script Issue
Hi there!
I work for a tour operator and I do basic office work/customer service work. There's this one task where I basically have to scan a form (hundreds of them but each file individually), e-mail them to my e-mail (with each e-mail is about 30 attachments), and then save them as individual .pdf's on my hard drive which then after I run through them and change the name corresponding to the name on the form.
So I wanted to create a macro that automatically saves all 30 or so attachments form each e-mail, and since they are all named something like DC001, I'd like to somehow save it as Form1, Form2, Form3, Form4, Form5, etc and have a counter integer in the macro to continuously save it as a different FormNumber.
The problem is: I'm not too great with Visual Basic (haven't programmed since high school!) and I am quite unfamiliar with this, I have a code that I'm trying to tweak and use at the bottom. For awhile I couldn't get the code to even recognize the attachments, but then finally it saved them all from one e-mail but it was named "Xerox Scanned Document" and it was an outlook file, because you have to double click the attachment to get it as a pdf file. I realize there is an option in Outlook that says "Save all attachments" but like previously it saved them all as one outlook file, here is a picture to kind of show what I mean:


Code:
Sub GetAttachments()
' This Outlook macro checks a the Outlook Inbox for messages
' with attached files (of any type) and saves them to disk.
' NOTE: make sure the specified save folder exists before
' running the macro.
On Error GoTo GetAttachments_err
' Declare variables
Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim SubFolder As MAPIFolder
Dim Item As Object
Dim Atmt As Attachment
Dim FileName As String
Dim i As Integer
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set SubFolder = Inbox.Folders("Parental Consent Forms")
i = 0
' Check Inbox for messages and exit of none found
If SubFolder.Items.Count = 0 Then
MsgBox "There are no messages in the Parental Consent Forms Folder.", vbInformation, _
"Nothing Found"
Exit Sub
End If
' Check each message for attachments
For Each Item In SubFolder.Items
' Save any attachments found
For Each Atmt In Item.Attachments
' This path must exist! Change folder name as necessary.
FileName = "C:\Documents and Settings\npasmore\My Documents\Parental Consent Forms\" & Atmt.FileName
Atmt.SaveAsFile FileName
i = i + 1
Next Atmt
Next Item
' Show summary message
If i > 0 Then
MsgBox "I found " & i & " attached files." _
& vbCrLf & "I have saved them into the C:\Documents and Settings\npasmore\My Documents\Parental Consent Forms." _
& vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
Else
MsgBox "I didn't find any attached files in your mail.", vbInformation, "Finished!"
End If
' Clear memory
GetAttachments_exit:
Set Atmt = Nothing
Set Item = Nothing
Set ns = Nothing
Exit Sub
' Handle errors
GetAttachments_err:
MsgBox "An unexpected error has occurred." _
& vbCrLf & "Please note and report the following information." _
& vbCrLf & "Macro Name: GetAttachments" _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
Resume GetAttachments_exit
End Sub
I would be greatly appreciative if someone could help as this is a long and strenuous task which takes hours but with the right macro - could take maybe half an hour.
-
Mar 28th, 2011, 10:53 AM
#2
Thread Starter
New Member
Re: Microsoft Office VB Script Issue
-
Mar 28th, 2011, 09:26 PM
#3
Re: Microsoft Office VB Script Issue
it appears that the attachment is a single pdf file
to separate the pages, into separate documents, requires some sort of pdf control, to open the pdf file and parse the pages, easy enough to do with a word doc, but not something i have tried with pdf files
search on the net for pdf tools to automate
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Mar 31st, 2011, 08:24 PM
#4
Fanatic Member
Re: Microsoft Office VB Script Issue
I don't think he's trying to combine the documents so much as just trying to save them. The pdfs are loaded in separate message files that are also attachments, so he has to open the email message, open each message attachment, and save the pdf file from there. I will have to check the object model to see the best way to grab them.
-
Mar 31st, 2011, 08:54 PM
#5
Fanatic Member
Re: Microsoft Office VB Script Issue
MS didn't make the outlook model very good. Looks like this may not be possible through straight outlook because the object is so restrictive. You may need to get outlook redemption even as a trial.
-
Apr 1st, 2011, 01:44 AM
#6
Re: Microsoft Office VB Script Issue
I don't think he's trying to combine the documents so much as just trying to save them.
yeah i thought he was getting a single attachment he wanted to save each page separately, but on rereading, i believe you are correct, which is much easier
i have certainly seen code in this forum to save email attachments in outlook using vba, but i can not confirm that it works
check out http://www.outlookcode.com/codedetail.aspx?id=70
a quick google brought many results
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|