What Will I Learn?
Learn more about word.Application object
How to use powershell to create ,save and close a word workbook
Requirements
Windows or other system like linux,macos
Powershell
Coding IDE
.NET framework
Difficulty
- Intermediate
Tutorial Contents
Before contents:
If you do not know any about powershell ,you had better reading this post firstly as well as consulting more from the books
Brief instructions
I assume that many readers use PowerShell to generate reports on servers, events, and various data on the Windows network. You may have created plain text files, even beautiful HTML reports. But you can also use Microsoft Word, of course, if you have already installed Word. In this part , I'll show you how to use the PowerShell to apply styles for Word documents.
Create the Word application object.
PowerShell can control Word applications through COM interfaces. The interesting thing is that although you can do all of the operations interactively, I hope you will eventually be able to manipulate everything with scripts. We start by creating a Word program object.
Use powershell to create a Word workbook
Create a COM object for the Word application.
$Word =new-object -ComObject "Word.Application"
- New-Object:create a new object
- -ComObject: the type of new object is COM
- Word.Application : the COM object name is Word.Application
Show visibility of the Word.Application
$Word.Visible = $True
Visible : the new Word application view is not displayed by default
$True : set the visibility of the Word application is visible
Create a workbook document adding to Word application
$doc=$Word.documents.Add()
documents.Add(): create a workbook container
However, without creating a document this can not be used, but it should create document firstly , which is the container for the word application. Now we need to add a document to the application. To do this, we need to use add method of the document object. The document object is accessed from the main Word .Application object. As you can see, we store the document object in a variable named $doc:
Save the word file
$doc.SaveAs([ref]"C:\Users\Administrator\Desktop\cha0s0000.docx")
SaveAs : Save the document workbook to file.After saving ,you can see the name of the word file is changed to cha0s0000
Close document workbook
$doc.Close()
Close() : After saving the file , close to release the resources.As we can see the below picture ,the document workbook is closed ,but the word application is still running.
Quit the word application object
$Word.quit()
quit() : Release the object sources.Quit the word application and the word.exe will be closed and release all the object.Show as below.
The whole program
$Word =new-object -ComObject "Word.Application" $Word.Visible = $True $doc=$Word.documents.Add() $doc.SaveAs([ref]"C:\Users\Administrator\Desktop\cha0s0000.docx") $doc.Close() $Word.quit()
Check the effect of the program
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Thanks
Hey @redart, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!
folback @zefriadi12 and upvote
Hi, we have seen that you submit a lot of similar tutorials about Power Shell. Please do not submit anymore or find more complex topics to cover. The structure of all tutorials are the same, you only change the commands.
I intend to write about the relative basic use about powershell and then give the more complex in the following tutorials
Rest assured it will be more and more diffcult in the following posts
Hey @cha0s0000 I am @utopian-io. I have just upvoted you!
Achievements
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x