WebView2 VBA Demo - Using cWebView2Host in Access and Excel
Overview
Demonstrates how to use cWebView2Host in VBA host environments (Microsoft Access and Microsoft Excel). cWebView2Host is a COM-creatable class that naturally supports being called from any VBA host, without requiring the VB6 runtime.
Project Structure
Download Source [ Note: Re-register the DLL file in the bin directory ]
Source code is available in the VBMAN2 project's demos/webview2/_vbaSamples directory.
_vbaSamples/
├── BaseAccess.mdb # Access database file (containing forms and VBA code)
└── BaseExcel.xlsm # Excel macro-enabled workbook (containing UserForm and VBA code)Core Code Walkthrough
1. Access Form (BaseAccess.mdb)
In Access, create a blank form, open the VBA editor, and paste the following code:
Option Compare Database
Dim wv As New cWebView2Host
Private Sub Form_Load()
wv.Initialize Me.Hwnd, "https://vb6.pro"
End SubSteps:
- Open BaseAccess.mdb
- Create a blank form, switch to Design View
- Press Alt+F11 to open the VBA editor
- Paste the above code in the form module
- Switch to Form View to run
2. Excel UserForm (BaseExcel.xlsm)
In Excel, create a UserForm, add a Frame control (named Frame1), and paste the following into the code module:
Dim wv As New cWebView2Host
Private Sub UserForm_Initialize()
wv.Initialize Me.Frame1, "https://vb6.pro"
End SubSteps:
- Open BaseExcel.xlsm
- Press Alt+F11 to open the VBA editor
- Insert a UserForm, add a Frame control (named Frame1)
- Paste the above code in the UserForm code module
- Run the UserForm
Feature Description
Access Form Embedding
- Uses
Me.Hwndto get the Access window handle - cWebView2Host auto-detects OForm class name and switches to MessageWindowAdapter
- Works without any additional configuration
- Uses
Excel UserForm Embedding
- Uses
Me.Frame1to pass the Frame control object - Initialize accepts VBA objects and auto-gets hWnd
- WebView2 displays within the Frame control area
- Uses
Simplified VBA Declaration
- Uses
Dim wv As New cWebView2Hostfor auto-instantiation - VBA's
Newkeyword auto-creates the object on first access - Completely consistent with VB6 usage
- Uses
Technical Notes
COM Interop: cWebView2Host is a COM-creatable class; after registering vbman2_win32.dll, any COM-capable host can use it
Access Auto-Adaptation: When cWebView2Host detects Access's OForm window class name, it automatically uses MessageWindowAdapter (message window + timer polling), avoiding crashes from subclassing the Access window
Excel UserForm with Frame: The UserForm itself can also directly pass hWnd (
Me.hWnd); passing a Frame control restricts WebView2 to the Frame area, coexisting with other controlsDLL Registration: Before use, you must register vbman2_win32.dll on the target machine:
regsvr32 vbman2_win32.dllAccess Limitations: MessageWindowAdapter does not support host mouse/keyboard events (HostMouse*, HostKeyDown*, etc.), but WebView2 content area events and BindUI/BindData are fully functional
Use Cases
- Access: Embed web reports, online documents, or data visualization charts in database forms
- Excel: Embed real-time data panels, web dashboards, or interactive charts in spreadsheets
- Word / PowerPoint: Same COM registration approach works in any Office VBA environment
Extension Suggestions
- In Access, use BindUI/BindData to link web forms with database fields
- In Excel, use JsRun to push worksheet data to WebView2 for chart rendering
- Use
EnvironmentOptions.UserDataFolderto persist login state and avoid repeated logins - For event handling, use
Dim WithEvents wv As cWebView2Host+Set wv = New cWebView2Hostdeclaration instead