Error - Active x component can't create an object

siddo

Board Regular
Joined
May 26, 2020
Messages
106
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I'm getting an error - Active X component can't create an object for the below code. Previously the code use to work fine.
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

I'm getting an error at this line

VBA Code:
Set wdApp = GetObject(, "Word.Application")
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Your current code works id Word is already open; to deal with the "word unopen" condition, enclose the failing instruction betweeen On Error resume Next /GoTo 0:
VBA Code:
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
On Error GoTo 0
Bye
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,244
Members
452,622
Latest member
Laura_PinksBTHFT

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