Excel Macro to Print a Document In Word Works - But Hangs When Word is Already Open

StephenBart

New Member
Joined
Jun 22, 2023
Messages
15
Office Version
  1. 365
Platform
  1. Windows
I have built a excel loader to populate customer proposals from data in created in an excel workbook. The macro opens a mail merge word template and names it with path and filenamed based on the contents of cell values.

Macro work great but hangs in Word is already open - any idea how to fix this - i'd rather not have to close word every time Ii run the macro?

Sub CreateNewCBO()
'
' Creates CBO Onboarding Package for JPVI
'
Dim iApp As Word.Application
Dim iDoc As Word.Document
Dim FileName As String
Dim Path As String
Path = Range("c112") & "\3-CBO_Binder\"
FileName = Range("c116")
Set iApp = CreateObject("Word.Application")
iApp.Visible = True
iApp.Activate
Set iDoc = iApp.Documents.Add(Template:="U:\1000 ADMIN\1990 Templates\FORM_re_CBOName-CBO-Onboarding-Package.dotx", NewTemplate:=False, DocumentType:=0)
iDoc.SaveAs2 FileName:=Path & "000_" & FileName & ".docx", FileFormat:=wdFormatXMLDocument, AddtoRecentFiles:=False
End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi StephenBart. This is sort of the standard way to prevent this type of error. HTH. Dave
Code:
Dim iApp As Object
'open Word application
On Error Resume Next
Set iApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
On Error GoTo 0
Set iApp = CreateObject("Word.Application")
End If
iApp .Visible = True
ps. Please use code tags
 
Upvote 0
Thank you Dave - sorry but what does use code tags mean.

do i just add thus at the beginning of my code?
 
Upvote 0
Ok. Here's the whole thing. This does leave the doc open and the Word application running. Dave
Code:
Sub CreateNewCBO()
'
' Creates CBO Onboarding Package for JPVI
'
Dim iApp As Object
Dim iDoc As Word.Document
Dim FileName As String
Dim Path As String
Path = Range("c112") & "\3-CBO_Binder\"
FileName = Range("c116")
'open Word application
On Error Resume Next
Set iApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
On Error GoTo 0
Set iApp = CreateObject("Word.Application")
End If
iApp .Visible = True
Set iDoc = iApp.Documents.Add(Template:="U:\1000 ADMIN\1990 Templates\FORM_re_CBOName-CBO-Onboarding-Package.dotx", NewTemplate:=False, DocumentType:=0)
iDoc.SaveAs2 FileName:=Path & "000_" & FileName & ".docx", FileFormat:=wdFormatXMLDocument, AddtoRecentFiles:=False
End Sub
 
Upvote 0
Solution
Dave.

Thank you very much. This works great.

Ps Sorry about the code tag - will use in future.

Have a great day!
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

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