Delete line if Bookmark is Empty

A

aehan

Hello everyone

I have a question on user forms in Word.

I have programmed a user form which inserts information at bookmarks.
However, it would be much more useful if I could also programme into it that
if the bookmark is empty, I want to delete the whole line - in other words
this is an optional field and I don't want empty lines in the document. I
hope I'm explaining this properly.

Any help would be greatly appreciated as always.

Thanks
Aehan
 
J

Jay Freedman

In forms I've done, I usually go the opposite route: If there's an optional
field, its bookmark is placed in the same paragraph as the next bookmark, so
an empty bookmark occupies no space at all. If the optional field has a
valid entry, then the userform code adds a paragraph mark (vbCr) to the end
of the string just before inserting it into the document at the bookmark.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
A

aehan

That sounds a great solution and for two of my bookmarks it will work well.
However, three of the bookmarks are phone numbers, and the country code, area
code and first two numbers are already on the document.

The bookmark, which prompts them to fill in their direct dial, is inserted
directly after the country and area codes, and I want to delete the entire
line including the data that is there if the users don't want to put in their
direct dials.
 
J

Jay Freedman

Adapt what I said about adding a paragraph mark. Remove the country code and
area code from the base document, as well as the paragraph mark that
separates them from the next line. Once the user inputs something for their
direct dial in the userform, the code should create a string containing the
country code, the area code, the direct dial, and the paragraph mark. Insert
that in the bookmark.
 
A

aehan

Hi Jay

Thanks very much for getting back to me. I don't really know how to go
about writing that as code. Would you mind giving me an example?

Cheers
Aehan
 
J

Jay Freedman

Let's say the country code and area code are 1 and 800. Also let's say the
text box on the userform where the user may enter the direct dial number is
called txtPhone. Finally, assume that the bookmark is named Phone. Then you
could do this, in addition to whatever else the OK button's code does:

Private Sub cmdOK_Click()
Dim strPhone As String

If Len(Trim(txtPhone.Text)) > 0 Then
' here you should validate the value of txtPhone.Text
' (make sure it's the correct length and form)

strPhone = "1-800-" & Trim(txtPhone.Text) & vbCr
ActiveDocument.Bookmarks("Phone").Range.Text = strPhone
End If

Me.Hide
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
A

aehan

Hi Jay

Thank you so much for helping me, I really appreciate it. Not being the
best programmer there is, I've spent hours trying to find a solution, and you
have given it in minutes.

Thanks
Aehan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top