Set PPT Font to 'Body' Type - vba

I am not able to paste the entirety of my code, but the crux is that I have a textbox in PPT 2013, myTb, that I have (programmatically) pasted some text into. I now want to perform the following two actions:
See if the original text was the PPT 'body default' font (e.g. 'Calibri (body)' vs. 'Calibri' in the MS Ribbon)
If it was the body default, set the new text to also be the body default.
I can't seem to figure either part out, even though I have experimented with reading/writing from/to most of the Shape.TextFrame[n].TextRange.Font.Name... fields. I also had two confounding points to ask about, regarding the Shape.TextFrame.TextRange.Font.NameComplexScript field:
This field does not seem to be a 'complete' indicator of body-default vs non-body-default font. The reason is that if this textbox is originally the body-default ('Calibri (body)'), it will read '+mn-cs', but then I can change the font to the non-default variant ('Calibri'), and it still reads '+mn-cs'.
I then proceed to change the textbox to an entirely different font, which changes this field to the font's name as expected. However, if I then change back to the body-default font (or any other font, for that matter), this field remains on the previous font's name.

To set the font to the body or heading font, you need to use a weird bit of syntax:
{object}.Font.Name = "+" + FontType + "-" + FontLang
Where:
FontType is "mj" or "mn" for Major (headings) or Minor (body) respectively.
FontLang is "lt", "cs" or "ea" for Latin, Complex Scripts or East Asian
For example to set the font to the theme's body text font for Latin text:
ActivePresentation.Slides(1).Shapes(1).TextFrame.TextRange.Font.Name = "+mn-lt"

A bit late to the party, but I found this in case anyone needs it.
Edit: This is C# code, using the Interop PowerPoint namespace.
string headingsThemeFont = Globals.ThisAddIn.Application.ActivePresentation.SlideMaster.Theme.ThemeFontScheme.MajorFont.Item(MsoFontLanguageIndex.msoThemeLatin).Name;
string bodyThemeFont = Globals.ThisAddIn.Application.ActivePresentation.SlideMaster.Theme.ThemeFontScheme.MinorFont.Item(MsoFontLanguageIndex.msoThemeLatin).Name;

Related

Using formatted text from Word table to Publisher

I'm trying (and failing) to fill out a text box (TextFrame) in Publisher using a macro from content in a Word table. I'm trying to do something along the lines of:
With doc.Pages(1).shapes(1)
.GroupItems.Item(8).TextFrame.TextRange = table.Cell(2, 3).Range.FormattedText
End With
The source text from table has a bunch of font formatting that I need in the text box but it won't seem to be able to copy over the formatting and I just get the plain text. Any ideas on how to get this working properly?
Edit: It seems like TextFrame can't accept formatted text at all. Is there any way around this?
In Word TextFrame.TextRange returns a range which has a FormattedText property. The usage for that would be:
.GroupItems.Item(8).TextFrame.TextRange.FormattedText = table.Cell(2, 3).Range.FormattedText
In your code you haven't specified which property of the TextRange you want to assign the formatted text to. This means it will be assigned to the default property, Text, which is just a string and cannot contain any formatting.
Golden Rule: never rely on default properties, always specify the
property you want to use.
Given that you appear to be taking a value from Word into Publisher you should be looking at the documentation for VBA in Publisher which shows you that the TextRange object in Publisher does not have a FormattedText property, so you cannot take formatting across using this method.

Update [Style] to Match Selection option on Character Style via macro

I'm having an issue with some character style that doesn't reflect the style change when applied (this doesn't happen always):
Example: I have a character style for italics, and when I apply the character style, the word still appears as normal (but the char style is applied, and checking the properties, the style has the check ok for italics).
In order to fix this, I select the word with the issue, right click on the style and use the option "Update [StyleName] to match selection", and it displays the italics correctly.
The problem here is that when I try to replicate this behavior with a VBA Macro (via recording macro), the macro that Word writes has this error:
Run-time error '5900': The property is not allowed for character styles. This is the line with error:
ActiveDocument.Styles("StyleItalic").AutomaticallyUpdate = False
Looking at the code created via Macro, seems that it is not possible for a character style to be automatically updated.
The character style is also created via macro, and I can't see anything wrong in the style:
Private Sub Creo(style As String, fontName As String, fontSize As Integer, hasItalic As Boolean)
On Error Resume Next
Selection.ClearFormatting
ActiveDocument.Find.style = ActiveDocument.Styles(style)
ActiveDocument.Find.Execute
If ActiveDocument.Find.Found = False Then
ActiveDocument.Styles.Add name:=style, Type:=wdStyleTypeCharacter
ActiveDocument.Styles(style).QuickStyle = True
ActiveDocument.Styles(style).font.Size = fontSize
ActiveDocument.Styles(style).font.name = fontName
ActiveDocument.Styles(style).font.Italic = hasItalic
End If
End Sub
Is there a way to fix this? Hope I explain myself. I am working with 1000+ pages Word Document, so this becomes a bit too tedious for manual editing. Also, sometimes the style in other word works ok, but other doesn't. (All the words have both paragraph style and character style)
Thanks!
Your question describes two unrelated issues.
Applying a character style with the same property as the underlying
paragraph style will cause that property to be turned off in the
text. Updating the character style to match the selection will have
the opposite effect to what you want.
To demonstrate: in a new document type a paragraph of text and apply
a style that is defined as italic, e.g. Quote or Intense Quote.
Select the whole paragraph and press Ctrl+I to turn off italics. Now
select just part of the text and apply the character style named
Emphasis. You will see that it has no apparent effect on the text.
This is because both the paragraph style and the character style are
italic, cancelling each other out and having the same effect as
turning italics off manually.
Now right click on Emphasis and select Update to Match Selection.
The selected text will now be italic but, as the text preview in the
Quick Styles gallery will show you, Emphasis is no longer italic.
Only paragraph and linked styles have the Automatically Update
property, which is why you get an error when attempting to set it on a character style.

(MS Word) Removing character style IF text has specific paragraph style applied

My Google-fu must be very weak today, ’cause this seems like an obvious thing to need to do sometimes, yet I cannot find a single case of anyone ever asking about it anywhere…
I have a document that I am preparing for proper typesetting in InDesign, which includes among other things getting rid of local overrides to paragraph and character styles. I did an find-and-replace to replace all instances of italic text with a character style called Italic, but stupidly forgot to limit this to text with the Normal style applied.
There are hundreds of headers strewn throughout the document which are supposed to be italic; that’s part of their paragraph style definitions. Since I forgot to limit the find-and-replace, the Italic style was applied to all these many headers. Annoyingly, since ‘italic’ is something like a boolean switch in Word, this means that all these headers are now not italic in the document.
I didn’t notice this for a while, so I can’t simply undo it now—the file has been saved and worked on since the find-and-replace. The author (who is a cantankerous, octogenarian technophobe) also needs to see the file again before it’s set, so while ultimately it doesn’t matter whether or not the font is italic in the Word document, it will matter to him.
So what I would very much like to do is to search for all text which has both the paragraph style Header and the character style Italic applied, and remove the character style.
This is an easy task in InDesign where paragraph and character styles are separate entities, but not in Word where they’re all lumped together in one big, messy pile. It doesn’t seem like it can be done through the UI, so I’m guessing I’ll have to resort to a VBA macro… which I’m utterly incompetent at.
Is there a way to find text with a particular paragraph style and a particular character style, and then remove the character style from that text?
here is some code to get you started
press F5 to run code, it will stop at Stop command
examine the Immediate window to determine the header style
each paragraph gets selected, so that you can tell which one you are examining
you can then modify the code with if/then statement to make specific paragraphs italic
Sub aaaa()
Dim ppp As Paragraph
Dim ccc As Range
For Each ppp In ActiveDocument.Content.Paragraphs
ppp.Range.Select ' visual aid only. not used by any other part of the program
Debug.Print "style :", ppp.Style
Debug.Print ppp.Style.Description
Stop
For Each ccc In ppp.Range.Characters ' you can probably comment out these 3 lines
Debug.Print ccc.FormattedText.Italic ' True prints as -1
Next ccc
Debug.Print "italic :", ppp.Range.Italic ' prints -1 if all are italic. 9999999 if some. 0 if none
ppp.Range.Italic = False ' this removes italic from whole paragraph
Next ppp
End Sub

Word 2003 vba cannot change bookmark font

The following code does not work
objDoc.Bookmarks("SomeBookMark").Range.Font.Bold = True
objDoc.Bookmarks("SomeBookMark").Range.Text = GetSomeText()
objDoc.Bookmarks("SomeBookMark").Range.Font.Bold = True
When I run the code the text that is retrieved by GetSomeText() still has the default font.
Why don't you just style the bookmarks?
I searched for this in the past and also didn't find a proper solution back then.
However, since a bookmark represents a fixed field in a text template, usually the format shouldn't change so easily.
I'm sorry to put this in the reply field, but I can't find the "comment" button :-s

Copy Special Symbols From One PowerPoint Presentation to Another

I need to copy text from one PowerPoint presentation to another. However, I have problems copying special symbols, such as smileys, which appear in the target presentation as empty boxes. Looking at the Open XML file in the original presentation, I can see that the Run containing the smiley has a "SymbolFont" attribute:
<a:sym typeface="Wingdings" />
However, in VBA, Shape.TextFrame2.TextRange2.Font =the Font of that Run - shows Arial.
How can I determine the SymbolFont of a text Run using VBA or C# (not XML)?
Then I could specify that SymbolFont in the target presentation.
Perhaps there other ways for copying the text that do not involve XML?
Note that this problem happens not only with Smileys; other special characters may show different SymbolFonts, such as:
<a:symTypeface = "Symbol", PitchFamily = 18, CharacterSet = 2>
Code example:
getRuns(TextRange2 paragraph)
{
foreach(TextRange2 run in paragraph.get_Runs(-1,-1))
_myRuns.Add(new MyRun {_text=run.Text, _font=run.Font} );
}
copyRunsToParagraph(TextRange2 paragraph)
{
foreach(MyRun run in _myRuns)
paragraph=paragraph.InsertAfter(run._text);
}
Note: Run.Font seems to return only the Latin font, not the Symbol font, e.g., Arial but not Wingdings. As I wrote, different symbols may have different SymbolFonts, so always using Wingdings does not work.
This can't be done in VBA.