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
 
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.
I'm not sure where, but once I run the code the excel gets stuck, see if you can help. Thanks
VBA Code:
Sub SOCMacro()

Dim d As String
Dim e As String
Dim strSql As String


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

Application.DisplayAlerts = False
Application.ScreenUpdating = False

d = a & "\" & b
e = a & "\" & C
strSql = "SELECT * FROM 'RawData$'"

Call MergeRun(d, e, strSql)


End Sub

Sub MergeRun(frmFile As String, datfile As String, SQL As String, Optional bClose As Boolean = False, Optional bPrint As Boolean = False, _
Optional iNoCopies As Long = 1)

If Dir(frmFile) = "" Then
MsgBox "Form file does not exist." & vbLf & frmFile, vbCritical, "Exit - Missing Form File"
End If

If Dir(datfile) = "" Then
MsgBox "Data file does not exist." & vbLf & datfile, vbCritical, "Exit - Missing Data File"
End If
 
If Dir(frmFile) = "" Or Dir(datfile) = "" Then Exit Sub

Dim wdApp As New Word.Application, myDoc As Word.Document
 
With wdApp
.DisplayAlerts = wdAlertsNone

Set myDoc = .Documents.Open(Filename:=frmFile, ReadOnly:=False, AddToRecentFiles:=False)

With myDoc
With .MailMerge
.MainDocumentType = wdFormLetters
.OpenDataSource Name:=datfile, ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=False, Revert:=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
End With

With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With

.Execute Pause:=False
End With

.Close False

End With

.DisplayAlerts = wdAlertsAll

If bPrint = True Then .ActiveDocument.PrintOut Copies:=iNoCopies
If bClose = True Then
.ActiveDocument.Close False
.Quit

End If

End With

Set myDoc = Nothing: Set wdApp = Nothing
 
End Sub
 
Last edited by a moderator:
Upvote 0

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I see that you have been googling. Once again though you have used a non descriptive term. The term 'stuck' does not help us help you.

Please explain, what is working, when it stops working, any errors encountered, does it just keep running and never stops, etc.
 
Upvote 0
Please remember, we are not there looking over your shoulder. We can't see what the program is doing or not doing. You have to be as descriptive as possible to relate what is happening or not happening.
 
Upvote 0
I see that you have been googling. Once again though you have used a non descriptive term. The term 'stuck' does not help us help you.

Please explain, what is working, when it stops working, any errors encountered, does it just keep running and never stops, etc.
Even I'm not sure where I'm stuck. Whenever I try to run the code it opens the word document and it does nothing it just gets stuck.
 
Upvote 0
Please remember, we are not there looking over your shoulder. We can't see what the program is doing or not doing. You have to be as descriptive as possible to relate what is happening or not happening.
I understand that, however I'm not sure where I have made a mistake in the code so that I can point out. Just that whenever I run it. It opens the proper word document and then it gets stuck
 
Upvote 0
So you have made some progress, now you can get the word document to open?
 
Upvote 0
In word I'm getting this popup
 

Attachments

  • Capture.PNG
    Capture.PNG
    20.8 KB · Views: 11
Upvote 0
Just my opinion, but I would consider this thread as solved.

I would recommend that you post what you did to finally get the word file to open, and then start a new thread with a new question so the answer to your original question here is not lost in the shuffle.

I am not a moderator here, so take my opinion with a grain of salt as the saying goes.
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,054
Latest member
juliecooper255

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