(问题下方的解决方案)
我有一个访问表单,该表单允许用户在文本框(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的,但是我不知道该怎么做。
我在论坛上找到了此代码
1)张贴此内容的人说他的用户“将文本输入一个大控件。然后将该控件合并到word文档中”。我的字段只是一个长文本字段,所以我仍然可以使用(或改编)下面的代码吗?
2)如果是,我不确定如何将其与代码混合使用(尽管我进行了所有搜索-我对VBA并不熟悉,但我不知道如何从表单中调用此Public Function):
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
好的,您似乎对实际的功能感到困惑。函数具有形式Function(Arguments)
,可以在Sub内部调用。它不能独立运行,因此不应被视为独立运行。可以使用的示例是MsgBox()
。您调用该MsgBox
函数,该函数接受一个参数(在这种情况下为一个或多个参数),并将其放入函数中,将其转换为所需的内容。
首先是第一件事。您拥有的文本框称为MYFIELD。那就是您的控制权。最好不要让Function具有相同的名称。将其重命名为Public Function ButChng(ctrl as Control)
。该功能本身并不需要从最初的外观上进行修改。
在您的MYFIELD_Exit
子目录中,您所需要做的就是调用该函数。因此它将显示为:
Private Sub MYFIELD_Exit(Cancel As Integer)
Call ButChng(MYFIELD) 'this puts the MYFIELD control as your variable to go through ButChng
Exit Sub
然后,它将按预期工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句