Skip to content

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:

vb
Option Compare Database

Dim wv As New cWebView2Host

Private Sub Form_Load()
    wv.Initialize Me.Hwnd, "https://vb6.pro"
End Sub

Steps:

  1. Open BaseAccess.mdb
  2. Create a blank form, switch to Design View
  3. Press Alt+F11 to open the VBA editor
  4. Paste the above code in the form module
  5. 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:

vb
Dim wv As New cWebView2Host

Private Sub UserForm_Initialize()
    wv.Initialize Me.Frame1, "https://vb6.pro"
End Sub

Steps:

  1. Open BaseExcel.xlsm
  2. Press Alt+F11 to open the VBA editor
  3. Insert a UserForm, add a Frame control (named Frame1)
  4. Paste the above code in the UserForm code module
  5. Run the UserForm

Feature Description

  1. Access Form Embedding

    • Uses Me.Hwnd to get the Access window handle
    • cWebView2Host auto-detects OForm class name and switches to MessageWindowAdapter
    • Works without any additional configuration
  2. Excel UserForm Embedding

    • Uses Me.Frame1 to pass the Frame control object
    • Initialize accepts VBA objects and auto-gets hWnd
    • WebView2 displays within the Frame control area
  3. Simplified VBA Declaration

    • Uses Dim wv As New cWebView2Host for auto-instantiation
    • VBA's New keyword auto-creates the object on first access
    • Completely consistent with VB6 usage

Technical Notes

  1. COM Interop: cWebView2Host is a COM-creatable class; after registering vbman2_win32.dll, any COM-capable host can use it

  2. 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

  3. 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 controls

  4. DLL Registration: Before use, you must register vbman2_win32.dll on the target machine:

    regsvr32 vbman2_win32.dll
  5. Access 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

  1. Access: Embed web reports, online documents, or data visualization charts in database forms
  2. Excel: Embed real-time data panels, web dashboards, or interactive charts in spreadsheets
  3. Word / PowerPoint: Same COM registration approach works in any Office VBA environment

Extension Suggestions

  1. In Access, use BindUI/BindData to link web forms with database fields
  2. In Excel, use JsRun to push worksheet data to WebView2 for chart rendering
  3. Use EnvironmentOptions.UserDataFolder to persist login state and avoid repeated logins
  4. For event handling, use Dim WithEvents wv As cWebView2Host + Set wv = New cWebView2Host declaration instead

VB6 and LOGO copyright of Microsoft Corporation