0

I am working on drawing a series of text boxes into a publisher file (via Publisher shapes) from an Excel sub (sample code below).

The problem I am running into is that I can't seem to declare a Font and reuse it for later, so I have to change the font, text size, color, bold, and alignment individually for each text box.

My question is: Is there a way to save a font as a variable, and set all text properties of a shape by simply assigning the font variable? How do I do this?

'Values
Dim ptab0v, ptab1v, ptab2v, ptab3v, ptab4v, ptab5v, ptab6v, ptab7v, ptab8v, ptab9v, ptab10v, ptab11v As String
'Shapes
Dim ptab0s, ptab1s, ptab2s, ptab3s, ptab4s, ptab5s, ptab6s, ptab7s, ptab8s, ptab9s, ptab10s, ptab11s As Publisher.Shape

ptab0v = "filler"

Set ptab0s = appPub.ActiveDocument.Pages(5).Shapes.AddTextbox _
(Orientation:=msoTextOrientationHorizontal, _
Left:=conv * 0.82, Top:=conv * 2.77, Width:=conv * 1.43, Height:=conv * 0.29)

ptab0s.TextFrame.TextRange.Text = ptab0v

'---I WANT THE FOLLOWING LINES TO BE SIMPLIFIED TO ONE LINE---
ptab0s.TextFrame.TextRange.Font.Name = "Monsterrat"
ptab0s.TextFrame.TextRange.Font.Size = 14
ptab0s.TextFrame.TextRange.Font.Color.RGB = RGB(255, 255, 255)
ptab0s.TextFrame.TextRange.Font.Bold = msoTrue
ptab0s.TextFrame.TextRange.ParagraphFormat.Alignment = pbParagraphAlignmentCenter
Robert Todar
  • 2,085
  • 2
  • 11
  • 31
Path
  • 37
  • 7
  • 1
    `ptab1s.TextFrame.TextRange.Font = ptab0s.TextFrame.TextRange.Font`, `ptab2s.TextFrame.TextRange.Font = ptab0s.TextFrame.TextRange.Font`, `ptab3s.TextFrame.TextRange.Font = ptab0s.TextFrame.TextRange.Font`?.. `Dim f As Font : Set f = ptab0s.TextFrame.TextRange.Font : ptab1s.TextFrame.TextRange.Font = f : ptab2s.TextFrame.TextRange.Font = f : ptab3s.TextFrame.TextRange.Font = f`?.. – GSerg Nov 18 '20 at 18:48
  • I was hoping I could save something like perhaps a TextFrame variable with the different font, alignment etc settings all saved to it. Then I could Just do ptab0s.TextFrame = presetTextFrame0 or something like that – Path Nov 18 '20 at 18:49
  • Like with the `f`? – GSerg Nov 18 '20 at 18:51
  • Oh sorry, yes just like that. I will test it out real quick. – Path Nov 18 '20 at 18:54
  • @GSerg it worked wonderfully. I posted an answer of my own to preserve the syntax, but if you would like to post your version as well, I can give you credit for the answer. – Path Nov 18 '20 at 19:20
  • you can also pull it into a different function (which will accomplish the one liner request) or use `with` to get rid of the redundancy. – Brad Nov 18 '20 at 19:24

1 Answers1

0

(Simplified for readability) You can do this by setting a font variable (in my case, a Publisher.Font type)

For example:

Dim ptab0v, ptab1v, ptab2v As String
Dim ptab0s, ptab1s, ptab2s As Publisher.Shape
Dim ptabFont As Publisher.Font

ptab0v = "filler"
ptab1v = "filler"
ptab2v = "filler"

Set ptab0s = (set shape)
        ptab0s.TextFrame.TextRange.Text = ptab0v ' set value

        'Font settings
        ptab0s.TextFrame.TextRange.Font.Name = "Monsterrat"
        ptab0s.TextFrame.TextRange.Font.Size = 14
        ptab0s.TextFrame.TextRange.Font.Color.RGB = RGB(153, 30, 32)
        ptab0s.TextFrame.TextRange.Font.Bold = msoTrue
        
        Set ptabFont = ptab0s.TextFrame.TextRange.Font



Set ptab1s = (set shape)
        ptab1s.TextFrame.TextRange.Text = ptab1v ' set value

        'reuse font
        ptab1s.TextFrame.TextRange.Font = ptabFont

Set ptab2s = (set shape)
        ptab2s.TextFrame.TextRange.Text = ptab2v ' set value

        'reuse font
        ptab2s.TextFrame.TextRange.Font = ptabFont

Path
  • 37
  • 7