Solved

How do I add a border to a paragraph in Ms Word?

Posted on 2009-07-09
14
833 Views
Last Modified: 2012-05-07
I'm trying to generate a word document and I need to add a border. My 2 attempts below to a add a border to a paragraph (or line) only results in a border at the bottom of any text displayed. I'm trying to just put it under "2009 Call Report".
Dim oWord As Object
        Dim oDoc As Object
        Dim oPara1 As Object, oPara2 As Object
        Dim strText As String
        oWord = CreateObject("Word.Application")
        oWord.Visible = True
        oDoc = oWord.Documents.Add
        oDoc.Activate()
 
        ' Attempt #1
        With oWord.Selection
            .Font.Size = 24
            .Font.Bold = True
            .TypeText("2009 Call Report")
            .Borders.Item(wdBorderBottom).LineStyle = wdLineStyleSingle
            .Font.Size = 12
            .Font.Bold = False
            .TypeParagraph()
            .Font.Color = wdColorRed
            .Font.Italic = False
            .TypeText("This sentence will appear in red. ")
            .TypeParagraph()
            .Font.Color = wdColorBlack
            .Font.Italic = True
            .Font.Size = .Font.Size + 2
            .TypeText("Text color was reset to black, " & _
                      "but the font size was increased by two points.")
        End With
 
        ' Attempt #2
        oPara1 = oDoc.Content.Paragraphs.Add
        oPara1.Range.Text = "2009 Call Reports"
        oPara1.Range.Font.Bold = True
        oPara1.Range.Borders.Item(wdBorderBottom).LineStyle = wdLineStyleSingle
        oPara1.Format.SpaceAfter = 24    '24 pt spacing after paragraph.
        oPara1.Range.InsertParagraphAfter()
        oPara2 = oDoc.Content.Paragraphs.Add
        oPara2.Range.Text = "Salesperson: "
        oPara2.Range.Font.Bold = True
        oPara2.Range.Font.Color = wdColorRed
        oPara2.Format.SpaceAfter = 14
        oPara2.Range.InsertParagraphAfter()

Open in new window

0
Comment
Question by:wellhole
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 4
14 Comments
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 24815466
I don't know vb.nbet...but to put a line border around the entire selected paragraph, vba would be (this is simpy a recorded macro):
    With Selection.ParagraphFormat
        With .Borders(wdBorderLeft)
            .LineStyle = wdLineStyleSingle
            .LineWidth = wdLineWidth050pt
            .Color = wdColorAutomatic
        End With
        With .Borders(wdBorderRight)
            .LineStyle = wdLineStyleSingle
            .LineWidth = wdLineWidth050pt
            .Color = wdColorAutomatic
        End With
        With .Borders(wdBorderTop)
            .LineStyle = wdLineStyleSingle
            .LineWidth = wdLineWidth050pt
            .Color = wdColorAutomatic
        End With
        With .Borders(wdBorderBottom)
            .LineStyle = wdLineStyleSingle
            .LineWidth = wdLineWidth050pt
            .Color = wdColorAutomatic
        End With
        With .Borders
            .DistanceFromTop = 1
            .DistanceFromLeft = 4
            .DistanceFromBottom = 1
            .DistanceFromRight = 4
            .Shadow = False
        End With
    End With
    With Options
        .DefaultBorderLineStyle = wdLineStyleSingle
        .DefaultBorderLineWidth = wdLineWidth050pt
        .DefaultBorderColor = wdColorAutomatic
    End With

Open in new window

0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 24815770
Hello wellhole,

Try the following, (again VBA) which might be easier to re-use.

Regards,
Chris
Sub addBorder()
Dim bord As Variant 
    With Selection.ParagraphFormat
        For Each bord In Array(wdBorderLeft, wdBorderRight, wdBorderTop, wdBorderBottom)
            With .Borders(bord)
                .LineStyle = wdLineStyleSingle
                .LineWidth = wdLineWidth050pt
                .Color = wdColorAutomatic
            End With
            With .Borders
                .DistanceFromTop = 1
                .DistanceFromLeft = 4
                .DistanceFromBottom = 1
                .DistanceFromRight = 4
                .Shadow = False
            End With
        Next
    End With 
End Sub

Open in new window

0
 
LVL 9

Author Comment

by:wellhole
ID: 24815931
Unfortunately, neither solves the problem and accomplish the exact same result (they add a border to the bottom of the text). Thanks for trying, though. The solution I finally figured out is:

.TypeText("2009 Call Report")
.Borders.Item(wdBorderBottom).LineStyle = wdLineStyleSingle
.InsertBreak(wdSectionBreakContinuous)
.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
.TypeText("Text without border.")
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 24816133
Well - not knowing vb.net - I can only say that the macro I posted works on only the selected paragraph and puts the border on all sides (exactly what you asked for but not in .net).  See attached screenshot.  I can understand if our suggestions do not work for vb.net but I do not agree with your statements that they do not work at all and our comments did not address your issue.



2003-Border-Around-Paragraph.gif
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 24816205
The requirement for para 1 was never stated and is easy to accomodate as below.

Chris
Sub addBorder()
Dim bord As Variant
 
    With ThisDocument.Paragraphs(1)
        For Each bord In Array(wdBorderLeft, wdBorderRight, wdBorderTop, wdBorderBottom)
            With .Borders(bord)
                .LineStyle = wdLineStyleSingle
                .LineWidth = wdLineWidth050pt
                .Color = wdColorAutomatic
            End With
            With .Borders
                .DistanceFromTop = 1
                .DistanceFromLeft = 4
                .DistanceFromBottom = 1
                .DistanceFromRight = 4
                .Shadow = False
            End With
        Next
    End With
 
End Sub

Open in new window

0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 24816207
The requirement for para 1 was never stated and is easy to accomodate as below.

Chris
Sub addBorder()
Dim bord As Variant
 
    With ThisDocument.Paragraphs(1)
        For Each bord In Array(wdBorderLeft, wdBorderRight, wdBorderTop, wdBorderBottom)
            With .Borders(bord)
                .LineStyle = wdLineStyleSingle
                .LineWidth = wdLineWidth050pt
                .Color = wdColorAutomatic
            End With
            With .Borders
                .DistanceFromTop = 1
                .DistanceFromLeft = 4
                .DistanceFromBottom = 1
                .DistanceFromRight = 4
                .Shadow = False
            End With
        Next
    End With
 
End Sub

Open in new window

0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 24816238
Apologies for the double post ... I don't know what happened though I was in the process of posting an objection as stated.

Chris
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 24816320
I also fail to see how your supposed solution creates a box around the entire phrase wellhole....
0
 
LVL 9

Author Comment

by:wellhole
ID: 24816503
Oops... Looks like Chris' function would work, but JOrzech's still doesn't. All it does is put a line on the bottom of all text and not on just 1 paragraph, which I've done.
0
 
LVL 9

Author Comment

by:wellhole
ID: 24816559
Well.... Chris' NEW function would, not his old one.
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 24817226
Good job Chris.

And I'll be sure to keep an eye out for future questions from you wellhole....
0
 
LVL 9

Author Comment

by:wellhole
ID: 24817274
JOrzech, I can't help it if you don't read the whole question. I wrote in plain english "I'm trying to just put it under "2009 Call Report"."
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 24817519
I do not intend to argue the issue - I did read the whole question.  I am simply stating that most lines and paragraphs end with a paragraph mark.  Working on that assumption, I provided the macro to you which, if the words "2009 Call Report" were on one line with a paragraph mark at the end and you selected those words, the macro would work as you requested.

0
 
LVL 9

Author Comment

by:wellhole
ID: 24817558
I'm too tired from working out this problem and I'd love not to do any kind of argument, but you do realize I'm generating the report with code, don't you? I don't have the option to select something and run a macro on it, unless somehow that's put into code as well.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to remove error in database 6 55
analyze malware in windows document 9 64
Check only one toolstripmenu item 12 63
Word 2010 - how to adjust graphic "inline with text" 5 37
I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question