Created
January 29, 2017 16:49
-
-
Save CosmosKey/7ba294d53889991f2720db8e0c58a810 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#################################### BEGIN ############################################## | |
$Title = "My Doc" | |
$lab = { | |
########## Module | |
###### Task - Set an alias | |
#### Description | |
## Set an alias using Set-Alias and test it. | |
### Example: | |
Set-Alias -Name list -Value Get-ChildItem | |
# Now try the list command | |
list C:\ | |
Get-Alias -Name List | |
# pop an image in a folder and reference it this way ##@image c:\ps\test.gif | |
} #################################### END ############################################## | |
<# | |
$text = ($lab.ToString().Split("`n") | % -begin {$s=0;$t=1} { | |
$x=$_ | |
switch ($x) { | |
{$_.Contains("%SECTION%")} {$s++;$x.Replace("%SECTION%",$s);$t=1} | |
{$_.Contains("%TASK%")} {$x.Replace("%TASK%","$s.$t");$t++} | |
default {$x} | |
} | |
}) -join "`n" | |
#> | |
$text = $lab.ToString() | |
$date = Get-Date -Format "yyyy-MM-dd HHmmss" | |
$file = "$env:TEMP\$Title ($date).ps1" | |
$filePath = "$env:TEMP\$Title ($date).docx" | |
Set-Content -Path $file -Value $text | |
$originalFile = $psISE.CurrentFile.FullPath | |
[void]$psISE.CurrentPowerShellTab.Files.Add($file) | |
$keys = "^{i}^{a}^{c}{ESC}" | |
for($i=0;$i -lt $psISE.CurrentPowerShellTab.Files.Count;$i++){ | |
$keys = $keys + "^{TAB}" | |
if($psISE.CurrentPowerShellTab.Files[$i].FullPath -eq $originalFile) { | |
break | |
} | |
} | |
[System.Windows.Forms.SendKeys]::SendWait($keys) | |
#$psise.CurrentFile.Editor.Select(1,1,$psise.CurrentFile.Editor.LineCount,$psise.CurrentFile.Editor.GetLineLength($psise.CurrentFile.Editor.LineCount)+1) | |
[ref]$SaveFormat = "microsoft.office.interop.word.WdSaveFormat" -as [type] | |
$word = New-Object -ComObject word.application | |
$word.visible = $true | |
$doc = $word.documents.add() | |
$selection = $word.selection | |
$selection.PasteAndFormat($wdFormatOriginalFormatting) | |
$selection.start = 0 | |
$selection.end = 0 | |
$section = 0 | |
$task = 0 | |
$FindText = "##" | |
$wdFindContinue = 1 | |
$wdReplaceAll = 2 | |
$MatchCase = $False | |
$MatchWholeWord = $False | |
$MatchWildcards = $False | |
$MatchSoundsLike = $False | |
$MatchAllWordForms = $False | |
$Forward = $True | |
$Wrap = 0 #$wdFindContinue | |
$Format = $False | |
$wdReplaceNone = 0 | |
$ReplaceWith = "" | |
#$selection.Find.ClearFormatting() | |
#$selection.Find.Forward = $true | |
#$selection.Find.Wrap = $wdFindContinue | |
do { | |
$found = $selection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, ` | |
$MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,` | |
$Wrap,$Format,$ReplaceWith,$wdReplaceNone) | |
if($found) { | |
$lineRange = $selection.Range.Paragraphs.Item(1).Range | |
switch -Wildcard ($lineRange.Text) { | |
"########## Module*" { | |
$section++ | |
$task = 0 | |
$lineRange.Text = $lineRange.Text -replace "########## Module","`t`t`tMODULE $section" | |
$lineRange.Style = "Heading 1" | |
$pos = $lineRange.Start | |
$selection.Start = $pos | |
$selection.End = $pos | |
if($section -ne 1) { | |
$selection.InsertBreak(7) # 7 = pagebreak | |
} | |
break | |
} | |
"###### Task*" { | |
$task++ | |
$lineRange.Text = $lineRange.Text -replace "###### Task","TASK $section.$task" | |
$lineRange.Style = "Heading 2" | |
$lineRange.Font.Bold = 1 | |
break | |
} | |
"#### *" { | |
$lineRange.Text = $lineRange.Text -replace "#### ","" | |
#$lineRange.Style = "Heading 3" | |
#$lineRange.Font.Bold = 1 | |
$lineRange.Style = "Normal" | |
$lineRange.Font.Bold = 1 | |
$lineRange.Font.Size = 13 | |
break | |
} | |
"### *" { | |
$lineRange.Text = $lineRange.Text -replace "### ","" | |
#$lineRange.Style = "Heading 3" | |
#$lineRange.Font.Bold = 0 | |
$lineRange.Style = "Normal" | |
$lineRange.Font.Bold = 1 | |
$lineRange.Font.Size = 11 | |
break | |
} | |
"## *" { | |
$lineRange.Text = $lineRange.Text -replace "## ","" | |
$lineRange.Style = "Normal" | |
break | |
} | |
"##@image *" { | |
$fileImage = $lineRange.Text -replace "##@image *" | |
$fileImage = $fileImage.Trim() | |
$pos = $lineRange.Start | |
$lineRange.Text = "" | |
$selection.Start = $pos | |
$selection.End = $pos | |
if(Test-Path $fileImage) { | |
[void]$selection.InlineShapes.AddPicture("$fileImage") | |
} else { | |
Write-Warning "Image file $fileImage does not exist" | |
} | |
break | |
} | |
"##*" { | |
$lineRange.Text = $lineRange.Text -replace "##","" | |
$lineRange.Style = "Normal" | |
break | |
} | |
} | |
} | |
} while ($found) | |
$selection.Start = 0 | |
$selection.End = 0 | |
$selection.TypeParagraph() | |
$selection.TypeParagraph() | |
$selection.TypeParagraph() | |
$selection.TypeParagraph() | |
$Selection.Style = 'Title' | |
$Selection.Style.Font.Bold = 1 | |
$Selection.TypeText($Title) | |
$Selection.ParagraphFormat.Alignment = 1 | |
$selection.TypeParagraph() | |
$selection.InsertBreak(7) # 7 = pagebreak | |
$document = $word.Documents.Item(1) | |
$range = $selection.Range | |
$toc = $document.TablesOfContents.Add($range) | |
$Selection.TypeParagraph() | |
$selection.InsertBreak(7) # 7 = pagebreak | |
<# | |
[Enum]::GetNames([Microsoft.Office.Interop.Word.WdBuiltinStyle]) | ForEach { | |
[pscustomobject]@{Style=$_} | |
} | Format-Wide -Property Style -Column 4 | |
#> | |
$doc.saveas([ref]$filePath, [ref]$saveFormat::wdFormatDocument) | |
$word.quit() | |
Start-Process $filePath |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment