2010-12-01 6 views
0

저는 VBA에 익숙하며 익숙합니다. 여기에 내 상황과 문제가있다 :Word VBA "레이블이 정의되지 않음"북마크가 존재하는 경우 명령

1) 북마크에 연결되는 텍스트와 콤보 박스가있는 작업 userform을 만들었습니다. 2) 문제가되는 것은 북마크가 존재하지 않는다면 작동하지 않는다는 것입니다. 양식이 모든 북마크가 존재하지 않는 문서에서 실행해야합니다.) 3) 북마크가 없으면 해당 양식에 오류 메시지를 표시하고 해당 특정 문서에있는 정보 만 입력하면됩니다. 4) 여기 있습니다 코드 :

Private Sub cmdOK_Click() 
    Application.ScreenUpdating = False 
    With ActiveDocument 
      If .Bookmarks.Exists("cboYourName") Then 
     .Range.Text = cboYourName.Value 
     Else: GoTo 28 
    End If 
     If .Bookmarks.Exists("cboYourPhone") Then 
     .Range.Text = cboYourPhone.Value 
     Else: GoTo 32 
    End If 
     If .Bookmarks.Exists("cboYourFax") Then 
     .Range.Text = cboYourFax.Value 
     Else: GoTo 36 
    End If 
     If .Bookmarks.Exists("cboYourEmail") Then 
     .Range.Text = cboYourEmail.Value 
     Else: GoTo 40 
    End If 
     If .Bookmarks.Exists("txtContractName") Then 
     .Range.Text = txtContractName.Value 
     Else: GoTo 44 
    End If 
      If .Bookmarks.Exists("txtContractNumber") Then 
      .Range.Text = txtContractNumber.Value 
      Else: End 
    End If 
    End With 
    Application.ScreenUpdating = True 
    Unload Me 
End Sub 

4) 어떻게 작동합니까? ?????????

답변

0

가까운 사이라고 생각합니다. 먼저 Goto 문을 사용하지 마십시오. 당신의 코드에서, 당신이하고자하는 것이 무엇인지를 말하기는 어렵습니다. 나는 오류가 고토 (Goto) 진술에서 나온 것이라고 생각한다. 그 매개 변수는 행 번호가 아닌 레이블입니다. 둘째, End를 사용하지 마십시오. 폐회식을 갖는 것이 낫습니다. 즉,이 코드는 Exists 문과 함께 작동합니다.

Private Sub cmdOK_Click() 
    Application.ScreenUpdating = False 
    With ActiveDocument 
     If .Bookmarks.Exists("cboYourName") Then 
      .Range.Text = "cboYourName text." 
     Else 
      Debug.Print "Bookmark exists." 
     End If 

     If .Bookmarks.Exists("cboYourPhone") Then 
      .Range.Text = "cboYourPhone text" 
     Else 
      Debug.Print "Bookmark does not exists." 
     End If 
    End With 

    Application.ScreenUpdating = True 
    Unload Me 
End Sub 

그러나 발견 된 각 책갈피는 이후 발견 된 책갈피를 포함하여 문서의 내용을 완전히 대체합니다. 그게 무슨 뜻 이니?

관련 문제