我创建了代码来在excel报告中设置分页符来处理孤儿问题(即一行文本溢出到下一页等)。当我在报告打开/可见时运行它时,代码工作正常。
它是打开的大型应用程序的一部分,代码从MS Access执行。在提高性能的过程中,Excel不可见。
当我从MS Access运行我的代码时,它不再有效...它不会产生错误,只是忽略了实际的分页设置命令。
我在各种forums上读到,为了避免这个问题,需要先将excel切换到ActiveWindow.View = xlPageBreakPreview
,但这也不起作用(我怀疑是因为Excel不可见)。
我已经测试了以下内容:
当Excel在后台运行时,如何让Excel通过代码更改分页?
这是我的代码:
Sub TheOrphanProblem()
Dim iPageBrkRow
'Determine if there are page breaks and if so on which row of the document
If FindNthAutoPageBreak(wsRptHolding, 1) Is Nothing Then
'No pagebreak found so we exit the sub
Exit Sub
Else
iPageBrkRow = FindNthAutoPageBreak(wsRptHolding, 1).Row 'Get row
End If
Debug.Print iPageBrkRow
Dim x As Integer
Dim sCase As String
Dim rNewposition As Range
With wsRptHolding
为了简洁,编辑了代码。此部分检查是否存在孤立问题,并在需要时查找分页符的新位置。 然后它将该位置提供为称为“rNewposition”的范围。
'Moves page break to calculated position
ActiveWindow.View = xlPageBreakPreview
.HPageBreaks.Add rNewposition
ActiveWindow.View = xlNormalView
End With
End Sub
这是我用来查找分页位置的代码......
Private Function FindNthAutoPageBreak(Sht As Worksheet, Nth As Long) As Range
'Set page break of the last page so that sub asset groups are kept together
Dim HP As HPageBreak
Dim Ctr As Long
For Each HP In Sht.HPageBreaks
If HP.Type = xlPageBreakAutomatic Then
Ctr = Ctr + 1
If Ctr = Nth Then
Set FindNthAutoPageBreak = HP.Location
End If
End If
Next HP
End Function
答案 0 :(得分:0)
试试这个
ActiveSheet.DisplayPageBreaks = True