Rob van der Woude's Scripting Pages

VBScript Scripting Techniques > OLE Automation > WordPerfect

Automate WordPerfect

  1. PublishPDF: convert WP documents to PDF
  2. SaveAs: convert WP documents to HTML
  3. Type: type and format text
  4. CapsLock: use MS Word or WordPerfect to check the CapsLock status

Corel WordPerfect Office Suites come with their own macro scripting language.

However, it is possible to use VBScript to automate WordPerfect through OLE.

The following code demonstrates some of WordPerfect's OLE automation capabilities.

 

PublishPDF
Use Corel WordPerfect to convert WP documents to PDF
VBScript Code:
Option Explicit

Dim blnExitWP, colItems, strMsg, strPDFDoc, strWPDoc
Dim objFSO, objItem, objWMIService, objWP

strMsg = ""

' Open a FileSystem Object
Set objFSO = CreateObject( "Scripting.FileSystemObject" )

' Parse the command line arguments
With WScript.Arguments
    If .Named.Count > 0 Then Syntax
    Select Case .Unnamed.Count
        Case 1
            strWPDoc  = .Unnamed(0)
            ' No PDF file name specified, so we'll take the location and
            ' file name of the WordPerfect document and append a PDF extension
            strPDFDoc = objFSO.BuildPath( objFSO.GetParentFolderName( strWPDoc ), _
                                          objFSO.GetBaseName( strWPDoc ) & ".pdf" )
        Case 2
            strWPDoc  = .Unnamed(0)
            strPDFDoc = .Unnamed(1)
        Case Else
            Syntax
    End Select
End With

' Check if the WordPerfect file exists
If Not objFSO.FileExists( strWPDoc ) Then
    strMsg = "ERROR:  File """ & strWPDoc & """ not found" & vbCrLf & vbCrLf
    Syntax
End If

' Check if WordPerfect is already active by
' searching for a process named WPWIN**.EXE
blnExitWP = True
Set objWMIService = GetObject( "winmgmts://./root/cimv2" )
Set colItems = objWMIService.ExecQuery( "SELECT * FROM Win32_Process" )
For Each objItem In colItems
    If Left( UCase( objItem.Name ), 5 ) = "WPWIN" And _
       Right( UCase( objItem.Name ), 4 ) = ".EXE" And _
       Len( objItem.Name ) < 12 Then blnExitWP = False
Next
Set objWMIService = Nothing

' Create a new WP OLE Automation object
Set objWP = CreateObject( "WordPerfect.PerfectScript" )

With objWP
    ' Open the specified document
    .FileOpen( strWPDoc )

    ' Publish to PDF
    .PdfDlg( strPDFDoc )

    ' Close the document
    .Close

    ' Close WordPerfect unless it was already active
    If blnExitWP Then .ExitWordPerfect
End With

' Release the objects
Set objFSO = Nothing
Set objWP  = Nothing


Sub Syntax( )
    strMsg = strMsg & vbCrLf _
           & WScript.ScriptName & ",  Version 1.00" & vbCrLf _
           & "Convert a WordPerfect document to Adobe PDF" & vbCrLf & vbCrLf _
           & "Usage:  " & UCase( WScript.ScriptName ) _
           & "  wpdoc_filename  [ pdf_filename ]" & vbCrLf & vbCrLf _
           & "Where:  ""wpdoc_filename""  is the WP file to be converted" _
           & vbCrLf _
           & "        ""pdf_filename""    is the name for the PDF file" _
           & vbCrLf _
           & "                          " _
           & "(default is name of WP file with .PDF extension)" _
           & vbCrLf & vbCrLf _
           & "Written by Rob van der Woude" & vbCrLf _
           & "http://www.robvanderwoude.com"
    WScript.Echo strMsg
    WScript.Quit(1)
End Sub
Requirements:
Windows version: any
Network: any
Client software: Corel WordPerfect
Script Engine: any
Summarized: Works in any Windows version, as long as Corel WordPerfect is installed.
Requires a WordPerfect version that has "Publish To", "PDF" menu entries (tested in Corel WordPerfect 12).
 
[Back to the top of this page]
 
SaveAs
Use Corel WordPerfect to convert WP documents to HTML
VBScript Code:
WP2HTML "C:\Documents and Settings\MyUserID\My Documents\resume.wpd"

Sub WP2HTML( myFile )
' This subroutine opens a WordPerfect document,
' then saves it as HTML, and closes WordPerfect.
' If the HTML file exists, the subroutine will
' prompt for overwrite.
' If WordPerfect was already active, the subroutine
' will prompt the user to save the changes in other
' documents.
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com

    ' Standard housekeeping
    Dim objFSO, objWP, objWPFile, strHTMLFile, strWPFile

    Const WordPerfect_6_7_8_FileOpen_Format = 4
    Const HTML_FileSave_ExportType          = 226
    Const No_FileSave_Overwrite             = 0
    Const Prompt_FileSave_Overwrite         = 2
    Const Yes_FileSave_Overwrite            = 1

    ' Create a File System object
    Set objFSO = CreateObject( "Scripting.FileSystemObject" )
    ' Create a WordPerfect OLE Automation object
    Set objWP = CreateObject( "WordPerfect.PerfectScript" )
    With objWP
        ' Check if the WordPerfect file exists
        If objFSO.FileExists( myFile ) Then
            Set objWPFile = objFSO.GetFile( myFile )
        Else
            WScript.Echo "FILE OPEN ERROR: The file does not exist" & vbCrLf
            ' Close WordPerfect
            .ExitWordPerfect
            Exit Sub
        End If
        strWPFile   = objWPFile.Path
        strHTMLFile = objFSO.BuildPath( objWPFile.ParentFolder, _
                      objFSO.GetBaseName( objWPFile ) & ".html" )

        ' Maximize the window
        .AppMaximize
        ' Open the document
        On Error Resume Next
        .FileOpen strWPFile, WordPerfect_6_7_8_FileOpen_Format
        If Err Then
            WScript.Echo "FILE OPEN ERROR: " & Err.Number & vbCrLf _
                       & Err.Description & vbCrLf
            Err.Clear
            ' Close WordPerfect
            .ExitWordPerfect
            Exit Sub
        End If
        ' Save the document as HTML file
        .FileSave strHTMLFile, HTML_FileSave_ExportType, Prompt_FileSave_Overwrite
        If Err Then
            WScript.Echo "FILE SAVE AS ERROR: " & Err.Number & vbCrLf _
                       & Err.Description & vbCrLf
            Err.Clear
        End If
        ' Close WordPerfect
        .ExitWordPerfect
        If Err Then
            WScript.Echo "PROGRAM CLOSE ERROR: " & Err.Number & vbCrLf _
                       & Err.Description & vbCrLf
            Err.Clear
        End If
        On Error Goto 0
    End With

    ' Release the object
    Set objWP = Nothing
End Sub
Requirements:
Windows version: any
Network: any
Client software: Corel WordPerfect
Script Engine: any
Summarized: Works in any Windows version, as long as Corel WordPerfect is installed.
 
[Back to the top of this page]
 
Type
Write and format text
VBScript Code:
' This script opens WordPerfect, creates a new document,
' types some text and changes text attributes on selections.
'
' Tested with Corel WordPerfect 12 only.
'
' Based on the article "The Cutting Edge: Using OLE
' Automation to Control WordPerfect" by Gordon McComb
' gmccomb.com/vault/edge/ole.html
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com

Option Explicit

Dim objWP

' Create a new WP OLE Automation object
Set objWP = CreateObject( "WordPerfect.PerfectScript" )

With objWP
    ' Maximize the window
    .AppMaximize

    ' Open a new document; otherwise the
    ' currently active document is used!
    .FileNew

    ' Set the new default font and size
    .Font = "Times New Roman"
    .FontSize = 200 ' 200 = 12pt

    ' Type some text, and center the line
    .Center
    .Type "This is normal text, 12pt Times New Roman, centered" & vbLf & vbLf

    ' Repeat, but select and format the text
    .Center
    .Type "This is Bold, Extra Large text, centered" & vbLf
    .MoveUp
    .SelectLineEnd
    .MoveLeft
    .FontExtraLargeToggle
    .BoldKey
    .SelectOff
    .MoveRight
    .Type vbLf

    ' Repeat, this time without centering
    .Type "This is normal text, left aligned, 18pt Arial" & vbLf
    .MoveUp
    .SelectLineEnd
    .MoveLeft
    .FontSize = 300 ' 300 = 18pt
    .Font = "Arial"
    .SelectOff
    .MoveRight
    .Type vbLf

    ' Once more
    .Type "This is 8pt Comic Sans MS, italics" & vbLf
    .MoveUp
    .SelectLineEnd
    .MoveLeft
    .Font = "Comic Sans MS"
    .FontSize = 133 ' 133 = 8pt
    .FontItalicToggle
    .SelectOff
    .MoveRight

    ' And again
    .Type vbLf & "Normal text, underlined" & vbLf
    .MoveUp
    .SelectLineEnd
    .MoveLeft
    .FontUnderlineToggle
    .SelectOff
    .MoveRight
    .Type vbLf

    ' Open the File Save As dialog and quit
    .FileSaveAsDlg
    .Quit
End With

' Release the object
Set objWP = Nothing

WScript.Echo "Done."
Requirements:
Windows version: any
Network: any
Client software: Corel WordPerfect
Script Engine: any (except of course the WScript.Echo command at the end)
Summarized: Works in any Windows version, as long as Corel WordPerfect is installed.
 
[Back to the top of this page]

 

Note the use of vbLf instead of vbCrLf when typing text in WordPerfect.
vbCrLf would result in a space before each linefeed.

These typing demo scripts can be downloaded here.
The downloadable versions also contain other file filter constants.


page last modified: 2022-10-25; loaded in 0.0018 seconds