将横向文件插入VBA合并文档会更改分节符。

huangapple go评论71阅读模式
英文:

Inserting Landscape file into VBA Combined Document changes Section Break

问题

我有一个Word VBA宏,它将文件夹中的Word文档合并为单个组合文件,将每个文档放入新的部分并保留原始文件的页眉和页脚。代码取消保护源文件,在目标文件的末尾插入分节符(下一页),将页面布局信息从源文档传输到新的目标部分,然后插入源文件。这些文件包含具有Word内容控件的表格,这些控件位于受保护的部分中(填写表单)。受保护部分的末尾包含一个分节符(下一页),然后是一个未受保护且没有内容控件的表格。

问题是,如果源文件是横向格式的,那么分节符会从"下一页"更改为"连续"。如果我插入两个"下一页"分节符,第二个会被更改为"连续"。手动删除"连续"分节符会将所有的"下一页"分节符都更改为"连续"。我正在使用Word365。我尝试过使用.InsertFile和.FormattedText,但结果都相同。我进行了广泛的研究,但找不到可行的解决方法。

引起问题的代码部分如下:

With DocTgt
      Set Rng = .Range.Characters.Last
      With Rng
        .Collapse wdCollapseEnd
         'Double section break required to prevent adding landscape files changing break 
         'from NextPage to Continuous
        .InsertBreak Type:=wdSectionBreakNextPage
        .InsertBreak Type:=wdSectionBreakNextPage 
       .Collapse wdCollapseEnd
        Call LayoutTransfer(DocSrc, DocTgt)
        '.FormattedText = DocSrc.Range.FormattedText
        .InsertFile FileName:=strFolder & "\" & strFile
      End With
...

我尝试过使用.InsertFile和.FormattedText,但结果都相同。我期望横向格式的文档能够像纵向格式的文档一样工作。

英文:

I have a Word VBA macro that combines Word documents in a file folder into a single combined file that puts each document in a new section and retains the header and footer of the original file. The code unprotects the source file, inserts a Section Break (Next Page) at the end of the Target file, transfers the page layout information from the source document to the new target section, then inserts the source file. The files contain tables with Word Content Controls which are in a protected section (Fill in forms). The end of the protected section contains a Section Break (Next Page) and then a table that is not protected and has no Content Controls.

The problem is if the source file is Landscape formatted the Section Break is converted from Next Page to Continuous. If I insert two Next Page breaks the second one is changed to continuous. Manually deleting the Continuous break changes all of the Next Page breaks to continuous. I'm using Word365. I have tried using both .InsertFile and .FormattedText but get the same result. I’ve researched extensively and am unable to find a fix that works.

Code section causing the problem

With DocTgt
      Set Rng = .Range.Characters.Last
      With Rng
        .Collapse wdCollapseEnd
         'Double section break required to prevent adding landscape files changing break 
         'from NextPage to Continuous
        .InsertBreak Type:=wdSectionBreakNextPage
        .InsertBreak Type:=wdSectionBreakNextPage 
       .Collapse wdCollapseEnd
        Call LayoutTransfer(DocSrc, DocTgt)
        '.FormattedText = DocSrc.Range.FormattedText
        .InsertFile FileName:=strFolder & "\" & strFile
      End With
...

I have tried using both .InsertFile and .FormattedText but get the same result. I was expecting the Landscape formatted documents to work the same as Portrait formatted ones.

答案1

得分: 1

我怀疑问题出在你插入的版面文件上。在Word中打开该文件,然后检查“页面设置”对话框的“版面开始”设置,该选项位于“布局”选项卡上。

将横向文件插入VBA合并文档会更改分节符。

正常的文档将具有“新建页”作为数值。如果你的文档具有“连续”作为数值,那将是问题的原因,这很容易修复:

Dim secIdx As Long
With DocTgt
    Set Rng = .Range.Characters.Last
    With Rng
        .Collapse wdCollapseEnd
        .InsertBreak Type:=wdSectionBreakNextPage
        .Collapse wdCollapseEnd
        '存储节号以便可以重置开始类型
        secIdx = .Information(wdActiveEndSectionNumber)
        Call LayoutTransfer(DocSrc, DocTgt)
        ' .FormattedText = DocSrc.Range.FormattedText
        .InsertFile FileName:=strFolder & "\" & strFile
    End With
    '如有必要,重置节的开始
    With .Sections(secIdx).PageSetup
        If .SectionStart = wdSectionContinuous Then .SectionStart = wdSectionNewPage
    End With
End With
英文:

I suspect that the issue is with the landscape file you are inserting. Open the file in Word and check the "Section start" setting on the Layout tab of the Page Setup dialog.

将横向文件插入VBA合并文档会更改分节符。

A normal document will have a value of "New page". If yours has a value of "Continuous" that will be the cause of the problem, which is very simple to fix:

Dim secIdx As Long
With DocTgt
    Set Rng = .Range.Characters.Last
    With Rng
        .Collapse wdCollapseEnd
        .InsertBreak Type:=wdSectionBreakNextPage
        .Collapse wdCollapseEnd
        'store section number so start type can be reset
        secIdx = .Information(wdActiveEndSectionNumber)
        Call LayoutTransfer(DocSrc, DocTgt)
        '.FormattedText = DocSrc.Range.FormattedText
        .InsertFile FileName:=strFolder & "\" & strFile
    End With
    'reset section start if necessary
    With .Sections(secIdx).PageSetup
        If .SectionStart = wdSectionContinuous Then .SectionStart = wdSectionNewPage
    End With
End With

答案2

得分: 0

根据我所知,你只需要在rng的末尾添加一个额外的段落,以防源文件末尾没有额外的空段落来分隔它们,以便插入的分节符号能够正确显示,否则它们将相互干扰。以下是我测试的代码,如你所说,下一页并没有变成连续的页面,插入的结果是下一页样式的分节符。

Sub Inserting_Landscape_file_into_VBA_Combined_Document_changes_Section_Break()
    With DocTgt
      Set rng = .Range.Characters.Last
      With rng
        .Collapse wdCollapseEnd
         '需要双重分节符以防止添加横向文件改变分节符
         '从NextPage变成Continuous

        '在你的代码中添加这两行,并且仅保留一个".InsertBreak"语句足够
        .InsertParagraphAfter
        .Collapse wdCollapseEnd

        .InsertBreak Type:=wdSectionBreakNextPage

        '仅保留一个".InsertBreak"语句足够
        '.InsertBreak Type:=wdSectionBreakNextPage

        .Collapse wdCollapseEnd
        '调用LayoutTransfer(DocSrc, DocTgt)
        '.FormattedText = DocSrc.Range.FormattedText
        .InsertFile FileName:=strFolder & "\" & strFile
      End With
    End With
End Sub
英文:

As far as I know, all you need is to add an extra paragraph at the end of the rng, in case there are no extra empty paragraphs at the end of the source file to separate them so that the inserted subsection break mark can be displayed correctly, otherwise they will interfere with each other. The following is the code I tested, and the next page does not become a continuous page as you said again, the inserted result is the next page style section break.

Sub Inserting_Landscape_file_into_VBA_Combined_Document_changes_Section_Break()
    With DocTgt
      Set rng = .Range.Characters.Last
      With rng
        .Collapse wdCollapseEnd
         'Double section break required to prevent adding landscape files changing break
'         from NextPage to Continuous

        rem add these two lines into your code and remain the only one ".InsertBreak" statement is enough
        .InsertParagraphAfter
        .Collapse wdCollapseEnd


        .InsertBreak Type:=wdSectionBreakNextPage


        rem remain the only one ".InsertBreak" statement is enough
'        .InsertBreak Type:=wdSectionBreakNextPage


       .Collapse wdCollapseEnd
'        Call LayoutTransfer(DocSrc, DocTgt)
        '.FormattedText = DocSrc.Range.FormattedText
        .InsertFile FileName:=strFolder & "\" & strFile
      End With
    End With
End Sub

huangapple
  • 本文由 发表于 2023年5月25日 07:28:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327988.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定