How can I delete a space/word next to a bookmark

S

spenningzahl

I have a large template, consisting of many bookmarks. The template
has a userform consisting of 8 tabs with apprx. 4 questions on each.
You can choose what to select quite randomly, and do not have to
select a question on each tab. To explain you can for instance only
pick 2 questions out of 32 or you can chose 15, 16 etc. In my template
I have the bookmarks set up like this Bkm1[space]Bkm2[space]Bkm3[space]
Bkm4[space].... and so on.

If I do not select a question I get all the spaces (from the bookmarks
I have not selected in front of first section like this.:
[space][space][space][space]MyHeading

Does anyone know how to fix this?

I have tried many codes, but nothing works. The last I have tried goes
something like this:

Dim bmk As Bookmark
Dim rng As Range


If Opt2_5_Ja.Value = False Then
Set bmk = ActiveDocument.Bookmarks(bm2_5_1)
Set rng = bmk.Range
rng.Words(1).Delete

End If

Thanks, Karin
 
M

macropod

Hi,

You can apply an IF test to each bookmark, using field coding like:
{IF{REF Bkm1}<> " " "{REF Bkm1} "}
where 'Bkm1' is the bookmark name and the " " represents the default field text (usually 5 spaces).

Note: The field brace pairs (ie '{ }') for the above example are created via Ctrl-F9 - you can't simply type them or copy & paste
them from this message.
 
S

spenningzahl

Hi,

You can apply an IF test to each bookmark, using field coding like:
{IF{REF Bkm1}<> "     " "{REF Bkm1} "}
where 'Bkm1' is the bookmark name and the "     " represents the default field text (usually 5 spaces).

Note: The field brace pairs (ie '{ }') for the above example are created via Ctrl-F9 - you can't simply type them or copy & paste
them from this message.

--
Cheers
macropod
[MVP - Microsoft Word]



I have a large template, consisting of many bookmarks. The template
has a userform consisting of 8 tabs with apprx. 4 questions on each.
You can choose what to select quite randomly, and do not have to
select a question on each tab. To explain you can for instance only
pick 2 questions out of 32 or you can chose 15, 16 etc. In my template
I have the bookmarks set up like this Bkm1[space]Bkm2[space]Bkm3[space]
Bkm4[space].... and so on.
If I do not select a question I get all the spaces (from the bookmarks
I have not selected in front of first section like this.:
[space][space][space][space]MyHeading
Does anyone know how to fix this?
I have tried many codes, but nothing works. The last I have tried goes
something like this:
Dim bmk As Bookmark
Dim rng As Range
If Opt2_5_Ja.Value = False Then
Set bmk = ActiveDocument.Bookmarks(bm2_5_1)
Set rng = bmk.Range
rng.Words(1).Delete
Thanks, Karin– Skjul sitert tekst –

– Vis sitert tekst –
Hi
Thank you for your reply. I am sorry, but as I am quite fresh with VBA
I do not understand where to insert the code stated above. Thanks.
 
D

Doug Robbins - Word MVP on news.microsoft.com

I would suggest that you delete all but one of the bookmarks and use code in
the userform to construct a string consisting of all of the selected items
with each one being separated by a space and then insert that string into
the bookmark range. The following code would go in the command button click
event that you have in the userform:

Dim mystr as String
mystr = ""
If Len(Trim(txtQuestion1.Text))>0 then
mystr = mystr & txtQuestion1.text
End if
If Len(Trim(txtQuestion2.Text))>0 then
mystr = mystr & " " & txtQuestion2.text
End if
If Len(Trim(txtQuestion3.Text))>0 then
mystr = mystr & " " & txtQuestion3.text
End if
'etc
If Len(Trim(txtQuestion32.Text))>0 then
mystr = mystr & " " & txtQuestion32.text
End if
ActiveDocument.Bookmarks("BookmarkName").Range.InsertBefore mystr


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
G

Graham Mayor

Hi
Thank you for your reply. I am sorry, but as I am quite fresh with VBA
I do not understand where to insert the code stated above. Thanks.

This is nothing to do with vba. These are fields inserted into the document.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

Frankly I would not have started from this position. I would have just the
one bookmark or docvariable field and assemble the text to be entered in the
userform macro, See - http://www.gmayor.com/SelectFile.htm, but if your
bookmarks are empty, you can simply use replace to remove the double spaces
eg a wildcard search for
([ ]){1,}
replace with
\1

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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