PowerPoint 2007 - 在包含文本的表格,图表等上设置语言

时间:2011-01-19 13:27:23

标签: vba ms-office powerpoint powerpoint-vba

所以我有这个宏基本上扫描PowerPoint中的每张幻灯片并设置指定的语言。效果很好。但是,它会跳过不是文本框的容器。我希望它能在桌子,智能手机,图表等上应用这种语言。基本上任何可能包含文本的东西。

这甚至可能吗?这是当前的代码:

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

3 个答案:

答案 0 :(得分:8)

是的,在PowerPoint中并不是那么直观,但可以做到。基本上,有三种主要类型的形状(简单,分组和表格)。此代码将全部检查:

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

答案 1 :(得分:0)

在执行宏以更改所有形状的语言时,我遇到了同样的问题。 据我所知,在PPT2007中,无法以编程方式在图表和SmartArt等对象上设置语言。无法在这些对象的VBA中设置languageID。但是,通过单击SmartArt对象或Chart对象来更改语言。

对于其他对象:

  • 分组项目 - 我必须通过编程遍历(例如Otaku的帖子中的示例)通过组中的所有对象来设置语言
  • table - 我遍历了所有细胞。

答案 2 :(得分:0)

虽然时间过了一点......来自https://superuser.com/questions/432366/how-do-i-change-the-language-of-all-powerpoint-slides-at-once。由于我更喜欢​​使用python,使用win32com包的python版本将是:

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()