Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-21-2015, 06:18 PM
dsjk9190 dsjk9190 is offline Delete bookmark and paragraph which is blank Windows 8 Delete bookmark and paragraph which is blank Office 2010 64bit
Novice
Delete bookmark and paragraph which is blank
 
Join Date: Jan 2015
Posts: 4
dsjk9190 is on a distinguished road
Default Delete bookmark and paragraph which is blank

I've got a few related queries, so here it goes.
Firstly, I have a cover letter which includes the address details (see below for example)

Mr/Mrs Contact Name


Business Name
Street Address
Suburb State Postcode

As you can see the second line of the address is the business name (which has it's own bookmark). When the userform is populated there may not be a business name filed into the relevant text book (and then bookmark), so how do I go about creating a code to remove this one line (paragraph)?

Secondly, later on in the document there is a reference made to the business name (with a different bookmark), however similar to the first query I want to replace this bookmark with the contact name if the business name textbook has not been populated in the userform. Does anyone have any thoughts or suggestions?

Thanks in anticipation
Reply With Quote
  #2  
Old 01-22-2015, 12:26 AM
Guessed's Avatar
Guessed Guessed is offline Delete bookmark and paragraph which is blank Windows 7 32bit Delete bookmark and paragraph which is blank Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

It is better to delete the paragraph at the time of writing the userform fields to the document but you haven't shown us your code.

This code can be used after you have populated the bookmarks. It doesn't query the text in the bookmark itself since it is likely your code is adding the text beside the bookmarks rather than inside them.

If Len(ActiveDocument.Bookmarks("aa").Range.Paragraph s(1).Range) = 1 Then
ActiveDocument.Bookmarks("aa").Range.Paragraphs(1) .Range.Delete
End If

With your second question, this should definitely be handled at the time of writing the userform values.
If Len(Me.BusinessName) = 0 then
ActiveDocument.Bookmarks("ab").Range.Text = Me.ContactName
Else
ActiveDocument.Bookmarks("ab").Range.Text = Me.BusinessName
End If

Note that this code doesn't actually fill the bookmark and may also actually delete the bookmark which is a pain I alluded to in the initial code. To populate the bookmark ranges 'properly' you would need to look at this info
http://word.mvps.org/faqs/macrosvba/...hBookmarks.htm
Reply With Quote
  #3  
Old 01-22-2015, 01:15 AM
gmayor's Avatar
gmayor gmayor is offline Delete bookmark and paragraph which is blank Windows 7 64bit Delete bookmark and paragraph which is blank Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

As suggested by 'Guessed', this is not the best way to achieve what you are trying to do here. It would be much better to use only one bookmark e.g. bkAddress and populate that bookmark with the results of your userform. Assign the values of the fields to strings as below, then assemble the strings as the finished address before filling the bookmark.

The FillBM code retains the bookmark so that you may run the userform again and change the content. Use it to fill any bookmark in your document:

Code:
Sub example()
Dim strAddress As String
Dim strName As String
Dim strBusiness As String
Dim strStreet As String
Dim strSuburb As String
Dim strState As String
Dim strPostCode As String
    strName = "Mr/Mrs Contact Name"
    strBusiness = ""        '"Business Name"
    strStreet = "Street Address"
    strSuburb = "Suburb"
    strState = "State"
    strPostCode = "Postcode"

    strAddress = strName & vbCr
    If Not strBusiness = "" Then strAddress = strAddress & strBusiness & vbCr
    strAddress = strAddress & strStreet & vbCr
    strAddress = strAddress & strSuburb & Chr(32) & _
                 strState & Chr(32) & strPostCode
    FillBM "bkAddress", strAddress
End Sub


Code:
Public Sub FillBM(strBMName As String, strValue As String)
Dim orng As Range
    With ActiveDocument
        On Error GoTo lbl_Exit
        Set orng = .Bookmarks(strBMName).Range
        orng.Text = strValue
        orng.Bookmarks.Add strBMName
    End With
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply

Tags
paragraph bookmark blank



Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete bookmark and paragraph which is blank Why when I paste text into word, blank space appears after a paragraph & I can't delete it? AWD Word 3 10-16-2018 01:10 PM
Delete bookmark and paragraph which is blank Calculate the coordinates of a DocVariable, Bookmark or paragraph. MaxInCO Word VBA 5 12-11-2013 03:28 PM
Delete bookmark and paragraph which is blank Delete Range After Bookmark? VBA_Elfe Word VBA 3 04-05-2013 05:05 AM
Delete bookmark and paragraph which is blank delete all bookmark text hklein Word VBA 4 08-10-2011 04:33 AM
Delete bookmark and paragraph which is blank Newbie: Delete Bookmark expatriate Word VBA 4 06-02-2011 01:12 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:30 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft