PDA

View Full Version : Solved: Word VBA "Label Not Defined" If Bookmark exists command



Michelle S.
12-01-2010, 04:43 PM
Hi
I am very new to VBA and just learning. Here's my situation and problem:
1) I created a working userform with text and comboboxes linking to bookmarks
 
2) Problem is that it doesn't work if some bookmarks don't exist (and the project will require this: the form will need to run on documents where not all bookmarks are present)
 
3) I would like the form stop giving me error messages if bookmarks arent there and just fill out the ones that are existing in that particular ocument
 
 
4) Here's the Code:
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) How do I get this to work?????????
 
 
 
 
 

gmaxey
12-01-2010, 06:09 PM
The error has nothing to do with bookmarks existing or not. The errors are caused by your references to labels that don't exist (e.g., 28. 32. 36 etc.)

You don't need them. If the bookmarks don't exists your code will continue running without using GoTo statements:

Private Sub cmdOK_Click()
Application.ScreenUpdating = False
With ActiveDocument
If .Bookmarks.Exists("cboYourName") Then .Range.Text = cboYourName.Value
If .Bookmarks.Exists("cboYourPhone") Then .Range.Text = cboYourPhone.Value
'and so on
End With
Application.ScreenUpdating = True
Unload Me
End Sub

gmaxey
12-01-2010, 06:23 PM
Sorry, the code I posted earlier doen't work. You don't need to use labels but you do need to point to the proper bookmark. Also you should write your data "in" the bookmark vice "at" the bookmark. Try something like:

Private Sub cmdOK_Click()
Application.ScreenUpdating = False
With ActiveDocument
If .Bookmarks.Exists("cboYourName") Then WriteToBookmarkRange "cboYourName", cboYourName.Value
If .Bookmarks.Exists("cboYourPhone") Then WriteToBookmarkRange "cboYourPhone", cboYourPhone.Value
'and so on
End With
Application.ScreenUpdating = True
Unload Me
End Sub
Sub WriteToBookmarkRange(ByRef pBMName As String, pStr As String)
Dim oRng As Word.Range
Set oRng = ActiveDocument.Bookmarks(pBMName).Range
oRng.Text = pStr
ActiveDocument.Bookmarks.Add pBMName, oRng
Set oRng = Nothing
End Sub




Hi
I am very new to VBA and just learning. Here's my situation and problem:
1) I created a working userform with text and comboboxes linking to bookmarks
 
2) Problem is that it doesn't work if some bookmarks don't exist (and the project will require this: the form will need to run on documents where not all bookmarks are present)
 
3) I would like the form stop giving me error messages if bookmarks arent there and just fill out the ones that are existing in that particular ocument
 
 
4) Here's the Code:
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) How do I get this to work?????????
 
 
 
 
 

Michelle S.
12-02-2010, 10:13 AM
It works! I love it! I love you! Thank you so much












Sorry, the code I posted earlier doen't work. You don't need to use labels but you do need to point to the proper bookmark. Also you should write your data "in" the bookmark vice "at" the bookmark. Try something like:

Private Sub cmdOK_Click()
Application.ScreenUpdating = False
With ActiveDocument
If .Bookmarks.Exists("cboYourName") Then WriteToBookmarkRange "cboYourName", cboYourName.Value
If .Bookmarks.Exists("cboYourPhone") Then WriteToBookmarkRange "cboYourPhone", cboYourPhone.Value
'and so on
End With
Application.ScreenUpdating = True
Unload Me
End Sub
Sub WriteToBookmarkRange(ByRef pBMName As String, pStr As String)
Dim oRng As Word.Range
Set oRng = ActiveDocument.Bookmarks(pBMName).Range
oRng.Text = pStr
ActiveDocument.Bookmarks.Add pBMName, oRng
Set oRng = Nothing
End Sub