MailMerge Can't Find My File

loract

New Member
Joined
Apr 21, 2023
Messages
14
Office Version
  1. 365
Platform
  1. Windows
I need help with a file path not opening in a MailMerge I am trying to write. I copied this code from a YouTube video and after some research on the 5174 error "Sorry can't find your file? Was it removed,etc?" I added the line about replacing Chr(13) but I'm still getting the error at the line
VBA Code:
oDoc.MailMerge.OpenDataSource sPath

I would really appreciate any help you can give me!
VBA Code:
Public Sub LabelMerge()
' From https://www.youtube.com/watch?v=0nBcxc5zt1Q
Dim oWord As Word.Application, oDoc As Word.Document
Dim sPath As String, i As Integer, oHeaders As Range, bPrint As Boolean
Set oHeaders = Range("A1").CurrentRegion.rows(1)
sPath = ActiveWorkbook.FullName
sPath = Trim(sPath)
sPath = Replace(sPath, "Chr(13)", "")

If MsgBox("Print labels out (or send to new document)?", vbOKCancel) = vbOK Then bPrint = True
Set oWord = CreateObject("Word.Application")
Set oDoc = oWord.Documents.Add
oWord.Visible = True
oDoc.MailMerge.MainDocumentType = wdMailingLabels
oWord.Dialogs(wdDialogLabelOptions).Show
oDoc.Activate
With oDoc.MailMerge.Fields
    For i = 1 To oHeaders.Columns.Count
        .Add oWord.Selection.Range, oHeaders.Cells(1, i)
        oWord.Selection.TypeText " "
    Next i
End With
oDoc.MailMerge.OpenDataSource sPath
oWord.WordBasic.mailmergepropagatelabel
oDoc.MailMerge.ViewMailMergeFieldCodes = False
oDoc.ActiveWindow.View.ShowFieldCodes = False
If bPrint = True Then
    oDoc.MailMerge.Destination = wdSendToPrinter
    oDoc.MailMerge.Execute
Else
    oDoc.MailMerge.Destination = wdSendToNewDocument
    oDoc.MailMerge.Execute
End If

Set oDoc = Nothing
Set oWord = Nothing



End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
I don't understand why you are trying to edit the file name.
Rich (BB code):
sPath = ActiveWorkbook.FullName
sPath = Trim(sPath)
sPath = Replace(sPath, "Chr(13)", "")
I don't think it should be necessary.

What happens if you remove those two lines?

If you add the line in red below your current line, what does it return?
Rich (BB code):
sPath = ActiveWorkbook.FullName
MsgBox sPath
 
Upvote 0
And does it make any difference if you change this line:
Rich (BB code):
oDoc.MailMerge.OpenDataSource sPath
to
Rich (BB code):
oDoc.MailMerge.OpenDataSource Name:=sPath

You may also want to take a look at the various things discussed in this thread here:
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,175
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top