开发者

PowerPoint 2007 - Set language on tables, charts etc that contains text

So I've got this macro that basically scans each slide in PowerPoint and sets the specified language. Works great. Howerver, it skips containers that aren't text boxes. I'd like it to apply the language on tables, smartart, charts etc. Basically anything that may contain text.

Is this even possible? This is the current code:

Public Sub changeLanguage()

    On Error Resume Next

    'lang = "English"
    lang = "Norwegian"

    'Determine language selected
    If lang = "English" Then
            lang = msoLanguageIDEnglishUK
    ElseIf lang = "Norwegian" Then
            lang = msoLanguageIDNorwegianBokmol
    End If

    'Set default language in application
    ActivePresentation.DefaultLanguageID = lang

    'Set language in each textbox in each slide
    For 开发者_Go百科Each oSlide In ActivePresentation.Slides
        Dim oShape As Shape

        For Each oShape In oSlide.Shapes
            oShape.Select
            oShape.TextFrame.TextRange.LanguageID = lang
        Next
    Next

End Sub


Yeah, it's not all that intuitive in PowerPoint, but it can be done. Basically, there are 3 major types of shapes (simple, grouped and tables). This code will check them all:

Public Sub changeLanguage()
    On Error Resume Next
    Dim gi As GroupShapes '<-this was added. used below
    'lang = "English"
    lang = "Norwegian"
    'Determine language selected
    If lang = "English" Then
        lang = msoLanguageIDEnglishUK
    ElseIf lang = "Norwegian" Then
        lang = msoLanguageIDNorwegianBokmol
    End If
    'Set default language in application
    ActivePresentation.DefaultLanguageID = lang

    'Set language in each textbox in each slide
    For Each oSlide In ActivePresentation.Slides
        Dim oShape As Shape

        ' Sets the language for the notes page as well.
        Dim oShape As Shape
        For Each oShape In oSlide.NotesPage.Shapes
            oShape.Select
            oShape.TextFrame.TextRange.LanguageID = lang
        Next

        For Each oShape In oSlide.Shapes
            'Check first if it is a table
            If oShape.HasTable Then
                For r = 1 To oShape.Table.Rows.Count
                    For c = 1 To oShape.Table.Columns.Count
                    oShape.Table.Cell(r, c).Shape.TextFrame.TextRange.LanguageID = lang
                    Next
                Next
            Else
                Set gi = oShape.GroupItems
                'Check if it is a group of shapes
                If Not gi Is Nothing Then
                    If oShape.GroupItems.Count > 0 Then
                        For i = 0 To oShape.GroupItems.Count - 1
                            oShape.GroupItems(i).TextFrame.TextRange.LanguageID = lang
                        Next
                    End If
                'it's none of the above, it's just a simple shape, change the language ID
                Else
                    oShape.TextFrame.TextRange.LanguageID = lang
                End If
            End If
        Next
    Next
End Sub


Although a bit of time passed ... came here from https://superuser.com/questions/432366/how-do-i-change-the-language-of-all-powerpoint-slides-at-once. Since I prefer working with python, a python version using the win32com package would be:

infile_name = 'drive:/path/to/in.pptx'
outfile_name = 'drive:/path/to/out.pptx'

target_language = 1031 # find in language list, here German

import time
import win32com.client


ppt = win32com.client.Dispatch('PowerPoint.Application')
ppt.Visible = True

presentation = ppt.Presentations.Open(infile_name)

def change_all_subshapes(target_shape, language_id):
    if target_shape.HasTextFrame:
        target_shape.TextFrame.TextRange.languageID = language_id

    if target_shape.HasTable:
        for r in range(1, target_shape.Table.Rows.Count + 1):
            for c in range(1, target_shape.Table.Columns.Count + 1):
                target_shape.Table.Cell(r, c).Shape.TextFrame.TextRange.LanguageID = language_id

    # look the constants msoGroup and msoSmartArt up
    if target_shape.Type in [6, 24]:
        for i in range(1, target_shape.GroupItems.Count + 1):
            change_all_subshapes(target_shape.GroupItems.Item(i), language_id)

# necessary: hopefully ppt is open after a second
time.sleep(1)

print('all slides')
for i in range(1, presentation.Slides.Count + 1):
    for j in range(1, presentation.Slides(i).Shapes.Count + 1):
        change_all_subshapes(presentation.Slides(i).Shapes(j), target_language)

print('master shapes')
for i in range(1, presentation.SlideMaster.CustomLayouts.Count + 1):
    for j in range(1, presentation.SlideMaster.CustomLayouts(i).Shapes.Count + 1):
        change_all_subshapes(presentation.SlideMaster.CustomLayouts(i).Shapes(j), target_language)

presentation.SaveAs(outfile_name)
presentation.Close()
ppt.Quit()


I had the same problem when doing macro for changing language in all shapes. As far as I learned, in PPT2007 it is not possible to set language on the objects such as Charts and SmartArt, programatically. There is no access to set languageID in VBA on these objects. However, changing language by clicking on the SmartArt object or Chart object works.

For the other objects:

  • grouped items - I had to traverse programatically (as on example in Otaku's post) through all objects in a group to set a language
  • table - I traversed through all cells.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜