"System.Dynamic - Invalid argument" on second and succeeding Document.ExecCommand

0

Have a WPF app using the WebBrowser control. It uses Document.ExecCommand() to copy the HTML of the displayed page to the clipboard and then stores that in a variable:

XAML

<WebBrowser x:Name="browser"... />

Code-behind

browser.Source = new Uri(selectedUrl);

dynamic document = browser.Document;
document.ExecCommand("SelectAll", true, null);
document.ExecCommand("Copy", true, null);
document.ExecCommand("Unselect", true, null);

IDataObject iDataObj = Clipboard.GetDataObject();
var content = iDataObj.GetData(DataFormats.Html).ToString();

This all works fine the first time. But after navigating to a second URL within the same session, on the Document.ExecCommand("SelectAll", true, null), it throws an exception of

{"Invalid argument."}
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2146827850 (0x800A01B6)
    HelpLink: null
    InnerException: null
    Message: "Invalid argument."
    Source: "System.Dynamic"
    StackTrace:
        at System.Dynamic.ComRuntimeHelpers.CheckThrowException(Int32 hresult, ExcepInfo& excepInfo, UInt32 argErr, String message)
        at CallSite.Target(Closure , CallSite , ComObject , String , Boolean , Object )
        at CallSite.Target(Closure , CallSite , Object , String , Boolean , Object )
        ...
    TargetSite: {Void CheckThrowException(Int32, System.Dynamic.ExcepInfo ByRef, UInt32, System.String)}

However, this doesn't happen all the time. One time I can test and it works on the first and every other page I bring up after that. The next time I test, it throws the exception.

c#
wpf
webbrowser-control
execcommand
asked on Stack Overflow Mar 2, 2017 by IronRod • edited Mar 2, 2017 by IronRod

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0