2016-10-26 33 views
0

我有一个关于使用VBScript将memofield从“纯文本”更改为“富文本”的问题,我在这里和在互联网上发现了一些解决方案,但是所有的解决方案都是针对VBScript的。我尝试通过Windows启动一个vbscript,但我的脚本不起作用。我很喜欢VBScripting,所以我希望你们能帮助我。我以前从论坛的例子为我的脚本: How to convert a text field in an Access table to a rich text memo using VBA使用VBScript将备忘录字段从“纯文本”更改为“富文本”

我的脚本:

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 

什么occures告诉我,问题是出在“设置accessApp.db = CurrentDB”错误的错误,发生的是:“对象不支持此属性或方法accessApp.db”如果我将“accessApp.db”更改为“db”,则发生其他错误:“Object required:'CurrentDB'”

+0

来自访问数据库的代码不会直接作为vbs脚本文件工作。您需要通过连接字符串连接到Ms Access DB,然后编写将PlainText转换为RichText的逻辑。 –

+0

你有一些不必要的行。尝试'Set db = accessApp.OpenCurrentDataBase“D:\ test.mdb”,true'并跳过'Set accessApp.db = CurrentDB',你不需要它。数据库不是Access应用程序的属性。请参阅https://msdn.microsoft.com/en-us/library/office/jj250267.aspx – Fionnuala

+0

@MukulVarshney,这是不正确的。您可能无法使用ADODB连接更改字段类型,但可以使用Access应用程序对象。 – Fionnuala

回答

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 
相关问题