Use powershell to create a word document

in utopian-io •  7 years ago  (edited)

What Will I Learn?

  • Learn more about word.Application object

  • How to use powershell to create ,save and close a word workbook

    GIF.gif

Requirements

  • Windows or other system like linux,macos

  • Powershell

    图片.png

  • Coding IDE

    图片.png

  • .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

some powershell foundation


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

  1. 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
  2. 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

      图片.png

  3. 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:

      图片.png

  4. 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

      图片.png

  5. 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.

      图片.png

  6. 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.

      GIF.gif

  1. 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

GIF.gif



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

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

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

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