Unable to open a Word document with Below VBA Code

siddo

Board Regular
Joined
May 26, 2020
Messages
106
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
VBA Code:
Sub SOCMacro()

a = Range("B1").Value
b = Range("B2").Value
C = Range("B3").Value

Application.DisplayAlerts = False
Application.ScreenUpdating = False

d = a & "\" & b
e = a & "\" & C

Call MergeRun("d", "e", "Select [DateA], [Address], [Service Organization Name], [Title of description of service organization’s system],[As of Date(D)],[Date to Date(E)], [Date(H)], [Subservice Orgenaization Name], [Indicate service(s) provided by the subservice organization], [Type or name of system], [Insert the title of subservice organization assertion] FROM [RawData$A2:L5]")

End Sub

Sub MergeRun(frmFile As String, datfile As String, SQL As String)


Dim wdApp As Word.Application
Dim myDoc As Word.Document


Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set wdApp = CreateObject("Word.Application")
End If


With wdApp

.Visible = True


Set myDoc = .Documents.Open(frmFile, False, False, False)
.ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
.ActiveDocument.MailMerge.OpenDataSource Name:=datfile, _
ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=False, _
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
Format:=wdOpenFormatAuto, Connection:="", SQLStatement:=SQL, SQLstatement1:="", _
SubType:=wdMergeSubTypeOther

With wdApp.ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True


With .DataSource
.FirstRecord = 1
.LastRecord = 2
End With
.Execute Pause:=False
End With

wdApp.Application.DisplayAlerts = wdAlertsAll
End With

errorExit:
On Error Resume Next
myDoc.Close False

 
End Sub

Request if you could help
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
'Unable to open' is not very descriptive.

Do you get an error? If so, on what line of code and what error?

Does it not do anything?

Please provide us some more details.
 
Upvote 0
'Unable to open' is not very descriptive.

Do you get an error? If so, on what line of code and what error?

Does it not do anything?

Please provide us some more details.

No, I'm not getting error. However I'm unable to open the word file with this line of code.
VBA Code:
Set myDoc = .Documents.Open(frmFile, False, False, False)
 
Upvote 0
What is the value of frmFile when you get to that line?
 
Upvote 0
That is supposed to be the file that you want to open.
 
Upvote 0
That is supposed to be the file that you want to open.
Yes the file I want to open is "D" itself, however the code doesn't open it. Not sure what is wrong with the code. Now I'm getting activex error as well for the same set of code
 
Upvote 0
d or D is not a file name. I believe you need the full file path, file name, & extension.
 
Upvote 0
d or D is not a file name. I believe you need the full file path, file name, & extension.
I'm taking input for the file name from user and storing it in variable "D"/"d"
Capture.PNG
 
Upvote 0
If it is a variable then get rid of the quotation marks around it in the Call line of code.

Same thing with the e variable.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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