You are presumably using Outlook without the option to send files immediately so they are accummulated in the outbox, where you are processing them. The problem with this is that there may be other unrelated e-mail messages pending in the outbox that would screw up your process.
The easiest way to keep the Word window out of the way is to minimize it
wdApp.Application.WindowState = 2
Do this after declaring wdApp immediately before
is exactly the same command as
Dialogs(wdDialogFileOpen)
but the numeric equivalent is used to allow late binding to Word (there is no need to set a reference to Word in Tools > References when using the code I posted).
The probable reason your GetFile function doesn't work for some is that they don't have Word declared as a reference in their Outlook VBA editors. This is why I prefer to use Late Binding to other office apps. It is harder to program with late binding, but it improves compatibility. GetFile is the better option so I have modified it to work from my version of the original code. It doesn't need yet another instance of Word as you already have Word opened, so use that one.
Option Explicit
Sub SetCCandattach()
Dim wdApp As Object
Dim Maillist As Object
Dim Datarange As Object, Datarangecc As Object
Dim i As Long, j As Long
Dim Item As Outlook.MailItem
Dim Attachment As Attachment
Dim olNS As Outlook.NameSpace
Dim MyFolder As Outlook.MAPIFolder
Dim count As Integer
Dim strFileName As String
Dim bStarted As Boolean
Const strPath As String = "C:\Path\" 'The initial filepath for the maillist document
' This sub assumes that this macro is being run from within Outlook
On Error Resume Next
'Set OutlookApp = GetObject(, "Outlook.Application") 'You are already in Outlook?
' Open the mailmerge table document
Set wdApp = GetObject(, "Word.Application") 'Get Word if it is already running
If Err Then
Set wdApp = CreateObject("Word.Application") 'or create it if it isn't
bStarted = True
End If
wdApp.Application.WindowState = 2 'Minimize Word
On Error GoTo 0
strFileName = GetFile(wdApp, strPath) 'Get the maillist filename
If strFileName = "" Then
MsgBox "Process cancelled"
GoTo lbl_Exit 'User cancelled
End If
Set Maillist = wdApp.documents.Open(strFileName) 'Open the selected document
' iterate through all items in the Outlook Outbox and adds an email from column 2 and an attachment from paths in colums 3plus
' By following change eliminates the security access prompts!
'Set olNS = oOutlookApp.GetNamespace("MAPI")
Set olNS = GetNamespace("MAPI")
Set MyFolder = olNS.GetDefaultFolder(olFolderOutbox)
For i = 1 To MyFolder.Items.count
Set Item = MyFolder.Items(i)
Set Datarangecc = Maillist.Tables(1).Cell(i, 2).Range
Datarangecc.End = Datarangecc.End - 1
Item.CC = Datarangecc
For j = 3 To Maillist.Tables(1).Columns.count
Set Datarange = Maillist.Tables(1).Cell(i, j).Range
Datarange.End = Datarange.End - 1
If FileExists(Trim(Datarange.Text)) Then
Item.Attachments.Add Trim(Datarange.Text), olByValue, 1
End If
Next j
Item.Save
Item.sEnd
count = count + 1
Next i
Maillist.Close 0
MsgBox count & " emails have had a cc added."
'Clean up
lbl_Exit:
If bStarted Then wdApp.Quit
Set Maillist = Nothing
Set wdApp = Nothing
Set Datarangecc = Nothing
Set Datarange = Nothing
Set olNS = Nothing
Set Item = Nothing
Set MyFolder = Nothing
Exit Sub
End Sub
Private Function FileExists(filespec) As Boolean
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(filespec) Then
FileExists = True
Else
FileExists = False
End If
lbl_Exit:
Exit Function
End Function
Function GetFile(OtherObject As Object, strPath As String) As String
Dim sItem As String
Dim fdFile As office.FileDialog
Set fdFile = OtherObject.FileDialog(msoFileDialogFilePicker)
With fdFile
.Title = "Select a File"
.AllowMultiSelect = False
.InitialFileName = strPath & "*.docx"
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
GetFile = sItem
lbl_Exit:
Set fdFile = Nothing
Exit Function
NextCode:
GetFile = ""
GoTo lbl_Exit
End Function