WebView2 in Excel Advance Pane error after Install

Add-in Express™ Support Service
That's what is more important than anything else

WebView2 in Excel Advance Pane error after Install
WebView2 
Glen Lewis




Posts: 29
Joined: 2021-03-12
I have added a WebView2 control I added to the Excel Advance Pane. It works perfectly when I run the project from Visual studio. Once I setup the installer and install it. The Webview2 Throws and exception error.



************** Exception Text **************
System.InvalidCastException: Specified cast is not valid.
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)
at Microsoft.Web.WebView2.Core.CoreWebView2Environment.<CreateAsync>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at ExcelFinanceAddIn.ADXRegisterApiPane.<InitializeWebView2Async>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()



Code I use to init the WebView2 Control.



        private void ADXRegisterApiPane_Load(object sender, EventArgs e)
        {
            InitializeWebView2Async(portalWebBrowser);
	}

        private async void InitializeWebView2Async(WebView2 wv, string initialSource = "", string webCacheDir = "")
        {
            //set value
            string tempWebCacheDir = webCacheDir;

            //if user didn't specify a webCacheDir, 
            //use the user's temp folder
            if (String.IsNullOrEmpty(tempWebCacheDir))
            {
                //get fully-qualified path to user's temp folder
                tempWebCacheDir = System.IO.Path.GetTempPath();
            }//if
            tempFolder.Text = tempWebCacheDir;
            CoreWebView2EnvironmentOptions options = null;
            //options = new CoreWebView2EnvironmentOptions("--disk-cache-size=200");
            //options = new CoreWebView2EnvironmentOptions("?Â?Ð?ãincognito ");

            //set WebView2 control cache/data folder. The cache/data folder is used to 
            //store WebView2 cached objects. If not specified, the folder where the executable
            //was started will be used. If the user doesn't have write permissions
            //on that folder, such as C:Program Files<your application folder>,
            //an exception will be thrown 

            CoreWebView2Environment webView2Environment = null;
            //webView2Environment = await CoreWebView2Environment.CreateAsync(@"C:Program Files (x86)MicrosoftEdge DevApplication85.0.564.8", tempDir2, options);
            webView2Environment = await CoreWebView2Environment.CreateAsync(null, tempWebCacheDir, options);

            //CoreWebView2 must be inialized before it can be used
            //wait for CoreWebView2 initialization
            //when complete, CoreWebView2Ready event will be thrown
            await wv.EnsureCoreWebView2Async(webView2Environment);

            //set source to 'about:blank'
            wv.Source = new Uri("about:blank", UriKind.Absolute);

            if (!String.IsNullOrEmpty(initialSource))
            {
                //Navigate to URL
                //Debug.Print("Navigating to URL...");
                WebsiteNavigate(wv, initialSource);
            }
        }

        public void WebsiteNavigate(WebView2 wv, string dest)
        {
            string tempDest = dest;

            if (wv != null && wv.CoreWebView2 != null)
            {
                if (!String.IsNullOrEmpty(dest))
                {
                    if (dest != "about:blank" &&
                        !dest.StartsWith("edge://") &&
                        !dest.StartsWith("file://") &&
                        !dest.StartsWith("http://") &&
                        !dest.StartsWith("https://"))
                    {
                        //URL must start with one of the specified strings
                        //if not, pre-pend with "http://" or "https://"
                        //Debug.Print("Prepending "http://" to URL.");

                        //set value
                        tempDest = "http://" + dest;
                    }

                    //option 1
                    wv.CoreWebView2.Navigate(tempDest);

                    //option 2
                    //wv.Source = new Uri(tempDest, UriKind.Absolute);
                }
            }
        }



What I cannot figure out is why it is failing when I install it and launch excel, but works when I am in debug?
Posted 06 May, 2021 11:02:53 Top
Andrei Smolin


Add-in Express team


Posts: 18829
Joined: 2006-05-11
Hello Glen,

Does it work if you comment out the initialization code completely?


Andrei Smolin
Add-in Express Team Leader
Posted 07 May, 2021 02:36:06 Top
Glen Lewis




Posts: 29
Joined: 2021-03-12
I don't get an error, but the WebView2 doesn't load.
Posted 11 May, 2021 10:45:47 Top
Andrei Smolin


Add-in Express team


Posts: 18829
Joined: 2006-05-11
Hello Glen,

We can't reproduce this issue using the add-in project at http://temp.add-in-express.com/support/MyAddin2-GlenLewis.zip.


Andrei Smolin
Add-in Express Team Leader
Posted 17 May, 2021 03:08:24 Top