Search code examples
c#-4.0comwordperfect

WordPerfect COM Automation Error


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


Solution

  • 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);
            }
        }
    }