PDA

View Full Version : Solved: Runtime Error 6028



flohrstw
04-13-2012, 12:18 PM
'm adding a UserForm to a Word template. Upon submitting the form, I receive 'Runtime Error 6028 - The range cannot be deleted'. The debugger points to the following script:

-----------------------------------------------------------------------------
Private Sub CmdSubmit_Click()

Application.ScreenUpdating = False

With ActiveDocument

--> .Bookmarks("Text1").Range.Text = txtUserName.Value

.Bookmarks("Text2").Range.Text = txtTest1.Value

.Bookmarks("Text3").Range.Text = txtTest2.Value

.Bookmarks("Text4").Range.Text = txtTest1.Value

End With

Application.ScreenUpdating = True

Unload Me

End Sub
----------------------------------------------------------------------------

This is a simple test form I have created before I begin working on the real document. On occassion, there will be values that are displayed in multiple places, hence the repition of txtTest1.

I am new to VB coding and any assistance would be greatly appreciated.

fumei
04-13-2012, 02:05 PM
6028 is a stange error.

First of all are you sure you are using bookmarks? Are they possibly formfield instead? Do you protect your document for forms?

Here is the thing; when you make a bookmark RANGE.text some text, the bookmark is deleted. It is gone after you put text into the range.text.

Unless this is what you want (and I doubt it), one possible solution is the recreate the bookmark with the text, like this:Sub UpdateBM(strBM As String, strText As String)
Dim r As Range
Set r = ActiveDocument.Bookmarks(strBM).Range
r.Text = strText
ActiveDocument.Bookmarks.Add Name:=strBM, Range:=r
End SubYou CALL the sub with your parameter values; like this:
Private Sub CmdSubmit_Click()
Application.ScreenUpdating = False
With ActiveDocument
Call UpdateBM("Text1", txtUserName.Value)
Call UpdateBM("Text2", txtTest1.Value)

HOWEVER, I am willing to bet money that you arte NOT in fact trying to put those values into a bookmark. You are trying to put them into formfields. "Text1" is the default name for a formfield.

Formfields are not bookmarks (although strangely I see this mistake quite often). Formfields HAVE bookmarks. They are not the same thing at all.

flohrstw
04-13-2012, 02:25 PM
You are correct, I am using the Text Form Fields in the document. The confussion likely lies in the fact that the properties box for the Text Form Field refers to a Bookmark.

The template document is not protected right now, but will be before it is released to all users.

Looking through some other forums and questions I came across something similar. Would this do what I want?

.Formfields("Text1").Result.Text = txtUserName.Value

fumei
04-14-2012, 02:15 PM
Yes.


the properties box for the Text Form Field refers to a Bookmark. I know. An unfortunate thing, but unavoidable. When you make a formfield, it ALSO makes a bookmark. The bookmark has the same name as the formfield. this Bookmark: is (for example) Text1. So the dialog is accurate. The unfortunate thing is that people mistake this to mean the formfield and bookmark are the same thing. Again, they are not.

You could delete the bookmark...and keep the formfield (although not the reverse). In other words, it is possible to have a formfield with no name. In fact this is what happens if you copy and paste formfields. You can not have duplicate bookmark names...so the paste of a formfield makes a formfield with NO bookmark.

Formfields and bookmarks are NOT the same. Formfields have bookmarks (or not). it is not a good practice to use bookmark methods when you are in fact trying to put data into a formfield.