【发布时间】:2014-06-16 07:26:43
【问题描述】:
(问题下方的解决方案)
我有一个访问表单,允许用户在文本框 (MYFIELD) 中输入文本。当用户退出该字段时,我已经自动设置了拼写检查选项
Private Sub MYFIELD_Exit(Cancel As Integer)
Dim strSpell
strSpell = MYFIELD
If IsNull(Len(strSpell)) Or Len(strSpell) = 0 Then
Exit Sub
End If
With MYFIELD
.SetFocus
.SelStart = 0
.SelLength = Len(strSpell)
End With
DoCmd.SetWarnings False
DoCmd.RunCommand acCmdSpelling
DoCmd.SetWarnings True
End Sub
我还想启用语法检查。因为它不是在 Access 中构建的,所以我需要使用 word 的,但我不知道该怎么做。
我在forum 上找到了这段代码
1) 发布此消息的人说他的用户“将文本输入到一个大控件中。然后将该控件合并到一个 word 文档中”。我的字段只是一个长文本字段,我还能使用(或改编)下面的代码吗?
2) 如果是,我不确定如何将它与我的代码混合使用(尽管我进行了所有搜索-我不熟悉 VBA-,但我不明白如何从我的表单中调用此公共函数):
Public Function SpellIt(ctl As Control)
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
On Error GoTo SpellIt_Err
Set wdApp = New Word.Application
If Not IsNull(ctl) Then
Set wdDoc = wdApp.Documents.Add
wdApp.Selection.Text = ctl
wdApp.Dialogs(wdDialogToolsSpellingAndGrammar).Show
If Len(wdApp.Selection.Text) <> 1 Then
ctl = wdApp.Selection.Text
Else
wdDoc.Close wdDoNotSaveChanges
wdApp.Quit
Set wdApp = Nothing
Exit Function
End If
wdDoc.Close wdDoNotSaveChanges
End If
wdApp.Quit
Set wdApp = Nothing
MsgBox "Spelling and Grammar Check Complete.", vbInformation, "Microsoft Word Spelling And Grammar:"
Exit Function
SpellIt_Err:
Err.Clear
ctl.Undo
MsgBox "We encountered an error in it's conversation with Microsoft Word regarding your comment." & vbCrLf & _
"As a precaution, any changes made within the grammar and spelling dialog box have not been retained.", _
vbCritical, "Spelling and Grammar Check NOT Complete:"
End Function
非常感谢您的帮助!
这里是解决方案:(我需要将ctl更改为ctrl)
Private Sub Description_Exit(Cancel As Integer)
Call SpellIt(Description)
End Sub
Public Function SpellIt(ctrl As Control)
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
On Error GoTo SpellIt_Err
Set wdApp = New Word.Application
If Not IsNull(ctrl) Then
Set wdDoc = wdApp.Documents.Add
wdApp.Selection.Text = ctrl
wdApp.Dialogs(wdDialogToolsSpellingAndGrammar).Show
If Len(wdApp.Selection.Text) <> 1 Then
ctl = wdApp.Selection.Text
Else
wdDoc.Close wdDoNotSaveChanges
wdApp.Quit
Set wdApp = Nothing
Exit Function
End If
wdDoc.Close wdDoNotSaveChanges
End If
wdApp.Quit
Set wdApp = Nothing
MsgBox "Spelling and Grammar Check Complete.", vbInformation, "Microsoft Word Spelling And Grammar:"
Exit Function
SpellIt_Err:
Err.Clear
ctl.Undo
MsgBox "We encountered an error in it's conversation with Microsoft Word regarding your comment." & vbCrLf & _
"As a precaution, any changes made within the grammar and spelling dialog box have not been retained.", _
vbCritical, "Spelling and Grammar Check NOT Complete:"
End Function
【问题讨论】:
-
只需将函数指向带有文本的控件对象。它将打开 word,在内容 (
wdApp.Dialogs) 上运行拼写/语法对话框,然后在完成后关闭。 -
CheckGrammar 方法,因为它适用于 Application 对象。检查字符串的语法错误。返回一个布尔值以指示字符串是否包含语法错误。如果字符串不包含错误,则为真。 expression.CheckGrammar(String) 表达式 必需。返回 Application 对象的表达式。字符串 必需的字符串。要检查语法错误的字符串。
-
谢谢博士!你给我一些线索来理解这段晦涩的代码
-
感谢@bmgh1985 的帮助!我已经更新了上面的代码,尝试“将函数指向带有文本的控制对象”。但是我从 Access 中得到一个错误:“过程声明与具有相同名称的事件或过程的描述不匹配”。你知道我做错了什么吗?如您所见,我是新手,这可能很明显,但我无法弄清楚。谢谢!
-
@bmgh1985 我做了更多的挖掘工作,但我仍然卡住了。我不知道语法检查将如何启动(我试图在退出字段 MYFIELD 时启动它,我想由语法检查器检查)。任何建议将不胜感激!谢谢!
标签: vba ms-access spell-checking