WORD VBA - 用户表单 - 自动填写
WORD VBA - Userform - Auto fill
我正在尝试在 VBA Microsoft word 中创建用户表单。
我一直在关注 http://gregmaxey.com/word_tip_pages/create_employ_userform.html
创建表单。
我对编程非常非常新,基本上只是边学边自学。
当我尝试逐步调用 UF
时,我得到一个 "compile error: Sub of Function not defined"
我附上了完整的代码供您查看并告诉我哪里出错了,很高兴收到任何建议。
模块-modMain
Option Explicit
Sub Autonew()
Create_Reset_Variables
Call UF
lbl_Exit:
Exit Sub
End Sub
Sub Create_Reset_Variables()
With ActiveDocument.Variables
.Item("varFormNumber").Value = " "
.Item("varTitle").Value = " "
.Item("varGivenName").Value = " "
.Item("varFamilyName").Value = " "
.Item("varStreet").Value = " "
.Item("varSuburb").Value = " "
.Item("varState ").Value = " "
.Item("varPostCode").Value = " "
.Item("varInterviewDate").Value = " "
End With
myUpdateFields
lbl_Exit:
Exit Sub
End Sub
Sub myUpdateFields()
Dim oStyRng As Word.Range
Dim iLink As Long
iLink = ActiveDocument.Sections(1).Headers(1).Range.StoryType
For Each oStyRng In ActiveDocument.StoryRanges
Do
oStyRng.Fields.Update
Set oStyRng = oStyRng.NextStoryRange
Loop Until oStyRng Is Nothing
Next
End Sub
表格 - frmLetter13
Option Explicit
Public boolProceed As Boolean
Sub CalUF()
Dim oFrm As frmLetter13
Dim oVars As Word.Variables
Dim strTemp As String
Dim oRng As Word.Range
Dim i As Long
Dim strMultiSel As String
Set oVars = ActiveDocument.Variables
Set oFrm = New frmLetter13
With oFrm
.Show
If .boolProceed Then
oVars("varFormNumber").Value = TextBoxFormNumber
oVars("varTitle").Value = ComboBoxTitle
oVars("varGivenName").Value = TextBoxGivenName
oVars("varFamilyName").Value = TextBoxFamilyName
oVars("varStreet").Value = TextBoxStreet
oVars("varSuburb").Value = TextBoxSuburb
oVars("varState").Value = ComboBoxState
oVars("varPostCode").Value = TextBoxPostCode
oVars("varInterviewDate").Value = TextBoxInterviewDate
End If
Unload oFrm
Set oFrm = Nothing
Set oVars = Nothing
Set oRng = Nothing
lbl_Exit
Exit Sub
End Sub
Private Sub TextBoxFormNumber_Change()
End Sub
Private Sub Userform_Initialize()
With ComboBoxTitle
.AddItem "Mr"
.AddItem "Mrs"
.AddItem "Miss"
.AddItem "Ms"
End With
With ComboBoxState
.AddItem "QLD"
.AddItem "NSW"
.AddItem "ACT"
.AddItem "VIC"
.AddItem "TAS"
.AddItem "SA"
.AddItem "WA"
.AddItem "NT"
End With
lbl_Exit:
Exit Sub
End Sub
Private Sub CommandButtonCancel_Click()
Me.Hide
End Sub
Private Sub CommandButtonClear_Click()
Me.Hide
End Sub
Private Sub CommandButtonOk_Click()
Select Case ""
Case Me.TextBoxFormNumber
MsgBox "Please enter the form number."
Me.TextBoxFormNumber.SetFocus
Exit Sub
Case Me.ComboBoxTitle
MsgBox "Please enter the Applicant's title."
Me.ComboBoxTitle.SetFocus
Exit Sub
Case Me.TextBoxGivenName
MsgBox "Please enter the Applicant's given name."
Me.TextBoxGivenName.SetFocus
Exit Sub
Case Me.TextBoxFamilyName
MsgBox "Please enter the Applicant's family name."
Me.TextBoxFamilyName.SetFocus
Exit Sub
Case Me.TextBoxStreet
MsgBox "Please enter the street address."
Me.TextBoxStreet.SetFocus
Exit Sub
Case Me.TextBoxSuburb
MsgBox "Please enter the suburb."
Me.TextBoxSuburb.SetFocus
Exit Sub
Case Me.ComboBoxState
MsgBox "Please enter the state."
Me.ComboBoxState.SetFocus
Exit Sub
Case Me.TextBoxPostCode
MsgBox "Please enter the postcode."
Me.TextBoxPostCode.SetFocus
Exit Sub
Case Me.TextBoxInterviewDate
MsgBox "Please enter the interview date."
Me.TextBoxInterviewDate.SetFocus
Exit Sub
End Select
'Set value of a public variable declared at the form level.'
Me.boolProceed = True
Me.Hide
lbl_Exit:
Exit Sub
End Sub
这里有几个问题。
第一个问题是您没有名为 UF 的例程供 Call UF
调用。
您命名为 CalUF
的例程不应在用户窗体的代码中,而应在 modMain 中并重命名为 CallUF
.
没有必要在您的例程中包含退出点,因为您没有错误处理程序。
您的 AutoNew 例程可以重写为:
Sub Autonew()
Create_Reset_Variables
CallUF
End Sub
我已经为你评论了sub myUpdateFields
。
Sub myUpdateFields()
Dim oStyRng As Word.Range
Dim iLink As Long
iLink = ActiveDocument.Sections(1).Headers(1).Range.StoryType
' logically, iLink should be the StoryType of the first header in Section 1
' Why would this be needed in all StoryRanges?
' Anyway, it is never used. Why have it, then?
' This loops through all the StoryRanges
For Each oStyRng In ActiveDocument.StoryRanges
' This also loops through all the StoryRanges
Do
oStyRng.Fields.Update
Set oStyRng = oStyRng.NextStoryRange
Loop Until oStyRng Is Nothing
'And after you have looped through all the StoryRanges
' Here you go back and start all over again.
Next oStyRng End Sub
坦率地说,我不知道 Do 循环是否在这里执行任何操作。也许是这样。阅读有关 NextStoryRange
property here 的信息。我也不知道在内部循环中使用相同的对象变量是否会扰乱外部循环。我不知道这些事情,因为我从来不需要知道它们。所以我想知道为什么你在上学的第二天需要它们。
您正在设置多个文档变量。这些可以链接到您希望更新的文档中的 REF 字段。我敢打赌您的文档只有一个部分,没有脚注,也没有包含字段的文本框。因此,我认为以下代码应该可以满足您的所有需求,甚至更多。
Sub myUpdateFields2()
Dim Rng As Word.Range
For Each Rng In ActiveDocument.StoryRanges
Rng.Fields.Update
Next Rng
End Sub
对您来说,这段代码的巨大优势在于您可以完全理解它。为此,我避免使用像 oStyRng 这样的名称(大概意思是 "StoryRange Object")。 Word.Range
确实是一个对象。同样,该过程将 Range
类型的 StoryRange
分配给此变量也是如此。但最重要的事实是它是 Word.Range
,因此也是 Range
。当您直言不讳而不是 "metal object for digging earth" 时,代码将更易于阅读。因此,我首选的 Word.Range
变量名称是 "Rng"。但是——只是说。一定要为您的变量使用名称,这样您自己就可以轻松阅读您的代码。
我正在尝试在 VBA Microsoft word 中创建用户表单。 我一直在关注 http://gregmaxey.com/word_tip_pages/create_employ_userform.html 创建表单。
我对编程非常非常新,基本上只是边学边自学。
当我尝试逐步调用 UF
时,我得到一个 "compile error: Sub of Function not defined"我附上了完整的代码供您查看并告诉我哪里出错了,很高兴收到任何建议。
模块-modMain
Option Explicit
Sub Autonew()
Create_Reset_Variables
Call UF
lbl_Exit:
Exit Sub
End Sub
Sub Create_Reset_Variables()
With ActiveDocument.Variables
.Item("varFormNumber").Value = " "
.Item("varTitle").Value = " "
.Item("varGivenName").Value = " "
.Item("varFamilyName").Value = " "
.Item("varStreet").Value = " "
.Item("varSuburb").Value = " "
.Item("varState ").Value = " "
.Item("varPostCode").Value = " "
.Item("varInterviewDate").Value = " "
End With
myUpdateFields
lbl_Exit:
Exit Sub
End Sub
Sub myUpdateFields()
Dim oStyRng As Word.Range
Dim iLink As Long
iLink = ActiveDocument.Sections(1).Headers(1).Range.StoryType
For Each oStyRng In ActiveDocument.StoryRanges
Do
oStyRng.Fields.Update
Set oStyRng = oStyRng.NextStoryRange
Loop Until oStyRng Is Nothing
Next
End Sub
表格 - frmLetter13
Option Explicit
Public boolProceed As Boolean
Sub CalUF()
Dim oFrm As frmLetter13
Dim oVars As Word.Variables
Dim strTemp As String
Dim oRng As Word.Range
Dim i As Long
Dim strMultiSel As String
Set oVars = ActiveDocument.Variables
Set oFrm = New frmLetter13
With oFrm
.Show
If .boolProceed Then
oVars("varFormNumber").Value = TextBoxFormNumber
oVars("varTitle").Value = ComboBoxTitle
oVars("varGivenName").Value = TextBoxGivenName
oVars("varFamilyName").Value = TextBoxFamilyName
oVars("varStreet").Value = TextBoxStreet
oVars("varSuburb").Value = TextBoxSuburb
oVars("varState").Value = ComboBoxState
oVars("varPostCode").Value = TextBoxPostCode
oVars("varInterviewDate").Value = TextBoxInterviewDate
End If
Unload oFrm
Set oFrm = Nothing
Set oVars = Nothing
Set oRng = Nothing
lbl_Exit
Exit Sub
End Sub
Private Sub TextBoxFormNumber_Change()
End Sub
Private Sub Userform_Initialize()
With ComboBoxTitle
.AddItem "Mr"
.AddItem "Mrs"
.AddItem "Miss"
.AddItem "Ms"
End With
With ComboBoxState
.AddItem "QLD"
.AddItem "NSW"
.AddItem "ACT"
.AddItem "VIC"
.AddItem "TAS"
.AddItem "SA"
.AddItem "WA"
.AddItem "NT"
End With
lbl_Exit:
Exit Sub
End Sub
Private Sub CommandButtonCancel_Click()
Me.Hide
End Sub
Private Sub CommandButtonClear_Click()
Me.Hide
End Sub
Private Sub CommandButtonOk_Click()
Select Case ""
Case Me.TextBoxFormNumber
MsgBox "Please enter the form number."
Me.TextBoxFormNumber.SetFocus
Exit Sub
Case Me.ComboBoxTitle
MsgBox "Please enter the Applicant's title."
Me.ComboBoxTitle.SetFocus
Exit Sub
Case Me.TextBoxGivenName
MsgBox "Please enter the Applicant's given name."
Me.TextBoxGivenName.SetFocus
Exit Sub
Case Me.TextBoxFamilyName
MsgBox "Please enter the Applicant's family name."
Me.TextBoxFamilyName.SetFocus
Exit Sub
Case Me.TextBoxStreet
MsgBox "Please enter the street address."
Me.TextBoxStreet.SetFocus
Exit Sub
Case Me.TextBoxSuburb
MsgBox "Please enter the suburb."
Me.TextBoxSuburb.SetFocus
Exit Sub
Case Me.ComboBoxState
MsgBox "Please enter the state."
Me.ComboBoxState.SetFocus
Exit Sub
Case Me.TextBoxPostCode
MsgBox "Please enter the postcode."
Me.TextBoxPostCode.SetFocus
Exit Sub
Case Me.TextBoxInterviewDate
MsgBox "Please enter the interview date."
Me.TextBoxInterviewDate.SetFocus
Exit Sub
End Select
'Set value of a public variable declared at the form level.'
Me.boolProceed = True
Me.Hide
lbl_Exit:
Exit Sub
End Sub
这里有几个问题。
第一个问题是您没有名为 UF 的例程供 Call UF
调用。
您命名为 CalUF
的例程不应在用户窗体的代码中,而应在 modMain 中并重命名为 CallUF
.
没有必要在您的例程中包含退出点,因为您没有错误处理程序。
您的 AutoNew 例程可以重写为:
Sub Autonew()
Create_Reset_Variables
CallUF
End Sub
我已经为你评论了sub myUpdateFields
。
Sub myUpdateFields() Dim oStyRng As Word.Range Dim iLink As Long
iLink = ActiveDocument.Sections(1).Headers(1).Range.StoryType ' logically, iLink should be the StoryType of the first header in Section 1 ' Why would this be needed in all StoryRanges? ' Anyway, it is never used. Why have it, then? ' This loops through all the StoryRanges For Each oStyRng In ActiveDocument.StoryRanges ' This also loops through all the StoryRanges Do oStyRng.Fields.Update Set oStyRng = oStyRng.NextStoryRange Loop Until oStyRng Is Nothing 'And after you have looped through all the StoryRanges ' Here you go back and start all over again. Next oStyRng End Sub
坦率地说,我不知道 Do 循环是否在这里执行任何操作。也许是这样。阅读有关 NextStoryRange
property here 的信息。我也不知道在内部循环中使用相同的对象变量是否会扰乱外部循环。我不知道这些事情,因为我从来不需要知道它们。所以我想知道为什么你在上学的第二天需要它们。
您正在设置多个文档变量。这些可以链接到您希望更新的文档中的 REF 字段。我敢打赌您的文档只有一个部分,没有脚注,也没有包含字段的文本框。因此,我认为以下代码应该可以满足您的所有需求,甚至更多。
Sub myUpdateFields2()
Dim Rng As Word.Range
For Each Rng In ActiveDocument.StoryRanges
Rng.Fields.Update
Next Rng
End Sub
对您来说,这段代码的巨大优势在于您可以完全理解它。为此,我避免使用像 oStyRng 这样的名称(大概意思是 "StoryRange Object")。 Word.Range
确实是一个对象。同样,该过程将 Range
类型的 StoryRange
分配给此变量也是如此。但最重要的事实是它是 Word.Range
,因此也是 Range
。当您直言不讳而不是 "metal object for digging earth" 时,代码将更易于阅读。因此,我首选的 Word.Range
变量名称是 "Rng"。但是——只是说。一定要为您的变量使用名称,这样您自己就可以轻松阅读您的代码。