Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete line or text if bookmark is empty 1

Status
Not open for further replies.

wordperfectconvert

Technical User
Feb 16, 2005
18
US
I am new to VBA. I use a software program that creates documents from a database. However, some times the information for the document does not exist (i.e. "accountnumber"). In those instances, the form is created just fine, but there is a line of text in the RE: for the letter that says "Account Number : [bookmark for info].

What I want to do is create a macro that opens when the template opens (I know how to do that) where the macro searches for the bookmark and then determines if there is any information in the bookmark. If there is information (account number) the macro terminates. If there is no information, I want to go to the bookmark, move to the beginning of the line and delete to the end of the line. (this is a word document).

Can anyone help. I am learning fast, but have been unable to find any help on this issue. Thanks.

Bill
 
Hi Bill,

Something along these lines ought to do it:
Code:
[blue]ActiveDocument.Bookmarks("[i]YourBookmark[/i]").Select
If Selection.Range = "" Then Selection.Bookmarks("\Line").Delete[/blue]
The check against the bookmark contents depends a little on how you have it set up and how you populate it with your account number but I hope that gives you something to start from.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Tony,
Thanks for the response. I tried it with a true bookmark and got an error for the portion of the code that read:

Selection.Bookmarks("\Line").Delete

I also tried it with a form field, but it read the field as not being empty and took no action.

The only way I could get it to work was to put default text in the form field that said "empty" and then had it select the form field for the text "empty". This is what I used, but there has got to be a better way.

If ActiveDocument.Bookmarks.Exists("CaseNo") = True Then
If ActiveDocument.Bookmarks("CaseNo").Range.Text = "Empty" Then
Selection.GoTo What:=wdGoToBookmark, Name:="CaseNo"
With ActiveDocument.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
End If

This works, but is not how I want it to work. Any help would be appreciated.
 
Hi Bill,

My apologies, I should have said
Code:
[blue] ... Selection.Bookmarks("\Line")[red].Range[/red].Delete[/blue]

Does that make it easier? I'm not sure off the top of my head whether being in a form field makes a difference. I'll check it out and come back if it does.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
No exactly sure what you are doing, but you can use .Exists for a bookmark, check the text of the formfield, then delete the bookmark if = "Empty"

Formfields are also bookmarks, and youo canuse certain methods between them.

For example, you can check if a formfield exists, by checking the .Exists of it as a bookmark, then delete it qs a formfield.
Code:
Sub CheckExists()
With ActiveDocument
    If .Bookmarks.Exists("CaseNo") = True Then
             .Unprotect Password:=""
        If .FormFields("CaseNo").Range.Text = "Empty" Then
            .Bookmarks("\line").Delete
            .Protect Type:=wdAllowOnlyFormFields, Password:=""
        Else
           .Protect Type:=wdAllowOnlyFormFields, Password:=""
            Exit Sub
        End If
    Else
        ' doing what if it does NOT esist?????
    End If
End With
End Sub

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top