2017-03-16 5 views
0

私はMicrosoft WordでVBAでユーザーフォームを作成しようとしています。 私はhttp://gregmaxey.com/word_tip_pages/create_employ_userform.html をフォローしてフォームを作成しています。WORD VBA - ユーザーフォーム - 自動入力

私は非常に非常にプログラミングの新しいと基本的にちょうど私が行くように自分自身を教えている。

私が手に:私がしようとするとコール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 

答えて

1

問題のカップルがここにあります。

最初の問題は、Call UFの呼び出しにUFという名前のルーチンがないことです。

CalUFという名前のルーチンは、ユーザーフォームのコードに含まれてはいけませんが、modMainにあり、CallUFという名前に変更する必要があります。

エラーハンドラを持たないため、ルーチンに終了点を含める必要はありません。私はあなたのためにsub myUpdateFieldsをコメントしている

Sub Autonew() 
Create_Reset_Variables 
CallUF 
End Sub 
+0

ありがとうございます - コードをステップ実行するたびに、 'Create_Reset_Variables'でコンパイルエラーが発生します 提案がありますか? –

+0

投稿したコードにコンパイルエラーがありません。 –

1

:として

あなたはAutoNewルーチンを書き換えることができます。

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ループが何かをしているかどうかはわかりません。おそらくそうです。 NextStoryRangeproperty hereについて読んでください。私はまた、内側のループで同じオブジェクト変数を使用することが外側のループを混乱させるかどうかわかりません。私はそれらを知る必要がないので、これらのことは知らない。だから私はなぜあなたが学校での2日目にそれらを必要とするのだろうかと思います。

複数のドキュメント変数を設定しています。これらは、更新したい文書のREFフィールドにリンクすることができます。あなたの文書にセクションが1つしかなく、脚注はなく、フィールドがあるテキストボックスはありません。したがって、私は次のコードは、それ以上のものでなければ必要なすべてを行うべきだと考えています。

このコードの大きな利点は、完全に理解できることです。この目的のために、私はoStyRng(おそらく "StoryRange Object"を意味する)のような名前の使用を避けています。 Word.Rangeがオブジェクトであることは事実です。また、プロシージャによってStoryRangeタイプのRangeがこの変数に割り当てられていることも当てはまります。しかし、乗り越えの真実は、それがWord.Rangeであり、したがってRangeであるということです。あなたがスペードをスペードと呼んだときに「土を掘るための金属物体」ではなく、コードが読みやすくなります。 Word.Rangeの私の好みの変数名は、 "Rng"です。しかし - ただ言って。是非、あなたのコードを自分で簡単に読むための変数の名前を使用してください。

関連する問題