locked
PowerShell to extract information from a Textfile and dump in a Word file RRS feed

  • Question

  • Hi All, 

    Could anybody please help me with a powershell script to do the following task.

    Let's say I have a text file called C:\origin.txt, it looks like the following 

    ItemName:    A 
    Color:   111
    Weight:  222
    Height:    333
    Age:  444
    ItemName:   B
    Color:   555
    Weight: 666
    Height: 777
    Age: 888

    I want to selective show the Weight and Age for each Item, and output the information to a word file: C:\output.docx

    Note: A line ---- or an empty line should seperate the two items

    ItemName:    A 
    Weight:  222
    Age:  444
    ----------------------
    ItemName:   B
    Weight: 666
    Age: 888

    Any Idea on how to do it ? 

    I've tried coding it but doesn't work out as what I want. 

    Thank you! 

    Tuesday, February 11, 2014 11:08 PM

Answers

  • For Word specific issue you need to do a few things.  First spend time learning how to use Word.  Next post word VBA questions in the Word VBA developers forum.

    As a scripter it will beyour responsibility to look these things up.  THat is how all of use do this.  There are way too many variations and you will have to learn how to look things up, test and validate you code.

    Now back to my original suggestion and example.  If you use my example the file will remain intact.  You can apply a word template to make it look the way you want.  YOU can add extra text and whitespace before inserting the text into the document.  Or...you can use word basics to edit the text in word.

    Word comes with Developer VBA help.  Look up the examples for how to use Word.


    ¯\_(ツ)_/¯

    • Marked as answer by Bill_Stewart Wednesday, March 5, 2014 10:13 PM
    Wednesday, February 12, 2014 2:28 PM

All replies

  • What's the purpose? Why does it need to go into a Word document? It would seem more sensible to me to convert it to a database-like format such as CSV.

    Bill

    Tuesday, February 11, 2014 11:19 PM
  • You can just open the text file in word and reformat it as needed.  You don't need a script to do that.  Word reads text files.

    You can even create templates (MailMerge) in Word that will do exactly what you are asking to do.


    ¯\_(ツ)_/¯

    Tuesday, February 11, 2014 11:21 PM
  • yes I agree with you, CSV may be better.

    But it's a company report that has to be sent out in Word format together with other information. 

    Not just this one type. 

    Wednesday, February 12, 2014 4:21 AM
  • Well, 

    there are more than one of this Text files. Each has its own data. I'm trying to get different data from different files not just one.

    So trying to use a script to automate that process. 

    Wednesday, February 12, 2014 4:22 AM
  • Hi,

    <snip>

    Any Idea on how to do it ? 

    I've tried coding it but doesn't work out as what I want. 

    Can you post what you have so far and what errors you're running into? That'll make it easier to help you.

    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)

    Wednesday, February 12, 2014 4:29 AM
  • Starters:

    #Get a word doc
    $wrd=New-Object -Com Word.Application
    $wrd.Visible=$true
    $doc=$wrd.Documetns.Add()
    $doc.Range().Text=Content file.txt


    ¯\_(ツ)_/¯

    Wednesday, February 12, 2014 5:30 AM
  • Hi Mike, 

    Please see my code below. 

    $savepath = "C:\Output.docx"
    
    $word=new-object -ComObject "Word.Application"
    $doc=$word.documents.Add()
    $Output=$word.Selection
    
    #---------------
    $ORIGIN = "C:\origin.txt" 
    
    $writeItem = Get-Content $ORIGIN | Select-String "ItemName", "Weight", "Age" 
    
    $Output.TypeText(($writeItem))
    
    # ----------
    
    
    $doc.SaveAs([ref]$savepath)    
    $doc.Close()
    $word.quit()
    
    Invoke-Item $savepath

    The output is:

    ItemName:    A  Weight:  222 Age:  444 ItemName:   B Weight: 666 Age: 888

    How can I start a new line for each tag, and insert a "----------" between the two Item? 

    Thank you!



    • Edited by IvyNorth Wednesday, February 12, 2014 2:17 PM
    Wednesday, February 12, 2014 2:13 PM
  • For Word specific issue you need to do a few things.  First spend time learning how to use Word.  Next post word VBA questions in the Word VBA developers forum.

    As a scripter it will beyour responsibility to look these things up.  THat is how all of use do this.  There are way too many variations and you will have to learn how to look things up, test and validate you code.

    Now back to my original suggestion and example.  If you use my example the file will remain intact.  You can apply a word template to make it look the way you want.  YOU can add extra text and whitespace before inserting the text into the document.  Or...you can use word basics to edit the text in word.

    Word comes with Developer VBA help.  Look up the examples for how to use Word.


    ¯\_(ツ)_/¯

    • Marked as answer by Bill_Stewart Wednesday, March 5, 2014 10:13 PM
    Wednesday, February 12, 2014 2:28 PM