<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PowerShell.nu &#187; Office</title>
	<atom:link href="http://www.powershell.nu/tag/office/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.powershell.nu</link>
	<description></description>
	<lastBuildDate>Wed, 14 Jul 2010 22:17:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Insert a Page Break in a Word Document using PowerShell</title>
		<link>http://www.powershell.nu/2009/03/15/insert-a-page-break-in-a-word-document-using-powershell/</link>
		<comments>http://www.powershell.nu/2009/03/15/insert-a-page-break-in-a-word-document-using-powershell/#comments</comments>
		<pubDate>Sun, 15 Mar 2009 22:15:18 +0000</pubDate>
		<dc:creator>Niklas Goude</dc:creator>
				<category><![CDATA[Microsoft Office]]></category>
		<category><![CDATA[Office]]></category>

		<guid isPermaLink="false">http://www.powershell.nu/?p=557</guid>
		<description><![CDATA[I got this idea from &#8220;dmtelf&#8221; who posted a comment on a previous post I wrote regarding Microsoft Word and PowerShell. I ran through a few tests and this is the result. First, lets Insert a Page Break after every Paragraph in a Word Document. The script will insert a Page break after each Paragraph. [...]]]></description>
			<content:encoded><![CDATA[<p>I got this idea from &#8220;dmtelf&#8221; who posted a comment on a previous post I wrote regarding Microsoft Word and PowerShell. I ran through a few tests and this is the result.</p>
<p />
First, lets Insert a Page Break after every Paragraph in a Word Document. The script will insert a Page break after each Paragraph. I&#8217;ve created a sample Word Document that has 5 Lorem Ipsum paragraphs.</p>
<p />
<a href="http://www.powershell.nu/wp-content/uploads/2009/03/word-03.jpg"><img src="http://www.powershell.nu/wp-content/uploads/2009/03/word-03-499x591.jpg" alt="word-03" title="word-03" width="499" height="591" class="alignnone size-large wp-image-558" /></a></p>
<p />
Connecting to the Word Document is done through the Word.Application -Com Object. Then we use the Open() method to connect to our Word Document. Next we create a New Variable holding every Paragraph in the Word Document, Loop through the Paragraphs and set the PageBreak to -1. This sets a Pagebreak for each new Paragraph. </p>
<p />
<pre>
<strong>
function Insert-PageBreak ([string]$File) {
	$Word = New-Object -Com Word.Application
	$OpenDoc = $Word.Documents.Open($File)
	$Paragraphs = $OpenDoc.Paragraphs

	$Paragraphs | ForEach {

		$_.PageBreakBefore = -1

	}

	$OpenDoc.Close()
}
</strong>
</pre>
<p />
Running the Function on a Word Document inserts a PageBreak after each Paragraph as shown below:</p>
<p />
<pre>
<strong>
PS > Insert-PageBreak "C:\Documents\Lorem Ipsum.doc"
</strong>
</pre>
<p />
<a href="http://www.powershell.nu/wp-content/uploads/2009/03/word-04.jpg"><img src="http://www.powershell.nu/wp-content/uploads/2009/03/word-04-499x591.jpg" alt="word-04" title="word-04" width="499" height="591" class="alignnone size-large wp-image-559" /></a></p>
<p />
Now, what if i want to insert a Pagebreak after a &#8220;Heading 1&#8243; instead of a paragraph, when I write Word documents i sometimes get a little sloppy and push &#8220;enter&#8221; a little too often so that i get lots of unwanted paragraphs. </p>
<p />
<a href="http://www.powershell.nu/wp-content/uploads/2009/03/word-05.jpg"><img src="http://www.powershell.nu/wp-content/uploads/2009/03/word-05-499x591.jpg" alt="word-05" title="word-05" width="499" height="591" class="alignnone size-large wp-image-560" /></a></p>
<p />
We can solve this with a simple if statement as shown below.</p>
<p />
<pre>
<strong>
function Insert-PageBreak ([string]$File) {
	$Word = New-Object -Com Word.Application
	$OpenDoc = $Word.Documents.Open($File)
	$Paragraphs = $OpenDoc.Paragraphs

	$Paragraphs | ForEach {

	$_.Style.NameLocal
		if ($_.Style.NameLocal -match "Heading 1") {
			$_.PageBreakBefore = -1
		}
	}

	$OpenDoc.Close()
}
</strong>
</pre>
<p />
Running the Function on a Word Document containing &#8220;Heading 1&#8243; sets the PageBreak for each New &#8220;Heading 1&#8243;</p>
<p />
<pre>
<strong>
PS > Insert-PageBreak "C:\Documents\Lorem Ipsum.doc"
</strong>
</pre>
<p />
<a href="http://www.powershell.nu/wp-content/uploads/2009/03/word-06.jpg"><img src="http://www.powershell.nu/wp-content/uploads/2009/03/word-06-500x605.jpg" alt="word-06" title="word-06" width="500" height="605" class="alignnone size-large wp-image-561" /></a></p>
<p />
Note that if the Headings are not correctly inserted in the Word Document, it might screw things up a little in the Document, so take a backup before trying this at home <img src='http://www.powershell.nu/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p />
<a href="http://www.powershell.nu/wp-content/uploads/2009/03/insert-pagebreak.ps1">Click Here to download a Script with a little more functionality</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.powershell.nu/2009/03/15/insert-a-page-break-in-a-word-document-using-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Replacing text in Word documents through PowerShell</title>
		<link>http://www.powershell.nu/2009/02/05/replacing-text-in-word-documents-through-powershell/</link>
		<comments>http://www.powershell.nu/2009/02/05/replacing-text-in-word-documents-through-powershell/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 22:24:22 +0000</pubDate>
		<dc:creator>Niklas Goude</dc:creator>
				<category><![CDATA[Microsoft Office]]></category>
		<category><![CDATA[Office]]></category>

		<guid isPermaLink="false">http://www.powershell.nu/?p=413</guid>
		<description><![CDATA[In a previous migration project, we had some issues with Word documents, actually a couple of thousand Word documents containing Server names pointing to the old servers.. In other words, after the migration, all these word documents would point to the wrong server. Changing this manually would take weeks. Changing the documents through PowerShell would [...]]]></description>
			<content:encoded><![CDATA[<p>In a previous migration project, we had some issues with Word documents, actually a couple of thousand Word documents containing Server names pointing to the old servers.. In other words, after the migration, all these word documents would point to the wrong server. Changing this manually would take weeks. Changing the documents through PowerShell would take a few minutes.</p>
<p />
In this post, I&#8217;m going to demonstrate how you can manipulate text in Word documents through PowerShell. Let&#8217;s say we have a document containing the Lyrics to Johhny Cash &#8211; Ring of Fire.</p>
<p />
<img src="http://www.powershell.nu/wp-content/uploads/2009/02/word-01-500x625.jpg" alt="word-01" title="word-01" width="500" height="625" class="alignnone size-large wp-image-414" /></p>
<p />
Lets replace the word &#8220;ring of fire&#8221; with &#8220;Chuck Norris RoundHouse Kick&#8221;</p>
<p />
The function used is pretty straight forward. First setting up the variables required by the Execute() method and then connecting to the document that we want to manipulate.</p>
<pre>
<strong>
function Replace-Word ([string]$Document,[string]$FindText,[string]$ReplaceText) {

	#Variables used to Match And Replace

	$ReplaceAll = 2
	$FindContinue = 1

	$MatchCase = $False
	$MatchWholeWord = $True
	$MatchWildcards = $False
	$MatchSoundsLike = $False
	$MatchAllWordForms = $False
	$Forward = $True
	$Wrap = $FindContinue
	$Format = $False

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

	$OpenDoc = $Word.Documents.Open($Document)
	$Selection = $Word.Selection

	$Selection.Find.Execute(
		$FindText,
		$MatchCase,
		$MatchWholeWord,
		$MatchWildcards,
		$MatchSoundsLike,
		$MatchAllWordForms,
		$Forward,
		$Wrap,
		$Format,
		$ReplaceText,
		$ReplaceAll
	)

	$OpenDoc.Close()
}
</strong>
</pre>
<p />
Running the function would look something like this:</p>
<p />
<pre>
<strong>
Replace-Word C:\Ring Of Fire.docx "ring of fire" "Chuck Norris RoundHouse Kick"
</strong>
</pre>
<p />
Here&#8217;s what happend to the Document when I ran the function.</p>
<p />
<img src="http://www.powershell.nu/wp-content/uploads/2009/02/word-02-500x625.jpg" alt="word-02" title="word-02" width="500" height="625" class="alignnone size-large wp-image-415" /></p>
<p />
<a href="http://www.powershell.nu/wp-content/uploads/2009/03/replace-word.ps1">Click here to download the Script.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.powershell.nu/2009/02/05/replacing-text-in-word-documents-through-powershell/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Edit Excel SpredSheet through PowerShell</title>
		<link>http://www.powershell.nu/2009/02/05/edit-excel-spredsheet-through-powershell/</link>
		<comments>http://www.powershell.nu/2009/02/05/edit-excel-spredsheet-through-powershell/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 11:31:52 +0000</pubDate>
		<dc:creator>Niklas Goude</dc:creator>
				<category><![CDATA[Microsoft Office]]></category>
		<category><![CDATA[Office]]></category>

		<guid isPermaLink="false">http://www.powershell.nu/?p=400</guid>
		<description><![CDATA[A friend at my company just asked me how to modify a Excel Spredsheet based on a template and save it as a new Excel spredsheet through PowerShell. Looking inot this, There where some issues when trying to close the Excel file through PowerShell. After a little research I found an article by Hristo Deshev [...]]]></description>
			<content:encoded><![CDATA[<p>A friend at my company just asked me how to modify a Excel Spredsheet based on a template and save it as a new Excel spredsheet through PowerShell. Looking inot this, There where some issues when trying to close the Excel file through PowerShell. After a little research I found an article by Hristo Deshev that was really helpful on this. Anyway, let&#8217;s start by looking at the template file. The template file in this example is a simplified version of the one we used but the PowerShell commands are basically the same.</p>
<p />
<img src="http://www.powershell.nu/wp-content/uploads/2009/02/excel-011-500x135.jpg" alt="excel-011" title="excel-011" width="500" height="135" class="alignnone size-large wp-image-402" /></p>
<p />
So it&#8217;s basically 2 cells that I&#8217;m going to modify and then saving it into a new Excel File.</p>
<p />
Connecting to Excel through PowerShell is done using the Excel.Application COM Object.</p>
<p />
<pre>
<strong>
PS > $Excel = New-Object -Com Excel.Application
</strong>
</pre>
<p />
Now that we have a connection to Excel we can open our template file through the Open() method. Use a fully qualified path when pointing out the excel template.</p>
<p />
<pre>
<strong>
PS > $WorkBook = $Excel.Workbooks.Open(C:\Excel\Template.xls)
</strong>
</pre>
<p />
Next, we have to point out which sheet we want o edit. This example uses the first sheet so if you want to modify another sheet, just change the number.</p>
<p />
<img src="http://www.powershell.nu/wp-content/uploads/2009/02/excel-02.jpg" alt="excel-02" title="excel-02" width="382" height="119" class="alignnone size-full wp-image-403" /></p>
<p />
<pre>
<strong>
PS > $WorkSheet = $WorkBook.Worksheets.Item(1)
</strong>
</pre>
<p />
Now that we have a connection to the Sheet, we can start Adding or Modifying information. The Cells that I want to change in this example are A2 and B2. I can specify this through the Item() method. The first Value is Row and the second is Column. When this is done, we can set the Value2 property.</p>
<p />
<pre>
<strong>
PS > $AddFirstName = $WorkSheet.Cells.Item(2,1)
PS > $AddFirstName.Value2 = $FirstName

PS > $AddLastName = $WorkSheet.Cells.Item(2,2)
PS > $AddLastName.Value2 = $LastName
</strong>
</pre>
<p />
Great. The last step is cleaning up and saving into a new Excel file. It&#8217;s important that we Null all Variables that are connected to Excel, otherwise the Excel SpredSheet won&#8217;t close correctly. First we Null the $Add variables.</p>
<p />
<pre>
<strong>
PS > $AddFirstName = $Null
PS > $AddLastName = $Null
</strong>
</pre>
<p />
Next, we save the Information to a new Excel Spredsheet and close the Excel connection</p>
<p />
<pre>
<strong>
PS > $WorkBook.SaveAs(C:\Excel\MyNewFile.xls)
PS > $Excel.Quit()
</strong>
</pre>
<p />
Last step is Nulling all remaining variables connected to Excel and finally releasing the Object Wrapper. This is done by calling [GC]::Collect()</p>
<p />
<pre>
<strong>
PS > $WorkBook = $Null
PS > $WorkSheet = $Null
PS > $Excel = $Null
PS > [GC]::Collect()
</strong>
</pre>
<p />
And now we have a new Excel Spredsheet based on a template file.</p>
<p />
<img src="http://www.powershell.nu/wp-content/uploads/2009/02/excel-03.jpg" alt="excel-03" title="excel-03" width="415" height="86" class="alignnone size-full wp-image-404" /></p>
<p />
Below is a function that automates these steps. Just Change the $File Variable to your Template file instead.</p>
<p><pre>
<strong>
function Set-Excel ($ExcelFile, $FirstName, $LastName) {

	# Template File. Set-Location to the
	# Folder where the template file is placed.

	$File = (ls Template.xls).FullName

	# Open Excel

	$Excel = New-Object -Com Excel.Application

	# Open Template File 

	$WorkBook = $Excel.Workbooks.Open($File)
	$WorkSheet = $WorkBook.Worksheets.Item(1)

	$AddFirstName = $WorkSheet.Cells.Item(2,1)
	$AddFirstName.Value2 = $FirstName

	$AddLastName = $WorkSheet.Cells.Item(2,2)
	$AddLastName.Value2 = $LastName

	# Setting All Variables to Null

	$AddFirstName = $Null
	$AddLastName = $Null

	$WorkBook.SaveAs($ExcelFile)
	$Excel.Quit()

	$WorkBook = $Null
	$WorkSheet = $Null
	$Excel = $Null

	# Releasing Object Wrapper

	[GC]::Collect()

}
</strong>
</pre>
<p />
Examples on running the function:</p>
<p />
<pre>
<strong>
PS > Set-Excel C:\Excel\MyNewFile.xls Niklas Goude
</strong>
</pre>
<p />
]]></content:encoded>
			<wfw:commentRss>http://www.powershell.nu/2009/02/05/edit-excel-spredsheet-through-powershell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
