【问题标题】:Differences Mac vs Windows with Lines/Paragraphs when manipulating TextRange操作 TextRange 时 Mac 与 Windows 的差异与行/段落
【发布时间】:2020-08-18 23:47:30
【问题描述】:

更新

我发现 vbCrLf 是 mac 上的 vbLf -- 并且https://stackoverflow.com/users/1188513/mathieu-guindon 得出了相同的结论。现在我还发现,在 Mac 中,TextRange 被解释为 LINES 而不是 PARAGRAPHS。

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

                    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

旧代码/不再相关:

对不起,这肯定不漂亮 - 我是 VBA 初学者。此代码在 Windows 上完美运行,但在 Mac 上抛出 6/Overflow。我知道我可以用 #IF Mac 重新编程 - 但首先我需要了解为什么它会抛出该错误 - 当我尝试将其读入 sSectionCollector 字符串时,它似乎是第 280 行可能是 SectionProperties.Name

更新

代码现在作为插件运行,没有错误,但它产生了不同的结果 Windows Under Windows ok Under Mac it somehow doubles the lines 不幸的是,我无法在 VBAEditor 的插件中看到/步进/调试代码:-(


    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

【问题讨论】:

  • 旁注...您为什么使用Single?应该是Long
  • 不熟悉 PowerPoint 对象模型,但如果 SectionProperties 是可迭代的,请考虑使用类似 For Each p In ActivePresentation.SectionProperties 的东西代替 For...Next;对象集合在 For Each 循环中工作得更好。
  • 与 Windows 版本相比,PowerPoint for Mac VBA 对象模型有很多缺失的部分。我会测试你的代码,但你只发布了一个非运行的 sn-p。
  • 你好@JohnKorchok 谢谢 - 是的 - 我知道“docs.microsoft.com/en-us/office/vba/office-mac/…”和“youpresent.co.uk/developing-vba-powerpoint-mac”但我找不到更多。将粘贴整个代码(希望便于发现要点)
  • 谢谢@BigBen

标签: vba macos powerpoint


【解决方案1】:

问题出在这里:&amp; vbCrLf &amp;

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

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

docs.microsoft.com

平台特定的换行符;以适合当前平台的为准

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

【讨论】:

  • 是的!我现在在调试中发现了这一点——只是在做这个——而且还有更多——显然在 MAC 中,Paragraph.count 只有 1——它被理解为 LINES——这就是为什么我可以'不要删除第一行!
  • Grr,我对你的回答的支持不被接受,因为我没有声誉。
  • 现在我明白了:“你最近问了 4 个问题,其中一些问题没有得到社区很好的接受。每个人都按照自己的节奏学习,犯一些错误也没关系。但是,到目前为止,您的问题收到的反馈可能最终会阻止您的帐户完全提问。”哈哈哈 - 我想我应该远离专业人士的游乐场
  • @G.N.MS ...欢迎来到 Stack Overflow,我猜! ;-) 我确实对此表示赞同,但考虑编辑问题(和标题)以清理它/删除不相关的绒毛;这个问题不仅会帮助,还会继续帮助面临类似问题的其他人。将他们视为您的听众,帮助他们找到它!每次编辑都会将帖子重新放到首页,所以让它变得有价值:额外的关注可能会让某人阅读您的帖子和edit history(请参阅,无需跟踪帖子本身的更改),并为这些努力点赞!
  • “每次编辑都会将帖子撞回首页,所以让它变得有价值:” OMG - 很抱歉。并感谢您提供的许多提示!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-05
  • 2011-01-02
  • 2011-01-05
  • 2010-11-10
  • 1970-01-01
相关资源
最近更新 更多