Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #---Useless initialization stuff----------------------
  2. $date = get-date -format MM-dd-yyyy-HH-mm-ss
  3. #$filePath = "C:\Users\Tomawar40\TestDocStore"
  4. [ref]$SaveFormat = "microsoft.office.interop.word.WdSaveFormat" -as [type]
  5. $word = New-Object -ComObject word.application
  6. $word.visible = $true
  7. $doc = $word.documents.add()
  8. $selection = $word.selection
  9.  
  10.  
  11. #---USELESS - $selection.WholeStory
  12. $selection.Style= "No Spacing"
  13. $selection.font.size = 12
  14. $selection.font.bold = 1
  15. $selection.typeText("My Document: THIS IS A TEST TITLE")
  16. #---Previous Parts Are to set up and initially make a first line on the Document.
  17. $selection.font.size = 10
  18. $selection.font.bold = 0
  19.  
  20. #---------- Here's where the trouble starts--------------
  21. #$selection.typeText("`v")
  22. #$selection.typeText("=lorem(9,10)`v")
  23.  
  24. # ***Begin new
  25. $selection.TypeParagraph()
  26. [void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
  27. [void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
  28.  
  29. $app = Get-Process winword
  30. '=lorem(9,10)' | clip
  31. [Microsoft.VisualBasic.Interaction]::AppActivate($app.ID)
  32. $selection.Paste()
  33. Start-Sleep 1
  34. $selection.TypeParagraph()
  35. $selection.MoveLeft()
  36. [System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
  37. # ***End New
  38.  
  39. #--FAILED TEST A: Test to maybe press enter?
  40. #--FAILED TEST B: Add-Type -AssemblyName System.Windows.Forms
  41. #--           [System.Windows.Forms.SendKeys]::SendWait('~');
  42. #--FAILED TEST C: $selection.sendKeys({ENTER})
  43. sleep 5
  44. $selection.TypeParagraph()
  45. $selection.TypeParagraph()
  46. $Selection.font.size = 11
  47. $selection.typeText("Date: $date")
  48.  
  49.  
  50. #---This part saves the document and then closes the word application
  51. $doc.saveas([ref] $filePath, [ref]$saveFormat::wdFormatDocument)
  52. $doc.Close()
  53. $word.Quit()
  54.  
  55. #---This part clears up memory and destroys the instance
  56. $null = [System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$word)
  57. [gc]::Collect()
  58. [gc]::WaitForPendingFinalizers()
  59. Remove-Variable word
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement