Ошибка при печати файла, но не при печати веб-страницы - PullRequest
0 голосов
/ 06 апреля 2011

Используя объект InternetExplorer , я могу напечатать веб-страницу.Но когда я пытаюсь напечатать файл, я получаю следующую ошибку:

System.Runtime.InteropServices.COMException: вызванный объект отключен от своих клиентов.Что я делаю не так?

Вот код (благодаря этот ответ ):

public class Program
{
    static void main(string[] args)
    {
        // this works
        (new HTMLPrinter()).Print("www.google.com");

        // this brings up the file in a browser 
        // but then throws an exception when checking print status!
        (new HTMLPrinter()).Print("C:\Temp\test.html");  
    }
}

public class HTMLPrinter
{
    private bool documentLoaded;
    private bool documentPrinted;

    private void ie_DocumentComplete(object pDisp, ref object URL)
    {
        documentLoaded = true;
    }

    private void ie_PrintTemplateTeardown(object pDisp)
    {
        documentPrinted = true;
    }

    public void Print(string htmlFilename)
    {
        documentLoaded = false;
        documentPrinted = false;

        InternetExplorer ie = new InternetExplorerClass();
        ie.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(ie_DocumentComplete);
        ie.PrintTemplateTeardown += new DWebBrowserEvents2_PrintTemplateTeardownEventHandler(ie_PrintTemplateTeardown);

        object missing = Missing.Value;

        ie.Navigate(htmlFilename, ref missing, ref missing, ref missing, ref missing);

        // An exception is thrown at 
        // SHDocVw.InternetExplorerClass.QueryStatusWB
        // only when trying to print a file!
        // Exception details: System.Runtime.InteropServices.COMException:
        // The object invoked has disconnected from its client.
        while (!documentLoaded && ie.QueryStatusWB(OLECMDID.OLECMDID_PRINT) != OLECMDF.OLECMDF_ENABLED)
            Thread.Sleep(100);        

        ie.ExecWB(OLECMDID.OLECMDID_PRINT, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref missing, ref missing);
        while (!documentPrinted)
            Thread.Sleep(100);

        ie.DocumentComplete -= ie_DocumentComplete;
        ie.PrintTemplateTeardown -= ie_PrintTemplateTeardown;
        ie.Quit();
    }
}

1 Ответ

0 голосов
/ 06 апреля 2011

Запустите программу от имени администратора.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...