我有一个关于更改memofield的问题"纯文本"到#34;富文本"使用VBScript,我在这里和互联网上找到了一些解决方案,但所有解决方案都是针对访问中的VBScript。我尝试通过Windows启动vbscript,但我的脚本不起作用。我对VBScripting很陌生,所以我希望你们能帮助我。我在论坛中使用了一个示例作为我的脚本: How to convert a text field in an Access table to a rich text memo using VBA
我的剧本:
Dim db
Dim tdf
Dim fld1
Dim fld2
Set accessApp = GetObject("D:\test.mdb")
Set accessApp = CreateObject("Access.Application")
accessApp.OpenCurrentDataBase "D:\test.mdb", true
accessApp.visible = false
accessApp.UserControl = true
Set accessApp.db = CurrentDB
Set accessApp.tdf = db.TableDefs("Database")
Set accessApp.fld1 = tdf.Fields("Name_Memofield1")
Set accessApp.fld2 = tdf.Fields("Name_Memofield2")
Debug.Print "acTextFormatPlain: " & acTextFormatPlain & _
"; acTextFormatHTMLRichText: " & acTextFormatHTMLRichText
With fld1.Properties("TextFormat")
Debug.Print "TextFormat: " & .Value
If .Value = acTextFormatPlain Then
.Value = acTextFormatHTMLRichText
Debug.Print "TextFormat changed to: " & .Value
End If
End With
With fld2.Properties("TextFormat")
Debug.Print "TextFormat: " & .Value
If .Value = acTextFormatPlain Then
.Value = acTextFormatHTMLRichText
Debug.Print "TextFormat changed to: " & .Value
End If
End With
发生的错误告诉我问题出在"设置accessApp.db = CurrentDB"发生的错误是:"对象不支持此属性或方法accessApp.db"如果我改变" accessApp.db"到" db"发生其他错误:"需要对象:' CurrentDB' "
答案 0 :(得分:1)
尝试类似下面的代码。它需要整理。
Option Explicit
Dim accessApp
Dim db
Dim dbname
Dim tdf
Dim fld1
Dim fld2
Dim acTextFormatPlain
Dim acTextFormatHTMLRichText
Dim dbInteger
'acTextFormatPlain=0
'acTextFormatHTMLRichText=1
dbInteger=3
dbname="D:\Test.mdb"
Set accessApp = CreateObject("Access.Application")
accessApp.OpenCurrentDataBase(dbname)
set db=accessapp.CurrentDb
Set tdf = db.TableDefs("2emails")
'The property may not exist
SetFieldProperty tdf.Fields(1), "TextFormat", dbInteger, 0
With tdf.Fields(1).Properties("TextFormat")
If .Value = 0 Then
.Value = 1
msgbox "TextFormat changed to: " & .Value
End If
End With
Sub SetFieldProperty(ByVal fld , ByVal strPropertyName , ByVal iDataType , ByVal vValue )
Dim prp
Set prp = Nothing
On Error Resume Next
Set prp = fld.Properties(strPropertyName)
On Error GoTo 0
If prp Is Nothing Then
Set prp = fld.CreateProperty(strPropertyName, iDataType, vValue)
fld.Properties.Append prp
Else
prp.Value = vValue
End If
End Sub