Buy Download Tour

Here's an example of how to use TextPipe's command line interface from within a Microsoft Word macro:

'wait for single object code
Const SYNCHRONIZE = &H100000
'Wait forever
Const INFINITE = &HFFFF


'Windows functions that we need to call - must be at top of file
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _
            ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, _
            ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long


Private Sub waitProcess(commandLine As String)

' The WaitForSingleObject function returns when one of the following occurs:
' - The specified object is in the signaled state.
' - The time-out interval elapses.
'
' The dwMilliseconds parameter specifies the time-out interval, in milliseconds.
' The function returns if the interval elapses, even if the object’s state is
' nonsignaled. If dwMilliseconds is zero, the function tests the object’s state
' and returns immediately. If dwMilliseconds is INFINITE, the function’s time-out
' interval never elapses.
'
' This example waits an INFINITE amount of time for the process to end. As a
' result this process will be frozen until the shelled process terminates. The
' down side is that if the shelled process hangs, so will this one.
'
' A better approach is to wait a specific amount of time. Once the time-out
' interval expires, test the return value. If it is WAIT_TIMEOUT, the process
' is still not signaled. Then you can either wait again or continue with your
' processing.
'
' DOS Applications:
' Waiting for a DOS application is tricky because the DOS window never goes
' away when the application is done. To get around this, prefix the app that
' you are shelling to with "command.com /c".
'
' For example: lPid = Shell("command.com /c " & commandLine, vbNormalFocus)
'
    Dim lPid As Long
    Dim lHnd As Long
    Dim lRet As Long
    
    If Trim$(commandLine) = "" Then Exit Sub
    
    'can also use vbNormalFocus instead of vbHide
    lPid = Shell(commandLine, vbHide)
    If lPid <> 0 Then
            'Get a handle to the shelled process.
            lHnd = OpenProcess(SYNCHRONIZE, 0, lPid)
            'If successful, wait for the application to end and close the handle.
            If lHnd <> 0 Then
                    lRet = WaitForSingleObject(lHnd, INFINITE)
                    CloseHandle (lHnd)
            End If
            'uncomment this line for debugging
            'MsgBox "Just terminated.", vbInformation, "Shelled Application"
    End If
End Sub



Sub TextPipeCommandLineExample()
'
' Macro by Simon Carter, DataMystic
' Demonstrates running TextPipe on a Word Document using the
'  Command Line
'

    Dim TaskId
    Dim TextPipePath
    Dim FilterName
    Dim pathname
    
    'select the entire document
    Selection.WholeStory
    'copy the entire document to the clipboard
    Selection.Copy
       
    'TextPipe's location - we must use double quotes because of spaces in the name
    TextPipePath = """c:\Program Files\TextPipe\textpipe.exe"""

    'The filter to run - we must use double quotes because of spaces in the name
    'Because we copy the whole document to the clipboard, this filter must be
    'set to process the clipboard
    FilterName = """/f=c:\Program Files\TextPipe\cleantext.fll"""
    
    'run TextPipe via the command line and wait for it to finish
    waitProcess( TextPipePath & " " & FilterName & " /g /q " )
    
    'paste the result in, overwriting the original
    Selection.WholeStory
    Selection.Paste

End Sub

Did you know? You can generate a command line for TextPipe using Tools Menu\Command line wizard.

Buy Download Tour