You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`stdWebView` embeds the Microsoft Edge **WebView2** control in a Win32 parent window. It is aimed at **Excel VBA UserForms**: host the browser inside an `MSForms.Frame` (or any HWND you obtain) to show HTML, open sites, and run JavaScript with optional async callbacks.
4
+
5
+
**Platform:** Windows only. Requires the [WebView2 Runtime](https://developer.microsoft.com/en-us/microsoft-edge/webview2/) and `WebView2Loader.dll` (same loader used by the WebView2 SDK) available to the host process.
6
+
7
+
**Implementation note:** Environment and controller callbacks are implemented with in-process vtables and executable thunks (similar patterns appear elsewhere in stdVBA). See the class header in `src/stdWebView.cls` for attribution to prior WebView2/VBA work.
8
+
9
+
## Spec
10
+
11
+
### Constructors
12
+
13
+
#### `CreateFromHwnd(ByVal hwnd As LongPtr, Optional ByVal OnReady As stdICallable = Nothing) As stdWebView`
14
+
15
+
Creates a `stdWebView` bound to an existing window handle. The WebView fills the **client area** of that window.
16
+
17
+
`OnReady`, if provided, is invoked once the controller and `CoreWebView2` are ready. The callback is called via `stdICallable.RunEx` with a single-element array: the `stdWebView` instance (`Array(Me)`).
Construction is **synchronous from the caller’s perspective**: the factory polls `DoEvents` until initialization finishes or times out (raises `WebView2 initialization failed or timed out`).
25
+
26
+
#### `CreateFromFrame(ByVal frm As Object, Optional ByVal OnReady As stdICallable = Nothing) As stdWebView`
27
+
28
+
Same as `CreateFromHwnd`, but resolves the HWND from an **`MSForms.Frame`**. Raises if `frm` is not a `Frame`.
Returns the document’s `document.documentElement.outerHTML`. Uses an internal synchronous script; requires `IsReady`.
42
+
43
+
#### `Html(ByVal rhs As String)` (Let)
44
+
45
+
Loads HTML into the view via WebView2’s string navigation (`NavigateToString`). Requires `IsReady`.
46
+
47
+
### Instance methods
48
+
49
+
#### `IsReady() As Boolean`
50
+
51
+
`True` when `CoreWebView2` is available. `Navigate`, `Html`, `JavaScriptRun`, and `JavaScriptRunSync` require a ready view.
52
+
53
+
#### `Quit()`
54
+
55
+
Tears down the controller reference and frees internal handler allocations. Safe to call when already shut down.
56
+
57
+
#### `Navigate(ByVal url As String)`
58
+
59
+
Navigates to a URL. Requires `IsReady`.
60
+
61
+
#### `Back()` / `Forward()`
62
+
63
+
History navigation implemented by running `history.back()` / `history.forward()` synchronously in the page.
64
+
65
+
#### `JavaScriptRunSync(ByVal script As String) As String`
66
+
67
+
Executes `script` in the page context and **blocks** until the result is delivered, pumping messages with `DoEvents`.
68
+
69
+
* Only **one** synchronous script may run at a time; a second call raises.
70
+
* The return value is the **JSON-encoded** result string from the WebView2 script API (e.g. quoted strings, `null`, numbers as JSON). Parse or unwrap as needed.
71
+
72
+
```vb
73
+
Debug.Printwv.JavaScriptRunSync("document.title")' e.g. returns JSON string including quotes
74
+
```
75
+
76
+
#### `JavaScriptRun(ByVal script As String, Optional ByVal callback As stdICallable = Nothing)`
77
+
78
+
Queues script execution without blocking. If `callback` is set, it is invoked with `RunEx(Array(errorCode, resultJson))` when execution completes.
`protCreate`, `protEnvCompleted`, `protCtrlCompleted`, and `protScriptCompleted` are **not** part of the public contract; they exist for the COM callback thunks. Do not call them from application code.
104
+
105
+
## stdVBA developer notes
106
+
107
+
* A per-instance user data folder is created under `%TEMP%` (`stdWebView_*`) for the WebView2 profile.
108
+
* If `CreateCoreWebView2EnvironmentWithOptions` fails, the error is raised with the HRESULT from the loader.
109
+
*`zzProtWebView_*` entry points must remain `Public` so thunk code can dispatch into the instance; they are not user APIs.
'@returns - Current document outer HTML (`document.documentElement.outerHTML`).
438
+
'@remark - Raises if the WebView is not ready.
380
439
PublicPropertyGet Html() AsString
381
440
IfNot IsReady Then Err.Raise 5, "stdWebView::Html [Get]", "WebView not ready"
382
441
Html = JsonUnquoteString(JavaScriptRunSync("(function(){try{return document.documentElement?document.documentElement.outerHTML:'';}catch(e){return '';}})()"))
383
442
End Property
384
443
444
+
'Set the current document outer HTML.
445
+
'@param rhs - HTML content loaded with WebView2 `NavigateToString`.
446
+
'@remark - Raises if the WebView is not ready.
385
447
PublicPropertyLet Html(ByVal rhs AsString)
386
448
IfNot IsReady Then Err.Raise 5, "stdWebView::Html [Let]", "WebView not ready"
0 commit comments