Vba 操作TextRange时Mac与Windows在线条/段落方面的差异

Vba 操作TextRange时Mac与Windows在线条/段落方面的差异,vba,macos,powerpoint,Vba,Macos,Powerpoint,更新 我发现vbCrLf是mac上的vbLf,并得出了相同的结论。现在我还发现,在Mac中,TextRange被解释为行而不是段落 为了创建议程,然后删除前两段,我需要以下代码: With ActivePresentation.SectionProperties MsgBox "We gather now the Section headers" For iSectIn

更新

我发现vbCrLf是mac上的vbLf,并得出了相同的结论。现在我还发现,在Mac中,TextRange被解释为行而不是段落

为了创建议程,然后删除前两段,我需要以下代码:

                    With ActivePresentation.SectionProperties
                    MsgBox "We gather now the Section headers"
                        For iSectIndex = 1 To .Count
                        If ActivePresentation.SectionProperties.Name(iSectIndex) <> "" Then
                            #If Mac Then

                            sSectionCollector = sSectionCollector & vbLf & ActivePresentation.SectionProperties.Name(iSectIndex)
                            #Else
                            sSectionCollector = sSectionCollector & vbCrLf & ActivePresentation.SectionProperties.Name(iSectIndex)
                            #End If
                        End If
                        Next iSectIndex
                    End With

                    sAgendaTextblock.TextFrame2.TextRange.Text = sSectionCollector

                    #If Mac Then
                    MsgBox "starting to delete"
                    MsgBox "line 1: " & sAgendaTextblock.TextFrame2.TextRange.Paragraphs(1).Lines(1).Text
                    sAgendaTextblock.TextFrame2.TextRange.Paragraphs(1).Lines(1, 2).Delete

                    #Else
                    MsgBox "starting to delete"
                    MsgBox "paragraph 1: " & sAgendaTextblock.TextFrame2.TextRange.Paragraphs(1).Text
                    sAgendaTextblock.TextFrame2.TextRange.Paragraphs(1, 2).Delete
                    #End If

问题就在这里:
&vbCrLf&

这是Windows上的换行符(
Chr$(10)
&
Chr$(13)
),但Mac使用Linux风格的换行符,如果您的VBA代码需要在两种平台上运行,那么最简单的方法就是用
vbNewLine
替换
vbCrLf

vbNewLine
在Windows上是
vbCrLf
,在Mac上是
vbCr

平台专用新线字符;以适用于当前平台的为准


这就是Mac上出现双线的原因。为
vbNewLine
更改硬编码的特定于Windows的
vbCrLf
将修复行尾,解决问题。

侧注。。。为什么要使用
单曲
?不应该使用PowerPoint对象模型,但是如果 Stand属性是可迭代的,在ActudioPersiod中考虑每个P的“代码”> St回事属性而不是<代码>……对象集合在
For Each
循环中工作得更好。与Windows版本相比,PowerPoint For Mac VBA对象模型缺少很多片段。我会测试您的代码,但您只发布了一个非运行的代码片段。您好@JohnKorchok谢谢-是的-我知道“”和“”,但我找不到更多。将粘贴整个代码(希望能够轻松发现要点)谢谢@BigBenYES!我发现在我的调试过程中,我现在正在处理这个问题,还有更多的问题,很明显在MAC中,段落数只有1,并且它被表示为行,这就是为什么我不能删除第一行!Grr,我不接受你的回答,因为我没有名声。现在我明白了:“您最近提出了4个问题,其中一些问题没有得到社区的很好响应。每个人都按照自己的节奏学习,犯一些错误也没关系。然而,到目前为止,你的问题受到的接待可能最终会阻止你的账户完全提问。“哈哈哈——我想我应该远离这个世界的游乐场。”pros@G.N.MS…欢迎来到堆栈溢出,我猜!!)——我确实投票赞成了,但是考虑编辑这个问题。清理/去除不相关的绒毛;这个问题不仅会帮助你,还会继续帮助其他面临类似问题的人。通过将他们视为你的读者来帮助他们找到它!每次编辑都会将文章推回到头版,因此值得一提:额外的关注可能会让人阅读你的文章和文章(请看,无需跟踪帖子本身的变化),并提高努力程度!”每次编辑都会将帖子推回到头版,因此值得一提:“天哪,我真的很抱歉。谢谢你这么多提示!
    Sub CreateAgendaWithSegments()
          'TODO DOCU
          'TODO Implement Button
          Dim oSl As Slide
          Dim oPl As Presentation
          Dim sAgendaCnt As Long
          Dim sAgendaTextblock As Shape
          Dim iSectIndex As Single
          Dim sSectionCollector As String
          Dim NewAgenda As Slide
          Dim AgendaLayout As CustomLayout

          'TODO reinstall ErrorHandler
10        On Error GoTo ErrorHandler

20        If ActivePresentation.SectionProperties.Count < 2 Then
30            MsgBox "You seem to have not segmented/sectioned your presentation - therefore we can not create an automated agenda slide for you -- sorry." & vbCrLf _
                  & "Consider using the SEGMENT tools first.", vbOKOnly Or vbExclamation, "No Segments"

40            GoTo Ende
50        End If
          'Collect Section Titles


          'Search for Agenda Slide
60        Set oPl = ActivePresentation
70        For Each oSl In oPl.Slides

80            If oSl.CustomLayout.Name = "AGENDA" Then

AgendaContent:
90                sAgendaCnt = sAgendaCnt + 1
100               sAgendaIndex = oSl.SlideIndex
110               oSl.Select
120               Call ExcelWork_2020.Delay(0.5)

                  'Do the magic
                  'First Reset
130               DoEvents
140               Application.CommandBars.ExecuteMso ("SlideReset")
150               DoEvents

                  'find the Textblock
160               oSl.Shapes(2).TextFrame.TextRange.Text = "Agenda"
170               Set sAgendaTextblock = oSl.Shapes(1)
180               With sAgendaTextblock.TextFrame2
190                   If .HasText Then
200                   Debug.Print sAgendaTextblock.TextFrame2.TextRange.Text

210                       Select Case MsgBox("Your agenda slide has already text. Are you sure you want to overwrite this with the new headlines from the Segmentation?", vbOKCancel Or vbExclamation, "Agenda has text")
                             Case vbCancel
220                          GoTo Ende
230                          Case vbOK
                              'Continue
240                           End Select
250                   End If 'Even if there is no text, we will write now.
                                  'Call SectionWriter
260                               With ActivePresentation.SectionProperties
270                                   For iSectIndex = 1 To .Count
280                                       sSectionCollector = sSectionCollector & vbCrLf & ActivePresentation.SectionProperties.Name(iSectIndex)
290                                   Next iSectIndex
300                               End With
310                               sAgendaTextblock.TextFrame2.TextRange.Text = sSectionCollector
320                               sAgendaTextblock.TextFrame2.TextRange.Paragraphs(1, 2).Delete
330                               GoTo Ende
                      'End If
340               End With

350           End If
360       Next oSl

          ' No Agenda found - we create one
370           Set AgendaLayout = ActivePresentation.Designs(1).SlideMaster.CustomLayouts(6)
380           Set NewAgenda = ActivePresentation.Slides.AddSlide(2, AgendaLayout)
390           Set oSl = NewAgenda


400       GoTo AgendaContent:

410       GoTo Ende

ErrorHandler:
420       MsgBox "Something went wrong -- maybe you did not select the right object for this task? If you can't find the problem, send a mail to nik@xex.one with a short description of what you tried to achieve - we will get back to you as soon as possible", vbOKOnly Or vbExclamation, "Error"

Ende:
End Sub