Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-11-2017, 10:55 AM
jmltinc jmltinc is offline Underline Inserted Text Windows 7 64bit Underline Inserted Text Office 2016
Novice
Underline Inserted Text
 
Join Date: Jan 2017
Posts: 9
jmltinc is on a distinguished road
Default Underline Inserted Text

Hi Folks,

Another trip to the well for a Word VBA Newb...

I have VBA code which opens a Word instance and programmatically populates the document with tables. However, each table must have a title, so my code is:

Code:
        Selection.Text = " Product Specifications"
        Selection.Font.Name = "calibri"
        Selection.Font.Size = 16
        Selection.Font.Bold = True
        Selection.EndKey Unit:=wdStory
 'Now add table...
        .Tables.Add Range:=Selection.Range, NumRows:=3, NumColumns:=4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
Now, if I want the title underlined, the code becomes:


Code:
        Selection.Text = " Product Specifications"
        Selection.Font.Name = "calibri"
        Selection.Font.Size = 16
        Selection.Font.Bold = True
        Selection.Font.Underline = wdUnderlineSingle 
        Selection.EndKey Unit:=wdStory
 'Now add table...
        .Tables.Add Range:=Selection.Range, NumRows:=3, NumColumns:=4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
.

Of course, when I do, everything in the document after that line is underlined.

I only want the titles underlined. What am I doing wrong, please?

Thanks,
-John
Reply With Quote
  #2  
Old 01-11-2017, 12:32 PM
dwirony dwirony is offline Underline Inserted Text Windows 7 64bit Underline Inserted Text Office 2003
Advanced Beginner
 
Join Date: Oct 2016
Posts: 49
dwirony will become famous soon enough
Arrow

Ooops, I may have posted some crappy code before. If you're trying to just underline that word, maybe just the use find and replace to underline it? That should only underline that title:

Code:
With Selection.Find
        .Text = " Product Specifications"
        .Replacement.Text = " Product Specifications"
        .Replacement.Font.Underline = True
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
Hope this helps...
Reply With Quote
  #3  
Old 01-12-2017, 03:41 AM
jmltinc jmltinc is offline Underline Inserted Text Windows 7 64bit Underline Inserted Text Office 2016
Novice
Underline Inserted Text
 
Join Date: Jan 2017
Posts: 9
jmltinc is on a distinguished road
Default

Quote:
If you're trying to just underline that word, maybe just the use find and replace to underline it?
Thanks for the reply.

I thought of doing a find and replace, but I think it will be time-consuming. The report is dynamic, generated on the fly by a definition which drives it. It may insert 10 tables, each with it own unique table title. Each title must be underlined.

I would like to underline each title at the time the title is inserted but before the table is inserted.

Thoughts?

Thanks,
-John
Reply With Quote
  #4  
Old 01-17-2017, 02:28 PM
dwirony dwirony is offline Underline Inserted Text Windows 7 64bit Underline Inserted Text Office 2003
Advanced Beginner
 
Join Date: Oct 2016
Posts: 49
dwirony will become famous soon enough
Default

I'm afraid I can't help you , I'm still somewhat of a novice myself.

Bump for a better answer-
Reply With Quote
  #5  
Old 01-17-2017, 10:45 PM
gmayor's Avatar
gmayor gmayor is offline Underline Inserted Text Windows 10 Underline Inserted Text Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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

Quote:
Originally Posted by jmltinc View Post
I would like to underline each title at the time the title is inserted but before the table is inserted.
Create or modify a paragraph style that has the characteristics you want, assign it to a keyboard shortcut (or use the ribbon) and apply it to the paragraph in question.
__________________
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
  #6  
Old 01-20-2017, 09:02 AM
jmltinc jmltinc is offline Underline Inserted Text Windows 7 64bit Underline Inserted Text Office 2016
Novice
Underline Inserted Text
 
Join Date: Jan 2017
Posts: 9
jmltinc is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
Create or modify a paragraph style that has the characteristics you want, assign it to a keyboard shortcut (or use the ribbon) and apply it to the paragraph in question.
I am sorry, but I am confused. This is done in VBA, so why the keyboard shortcut or Ribbon?

The code creates title text which I want underlined, followed below by some more text (not underlined) or a table. I am trying to underline (by code) the title text only, immediately after creating it or as it is created.

Thanks,
-John
Reply With Quote
  #7  
Old 01-20-2017, 09:54 PM
gmayor's Avatar
gmayor gmayor is offline Underline Inserted Text Windows 10 Underline Inserted Text Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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

My point was that styles are the best way of formatting text. i.e. format (say) Heading 2 style to be 16 point Calibri Bold, Underlined and apply that to the paragraph that is the table title.

Using VBA you have to identify the variable texts to be formatted and you have not explained enough to be able to do that however ....

If the table title is the paragraph immediately before each table then

Code:
Sub U_LineTableTitles()
Dim oRng As Range
Dim oTable As Table
    For Each oTable In ActiveDocument.Tables
        Set oRng = oTable.Range.Previous.Paragraphs(1).Range
        With oRng
        .Font.Name = "Calibri"
        .Font.Size = 16
        .Font.Bold = True
        .Font.Underline = wdUnderlineSingle
        'Or instead of the font commands
        '.Style = "Heading 2"
        .Collapse 0
        End With
    Next oTable
End Sub
As you have not explained how the tables and their titles are created then it is not possible to be specific about how to format the titles in your original code.
__________________
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
  #8  
Old 01-22-2017, 12:10 PM
jmltinc jmltinc is offline Underline Inserted Text Windows 7 64bit Underline Inserted Text Office 2016
Novice
Underline Inserted Text
 
Join Date: Jan 2017
Posts: 9
jmltinc is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
If the table title is the paragraph immediately before each table then
Thank you. As I said the Word object model is not something I am intimately familiar with yet and am frankly confused by it. It is far more complex than Access.

Having said that, I do not quite grasp the paragraph method. When I use a paragraph to separate text from the table (Selection.TypeParagraph), I get a blank line; which I do not want. So I have:
Code:
        Selection.TypeParagraph
 
'Product Specifications
        Selection.Text = " Product Specifications"
        Selection.Font.Name = "calibri"
        Selection.Font.Size = 16
        Selection.Font.Bold = True
        'Selection.Font.Underline = wdUnderlineSingle
        Selection.EndKey Unit:=wdStory
 
        .Tables.Add Range:=Selection.Range, NumRows:=3, NumColumns:=4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
        With .Tables(x)
Since I am not using TypeParagraph between the text and the table, I am assuming (perhaps falsely) that I haven't closed the paragraph and your code would underline the table text as well.

Obviously, I haven't tried it yet. From my tardy response to your kind advice, you can see I have been pulled in other directions at work right now. I will try your method and hopefully my thought that I haven't closed the paragraph is incorrect.

I would appreciate any explaination of the TypeParagraph method and its proper use...

Thank you,
-John
Reply With Quote
  #9  
Old 01-23-2017, 02:13 AM
gmayor's Avatar
gmayor gmayor is offline Underline Inserted Text Windows 10 Underline Inserted Text Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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

TypeParagraph will insert a paragraph break. You don't need an extra one. There will be one at the end of the paragraph before your table. If you want more space add it to the paragraph.

It is better to set ranges and work with ranges and name the tables if you wish to process them e.g.

Code:
Dim oRng As Range
Dim oTable As Table
    Set oRng = Selection.Range
    With oRng
        'Product Specifications
        .Text = " Product Specifications"
        .Font.Name = "Calibri"
        .Font.Size = 16
        .Font.Bold = True
        .Collapse 0    'collapse the range to its end
        'Insert a table at the range
        Set oTable = .Tables.Add(Range:=oRng, NumRows:=3, _
                                 NumColumns:=4, _
                                 DefaultTableBehavior:=wdWord9TableBehavior, _
                                 AutoFitBehavior:=wdAutoFitFixed)
        .End = oTable.Range.End + 1    'set the end of the range to just beyond the table
        'do stuff with otable
        .Collapse 0    'collapse the range to its end
        'Add another heading
        .Text = " Another heading"
        .Font.Name = "Calibri"
        .Font.Size = 16
        .Font.Bold = True
        .Collapse 0    'collapse the range to its end
        'Insert a table at the range
        Set oTable = .Tables.Add(Range:=oRng, NumRows:=3, _
                                 NumColumns:=4, _
                                 DefaultTableBehavior:=wdWord9TableBehavior, _
                                 AutoFitBehavior:=wdAutoFitFixed)
        .End = oTable.Range.End + 1    'set the end of the range to just beyond the table
        'do stuff with otable
        .Collapse 0    'collapse the range to its end
        .Select    'moves the cursor to the end of the table
    End With
__________________
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
  #10  
Old 02-08-2017, 08:10 AM
jmltinc jmltinc is offline Underline Inserted Text Windows 7 64bit Underline Inserted Text Office 2016
Novice
Underline Inserted Text
 
Join Date: Jan 2017
Posts: 9
jmltinc is on a distinguished road
Default

I never posted a reply.

Thank you Graham.

-John
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing a comment with underline or other formatting (or adding that formatting to commented text) paulkaye Word 4 05-16-2016 05:38 AM
Underline Inserted Text Is there a way of having text automatically inserted based on the selection of other text? MiniMum97 Word 2 04-12-2016 06:09 AM
Copy Underline text from Word and Paste into excel rfaris Excel Programming 7 10-05-2015 05:56 AM
Underline Inserted Text Select text to be underline and bold ksigcajun Word VBA 1 03-16-2015 07:51 PM
How to remove the UNDERLINE from a hyperlink text? Learner7 PowerPoint 3 05-17-2010 09:35 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:38 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