Solved

VBScript in Powerpoint - Change Formatting in Text Box on a line-by-line basis

Posted on 2016-09-10
  • VB Script
  • MS PowerPoint
  • VBA
  • MS Applications
  • MS Office
  • +1
9
60 Views
Last Modified: 2016-09-14
I want to be able to paste text and change the formatting of certain lines in an existing text box, named "author", on my powerpoint file.  I have a bank of authors that the user chooses from (1-10).  I have a Select Case setup to automatically assign values to variables I'm using to populate the text box with:

	
Case Else
  strEngName = "<author's name>"  'line 1
  strEngTitle = "<author's title>"  'line 2
  strPhone = "Office: <author's phone>"  'line 3
  strEmail = "Email: <author's email>"  'line 4

Open in new window


The first two lines are Black and Bold Italic, the next two lines are Gray and Italic.

The text box is existing and I'm just replacing placeholder text.
0
Comment
Question by:Mark DeMint
  • 4
  • 4
9 Comments
 
LVL 48

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 250 total points
ID: 41793772
Hi,

pls try something like this
Sub Macro()
    Set txtrng = ActivePresentation.Slides(1).Shapes(1).TextFrame2.TextRange
    aLines = Split(txtrng.Text, Chr(11))
    first2Lines = aLines(0) & Chr(11) & aLines(1) & Chr(11)
    For Idx = 2 To UBound(aLines)
        RemainingLines = RemainingLines & Chr(11) & aLines(Idx)
    Next
    RemainingLines = Right(RemainingLines, Len(RemainingLines) - 1)
    txtrng.Characters(1, Len(first2Lines)).Font.Bold = msoTrue
    txtrng.Characters(1, Len(first2Lines)).Font.Fill.ForeColor.RGB = RGB(0, 0, 0)
    txtrng.Characters(Len(first2Lines) + 1, Len(txtrng.Text) - Len(first2Lines)).Font.Italic = msoTrue
    txtrng.Characters(Len(first2Lines) + 1, Len(txtrng.Text) - Len(first2Lines)).Font.Fill.ForeColor.RGB = RGB(217, 217, 217)
End Sub

Open in new window

depending which return you used you'll have to change chr(11) into chr(13)

Regards
1
 

Author Comment

by:Mark DeMint
ID: 41797047
Thanks, Rgonzo1971!  Where would I use the variables I listed above: strEngName, strEngTitle, strPhone, strEmail?

The user will select from a dialog which user they are (1-10) then the text box will automatically place the text and format it.  So I've got the formatting like you gave me above, but where do I get it to use values assigned to these variables for the content?
0
 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 41797332
Without seeing the code I cannot say
0
 

Author Comment

by:Mark DeMint
ID: 41797425
What else do you need to see?  At the top I shared a generic case that sets a variable equal to a string.  I just want to take the code you supplied and set the content in each line equal to those variables.  Then I would have your code format the text.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 41797427
How do you reference the shape ?
Are the lines CHr(11) or CHr(13) separated?
0
 

Author Comment

by:Mark DeMint
ID: 41797431
Not at my work computer right now, but it was similar to how you referenced it, but I named the shape "author".

ActivePresentation.Slides(1).Shapes("author").TextFrame2

As far as the return goes I could use whichever is preferred.  I don't recall if it was chr(11) or chr(13), but I can change to either if need be.
0
 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 41797434
my code should be afterwards since it format the first 2 lines then the 2 remaining lines
0
 
LVL 23

Accepted Solution

by:
JSRWilson earned 250 total points
ID: 41798461
Maybe something based on this would work

Sub test()
Dim strEngName As String
Dim strEngTitle As String
Dim strPhone As String
Dim strEmail As String
Dim osld As Slide
Dim oshp As Shape
Dim P As Long

  strEngName = "<author's name>"  'line 1
  strEngTitle = "<author's title>"  'line 2
  strPhone = "Office: <author's phone>"  'line 3
  strEmail = "Email: <author's email>" 'line4
  Set osld = ActiveWindow.Selection.SlideRange(1)
  Set oshp = osld.Shapes("author")
  With oshp.TextFrame2.TextRange
.Text = strEngName & vbCrLf & strEngTitle & vbCrLf & strPhone & vbCrLf & strEngName
  For P = 1 To 2
  .Paragraphs(P).Font.Bold = True
  .Paragraphs(P).Font.Fill.ForeColor.RGB = vbBlack
  Next P
  For P = 3 To 4
  .Paragraphs(P).Font.Bold = False
  .Paragraphs(P).Font.Fill.ForeColor.RGB = RGB(127, 127, 127)
  Next P
  End With
End Sub

Open in new window

0
 

Author Closing Comment

by:Mark DeMint
ID: 41798633
Thank you both!  This was exactly what I was looking for.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
Learn the basics of inputting and editing your text components in Prezi. We will cover how to set styles, position, and group your text components. In your Prezi editor, click anywhere on the canvas to add text: A flashing cursor informs you that yo…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now