Need a code to open Word via Excel VBA

eduzs

Well-known Member
Joined
Jul 6, 2014
Messages
704
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
I am working on a project and I need to code it using VBA to open an existing Word doc (hidden windows if possible), type anything in anywhere in this doc, print this Word doc, then close the doc, and if there's no other open document close the Word application and set focus (activate) back the Excel window with the worksheet last on focus.

Need help.

Thanks
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
At its most basic you could use code like:
Code:
Sub Demo()
'Note: A reference to the Word library must be set, via Tools|References
Dim wdApp As New Word.Application, wdDoc As Word.Document
Const StrDocNm As String = "Full document name & path"
If Dir(StrDocNm) = "" Then Exit Sub
Set wdDoc = wdApp.Documents.Open(Filename:=StrDocNm, ReadOnly:=False, AddToRecentfiles:=False)
With wdDoc
    'process the document
    
    'print the document
    .PrintOut
    'save & close
    .Close SaveChanges:=True
End With
Set wdDoc = Nothing: Set wdApp = Nothing
End Sub
However, since you don't say what is to be updated or where the data for that come from, I can't help you with that. Note that a VBA refernce to the Word library is required and that the code also assumes the changes are to be saved. You'll also need to supply the full document name & path, as indicated in the code.
 
Upvote 0
It seens that work fine, I will adapt to my existing code.

Thanks
 
Upvote 0

Forum statistics

Threads
1,223,806
Messages
6,174,725
Members
452,578
Latest member
Predaking

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