0

In c# .Net 4.0 I am attempting to automate WordPerfect.

To do this I add a reference in my project to the wpwin14.tlb file that lives in the WordPerfect program folder.

That has the effect of creating the COM interfaces within my project.

Next I should be able to write code that instantiates a WordPerfect.PerfectScript object that I can use to automate WordPerfect.

However, when I try to instantiate the WordPerfect.PerfectScript object c# throws the error:

"Unable to cast COM object of type 'System.__ComObject' to interface type 'WordPerfect.PerfectScript'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{C0E20006-0004-1000-0001-C0E1C0E1C0E1}' failed due to the following error: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)."

The thing to zero in on in that message (I do believe) is that the RPC server is unavailable.

I have tried this with WordPerfect running in the background and without. And I have gone to my services and made sure that RPC services were all running and restarting everything.

Is it possible that I am getting blocked by a firewall? That is my only faintest guess

GrAnd
  • 10,141
  • 3
  • 31
  • 43
Chad
  • 3
  • 1

2 Answers2

1

I just wrap it as an OLE call and clean up my COM object with FinalReleaseComObject.

Here's a simple wrapper class I've been using to open Wp docs and convert them to pdf. It cleans up nicely in our automated process:

public class WpInterop : IDisposable
{
    private bool _disposed;
    private PerfectScript _perfectScript;

    public PerfectScript PerfectScript
    {
        get
        {
            if (_perfectScript == null)
            {
                Type psType = Type.GetTypeFromProgID("WordPerfect.PerfectScript");
                _perfectScript = Activator.CreateInstance(psType) as PerfectScript;
            }
            return _perfectScript;
        }
    }

    protected void Dispose(bool disposing)
    {
        if (disposing)
        {
            Marshal.FinalReleaseComObject(_perfectScript);
        }
        _disposed = true;
    }

    public void Dispose()
    {
        if (_disposed == false)
        {
            GC.SuppressFinalize(this);
            Dispose(true);
        }
    }
}
AlexB
  • 78
  • 4
  • Confused as to why this would work any better than a direct call. But I will soon try it in my projects, especially given the tendency of users to not update their software. Do you know of any drawbacks to this method? – Ric Gaudet Mar 10 '15 at 04:36
  • @RicGaudet Yes, the drawback is that it's tied to that particular .tlb. If you wanted to use this with another .tlb, you could use dynamic or object as the returning type for PerfectScript and then cast it in the calling code. My office works with older WordPerfect versions (X3, X4) and this has been the most headache-free way without writing VB. – AlexB Mar 10 '15 at 12:22
0

Make sure your version of WordPerfect has all of the service packs and hot fixes installed. This step has fixed many random-sounding issues for me over the years. Looks like you are using X4, which is no longer supported by Corel, which means that the updates are no longer on its web site. You should be running version 14.0.0.756 (SP2 plus 2 hotfixes).

I just uninstalled WPX4 and re-installed it, without the service pack updates. Running this code gave the exact error as the OP:

using System.Runtime.InteropServices;
using WordPerfect;

namespace WP14TLB
{


class Program
{
    static void Main(string[] args)
    {

        PerfectScript ps = new PerfectScript();
        ps.WPActivate();
        ps.KeyType("Hello WP World!");

        Marshal.ReleaseComObject(ps);
        ps = null;

    }
}
}

Installing the service packs "magically" fixed the problem.

BTW, for future reference, you can also try the WPUniverse forums. There are quite a few WP experts who regularly answer difficult questions.

There is also a link to the X4 updates here:

Ric Gaudet
  • 898
  • 6
  • 16
  • Thank you for your response. This did in fact work but I picked the other solution because I have many users and I don't want to have to ensure that their software is up-to-date. The other solution works regardless of the version of WordPerfect being used. – Chad Mar 09 '15 at 19:53