我在备忘录字段中将一些 html字符串存储在 Microsoft Access 2013 x64中构建的表格数据库中。
我在 Native WebBrowser控件(Access 2010/2013)中显示这些html字符串。
我想使用 head 的样式元素将简单的格式(字体系列,字体大小等)应用于这些html文档文件的一部分。
我已尝试以下内容:
首先,我尝试直接在字符串:
中添加head部分' strModified is just the body of a modified html document.
strModified = "<HTML><HEAD><style> " & _
"html *{" & _
"font-family:Calibri !important;" & _
"font-size: 20pt !important;" & _
"} " & _
"</style></HEAD>" & strModified & "</HTML>"
接下来,我尝试直接在头部创建样式节点。我成功了,但没有找到将 innerText 传递给它的方法:
Dim html As HTMLDocument
Dim elc As HTMLHtmlElement
Set html = Me.WebBrowser2.Object.Document
With html
Set elc = .createElement("style")
' elc.innerText = "html *{ " & _
"font-family:Calibri !important;" & _
"font-size: 9pt !important" & _
"}"
.Head.appendChild elc
End With
Set html = Nothing
最后,我每次显示文档更改时都尝试设置头部样式:
Private Sub cboDescriptions_AfterUpdate()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("tblDescriptions")
With rst
.Index = "PrimaryKey"
.Seek "=", Me.cboDescriptions
Dim html As HTMLDocument
Set html = Me.WebBrowser2.Object.Document
With html
Do While .ReadyState <> "complete": DoEvents: Loop
.Head.Style.fontFamily = "Verdana"
.Head.Style.FontSize = 20
.body.innerHTML = rst.Fields(2)
End With
Set html = Nothing
End With
Set rst = Nothing
Set dbs = Nothing
End Sub
我想问一下如何将头部样式添加到 html字符串以显示在 WebBrowser控件(本机或ActiveX)中的 MSACCESS
提前致谢,
迭
答案 0 :(得分:2)
好吧,我决定使用ActiveX WebBrowser控件而不是Access 2010/2013的本机WebBrowser,因为前者接受html字符串并使用文档的write属性正确格式化文档。
With Me.WebBrowser6.Object
.Navigate2 "about:blank"
.Document.write rst.Fields(2)
.Refresh
End With