We help IT Professionals succeed at work.

Powershell Scripting : Merge Visio Document into word Document using nothing but powershell.

275 Views
1 Endorsement
Last Modified: 2020-09-17
Experts,

I have a need to insert a visio document into a word document programmatically what is the easiest way to do this?

A snippet of my code is as follows:

#Create word Instance
$merged_Object = ""|TitleHeadingBar
$Merged_Object.TitleheadingBar = "Test"
$Word = New-Object -ComObject Word.Application
#Make Word Visible
$Word.Visible = $True
#Create Blank Document
$Document = $Word.Documents.Add()
#Create Selection object
$Selection = $Word.Selection

#Alignment Options:
$Alignment = ""|Select Center,Left,Right
$Alignment.Center = "1"
$Alignment.Left = "0"
$Alignment.Right = "2"

#Create Content
$Selection.ParagraphFormat.Alignment = "$($Alignment.Center)"
$Selection.Font.Bold = 1
$Selection.Font.Italic = 1
$Selection.Font.Underline = 1
$selection.TypeText("$($Merged_Object.TitleHeadingBar))")

Open in new window


I Thought I might be able to do the following, but it fails with


$selection.InsertFile("C:\worddoc\wordmerge\Drawingvsdx.vsdx")

Open in new window


Word has encountered a problem.
At line:1 char:1
+ $selection.InsertFile("C:\worddoc\wordmerge\Drawingvsdx.vsdx")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2008
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT

Author

Commented:
Thanks for the answer. It worked more efficiently than what I had come up with..


here was the answer I arrived at:


$visio = new-object -ComObject visio.Application
    $visio.Visible = $False
    $visioDoc = $visio.Documents.open("C:\worddoc\wordmerge\Drawingvsdx.vsdx")

    $pagobj = $visiodoc.Pages
    #$pagobj = $visiodoc.Page

    foreach ($pagobj in $pagobj){
        $ActivePage = $pagobj.Name
        write-host "$activepage"
        $pagobj.shapes.application.ActiveWindow.SelectAll()
        $pagobj.Shapes.application.ActiveWindow.Selection.Copy()
    }
        #Paste Clipboard into word
        $Selection.Paste()

        #Exit Visio Application
        $visio.Application.quit()

        #Insert Page Break
        $selection.InsertBreak()
}

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions