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
73 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 49

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 49

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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 49

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 49

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

This article will show you how to use shortcut menus in the Access run-time environment.
PowerPoint is the go-to presentation software for millions of users around the world. Many presentations use basic text features but you can really make special text jump out of your slide by applying this bubble text design process. This article ha…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

911 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