So we finished our budget for 74 departments. And I m the one who is sending individually to all. I had a vba code to send these to different people. I have been using vba code to create emails for all of the recipients. This time there was a standard word file which I was supposed to send as text of the email. The code is given below.
This time the code gave me a tough time. It kept on crashing whenever I tried to create emails. The irony is that I still sent all emails through the code. What I did was that I pressed F8 (executed code line by line) and it worked okay. But if I run the entire code with the execute button it always got stuck.
My question is …. Can someone advise why excel does that. It didn’t execute full code, but if I went with F8, line by line, the code worked fine.
I m not an expert, but my diagnosis is that the Word file was a little heavy (in terms of processing). When I was executing code line by line I could see that the steps where I had to open the word file, copy its contents and then paste them as body of email, that step took a little time. Around 4 to 5 seconds each time.
Even while executing line by line I got error sometime. But then I stopped the code and ran it again and it worked fine. But line by line.
Previously I used to create 74 emails with one click, but this time I had to execute line by line which took me more than an hour just creating emails and sending.
Any comments ???
Code:
Sub TempFile_bulkemail()
Dim wd As Object, editor As Object
Dim doc As Object
Dim olApp As Outlook.Application
Dim olMail As Outlook.MailItem
For i = 21 To 30
Set olApp = New Outlook.Application
Set olMail = olApp.CreateItem(olMailItem)
Set wd = CreateObject("Word.Application")
Set doc = wd.documents.Open("C:\Users\201799\Desktop\Experiment Folder\Budget Templates for 2018-19\Budget e-Mail Message.docx")
doc.Content.Copy
doc.Close
Set wd = Nothing
With olMail
.To = Cells(i, 1)
.CC = Cells(i, 2)
.Subject = Cells(i, 3)
.BodyFormat = olFormatHTML
Set editor = .GetInspector.WordEditor
editor.Content.Paste
.Attachments.Add ("C:\Users\201799\Desktop\Experiment Folder\Budget Templates for 2018-19\Budget templates for circulation\PDFs\" & Cells(i, 5))
.Attachments.Add ("C:\Users\201799\Desktop\Experiment Folder\Budget Templates for 2018-19\Budget templates for circulation\" & Cells(i, 4))
.Display
End With
Next
End Sub
This time the code gave me a tough time. It kept on crashing whenever I tried to create emails. The irony is that I still sent all emails through the code. What I did was that I pressed F8 (executed code line by line) and it worked okay. But if I run the entire code with the execute button it always got stuck.
My question is …. Can someone advise why excel does that. It didn’t execute full code, but if I went with F8, line by line, the code worked fine.
I m not an expert, but my diagnosis is that the Word file was a little heavy (in terms of processing). When I was executing code line by line I could see that the steps where I had to open the word file, copy its contents and then paste them as body of email, that step took a little time. Around 4 to 5 seconds each time.
Even while executing line by line I got error sometime. But then I stopped the code and ran it again and it worked fine. But line by line.
Previously I used to create 74 emails with one click, but this time I had to execute line by line which took me more than an hour just creating emails and sending.
Any comments ???