【问题标题】:WORD VBA - Userform - Auto fillWORD VBA - 用户表单 - 自动填充
【发布时间】:2017-08-07 00:04:54
【问题描述】:

我正在尝试在 Microsoft Word 上的 VBA 中创建用户表单。 我一直在关注http://gregmaxey.com/word_tip_pages/create_employ_userform.html 创建表单。

我对编程非常陌生,基本上只是边学边自学。

当我尝试单步执行 Call UF 时收到“编译错误:未定义子函数”

我已附上整个代码供您查看并告诉我哪里出了问题,很高兴有任何建议。

模块 - 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

【问题讨论】:

    标签: vba automation ms-word userform


    【解决方案1】:

    这里有几个问题。

    第一个问题是您没有一个名为 UF 的例程供Call UF 调用。

    您命名为CalUF 的例程不应在用户窗体的代码中,而应在modMain 中并重命名为CallUF

    由于您没有错误处理程序,因此无需在例程中包含退出点。

    您的 AutoNew 例程可以重写为:

    Sub Autonew()
    Create_Reset_Variables
    CallUF
    End Sub
    

    【讨论】:

    • 感谢您的更新 - 每当我单步执行代码时,我都会在“Create_Reset_Variables”上收到编译错误,有什么建议吗?
    • 我没有收到您发布的代码的编译错误。
    【解决方案2】:

    我已经为你评论了你的sub myUpdateFields

    Sub myUpdateFields() Dim oStyRng As Word.Range 将 iLink 调暗

    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 对象”)。确实,Word.Range 是一个对象。该过程也确实为该变量分配了Range 类型的StoryRange。但最重要的事实是它是Word.Range,因此是Range。当您称其为铁锹而不是“用于挖土的金属物体”时,代码将更易于阅读。因此,我对Word.Range 的首选变量名是“Rng”。但是——只是说。无论如何,为您的变量使用名称,这样您自己就可以轻松阅读您的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-22
      • 2018-11-28
      相关资源
      最近更新 更多