# Batch Processing a pile of Word Documents

**URL:** <https://forums.powershell.org/t/batch-processing-a-pile-of-word-documents/7983>\
**Category:** PowerShell Help\
**Created:** [February 3, 2017, 7:31am UTC](https://forums.powershell.org/t/batch-processing-a-pile-of-word-documents/7983 "2017-02-03T07:31:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike-namib](https://avatars.discourse-cdn.com/v4/letter/m/85f322/32.png) [@mike-namib](https://forums.powershell.org/u/mike-namib)\
**Post date:** [February 3, 2017, 7:31am UTC](https://forums.powershell.org/t/batch-processing-a-pile-of-word-documents/7983/1 "2017-02-03T07:31:12Z")

</div>

Hey Folks,

I’ve been looking into a problem of a friend of mine. He has a group people filling aut a form (word / doc /docx ) and these are in a folder. So we need to automate the processing hundreds documents and grab specific parameters and write the into a “.csv”-file.

So, I can read the files and stack them but fishing the specified string, that`s where it starts to get tuff.

Can anybody help out here?

```
$docPath = $args[0]
Write-Host "Processing Documents from:" $docPath 
$all_docs = Get-ChildItem $docPath -filter "*.docx"

$word = New-Object -comobject "Word.Application"
$word.Visible = $False

# Now, open each document and list the "ContentControls"
$all_items = @()
foreach ( $doc in $all_docs)
{
  Write-Host "Processing :" $doc.FullName
  $doc = $word.Documents.Open($doc.FullName);
	
  $controls = $doc.ContentControls.Count	
  Write-Host "Found : " $controls.Count " Content Controls"

  # Now, we create a collection of custom objects which are holding the data
  $item = New-Object System.Object
  foreach ( $control in $doc.ContentControls )
  {
    $item | Add-Member -type NoteProperty -name $control.Title -value $control.Range.Text
  }
  $item
  $all_items += $item
  $doc.Close()
}

# Last, we save the collection to a CSV file
$all_items | Export-CSV "exportData_DATE.CSV"
```

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [February 3, 2017, 11:30am UTC](https://forums.powershell.org/t/batch-processing-a-pile-of-word-documents/7983/2 "2017-02-03T11:30:28Z")

</div>

Yeah, Word is about the worst-case scenario for this, unfortunately, and you’re going to be stuck with a decade-old COM object to work with. And this isn’t _really_ PowerShell; it’s COM programming against Word. I say this only because, historically, we’ve have very low turnout on this type of question, and I didn’t want you hanging around waiting for an answer that might not be forthcoming. Sorry :(.

But, if you figure it out, maybe you’ll consider dropping by now and again to answer these kinds of questions when they come up :).

---

<div class="post-metadata">

**Author:** ![skalizar](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@skalizar](https://forums.powershell.org/u/skalizar)\
**Post date:** [February 3, 2017, 12:04pm UTC](https://forums.powershell.org/t/batch-processing-a-pile-of-word-documents/7983/3 "2017-02-03T12:04:25Z")

</div>

It looks like he may have already figured out the COM part, but doesn’t know how to get the data being returned in the format he wants. It might help to fill out a form with test data, show us the output, and then show us what you want it to be. This might actually be more of a Regex question.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:39pm UTC](https://forums.powershell.org/t/batch-processing-a-pile-of-word-documents/7983/4 "2024-05-16T20:39:17Z")

</div>


