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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4735765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:32:33  来源:igfitidea点击:

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

vbams-officepowerpointpowerpoint-vba

提问by Kenny Bones

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.

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

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

回答by Todd Main

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:

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

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

回答by Sebastian

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 win32compackage would be:

虽然过了一段时间......从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()

回答by Peter Z. Prezentio

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.

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

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.
  • 分组项目 - 我必须以编程方式(如 Otaku 的帖子中的示例)遍历组中的所有对象以设置语言
  • table - 我遍历了所有单元格。