我一直在尝试附加的VBA代码,该代码在将Excel表导出到Word时试图设置页面方向。一张桌子很容易做,但是我试图导出几张桌子,有些桌子应该是纵向的,有些桌子应该是横向的。问题是在新的Word文档中从一种格式切换到另一种格式。我尝试了各种添加分节符的方法,但是我没有做任何接近我想要的事情。
下面的代码段循环运行,并在检查表单控件的选择内容时导出每个表。
代码取决于用户在表单上的选择,因此每次可能不同。为了更容易阅读,我只包括了实际上处理新Word文档(newDoc)格式的代码部分。显示的是该代码的最后一个版本,有很多版本。
我需要的是一种运行循环(以及附加的代码)的方法,以使每个表都根据其预定的需求(新Word文档中的纵向或横向)进行定位。
Select Case intDoc ' set the Word pages based on the selected document
Case 1, 4, 5, 6 ' Estimate(1), Invoice(4), Scope of Work-->Orientation = wdOrientPortrait
' paste the table into the newDoc and set some options
With newDoc
.ActiveWindow.View.ShowAll = False ' Hide all formatting marks
.ActiveWindow.View.ShowHiddenText = False ' Hide all hidden text
.Paragraphs(.Paragraphs.Count).Range.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=True
.Content.InsertParagraphAfter
.Range(.Content.End - 1).InsertBreak Type:=wdSectionBreakNextPage 'wdPageBreak
With .PageSetup
.Orientation = wdOrientPortrait
.RightMargin = Application.InchesToPoints(0.75)
.LeftMargin = Application.InchesToPoints(0.75)
End With
End With
' Autofit the table to the page
Set wordTbl = newDoc.Tables(newDoc.Tables.Count)
wordTbl.AutoFitBehavior (wdAutoFitWindow)
Case 2, 3 ' the Detail listing (2), or the Itemized listing(3)-->Orientation = wdOrientLandscape
' paste the table into the newDoc and set some options
With newDoc
.ActiveWindow.View.ShowAll = False ' Hide all formatting marks
.ActiveWindow.View.ShowHiddenText = False ' Hide all hidden text
.Paragraphs(.Paragraphs.Count).Range.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=True
.Content.InsertParagraphAfter
.Range(.Content.End - 1).InsertBreak Type:=wdSectionBreakNextPage 'wdPageBreak
With .PageSetup
.Orientation = wdOrientLandscape
.RightMargin = Application.InchesToPoints(0.5)
.LeftMargin = Application.InchesToPoints(0.5)
End With
End With
' Autofit the table to the page
Set wordTbl = newDoc.Tables(newDoc.Tables.Count)
wordTbl.AutoFitBehavior (wdAutoFitWindow)
End Select
答案 0 :(得分:0)
由于您没有提供[mcve],因此我无法直接使用您的代码。以下是我的测试代码-您应该能够将其适应您的实际情况。处理任务的方法不只一种,但是我尝试尽可能地接近您的原始方法。
请注意,我已经添加了对象变量rngNewTable
-一个Word.Range
对象。这有助于更好地准确跟踪文档中需要发生的地方(实际上与Excel中相同)。 Document.Content
提供了很好的参考范围,但是标识特定的点或区域不是“合适的”。
我还添加了一个变量来存储当前页面方向。如果方向已经正确,则无需重复某些操作,因此可用于测试当前方向。 (实际上,如果方向不变,您甚至不需要为每个表使用单独的部分-但我不知道所发生情况的完整逻辑,因此请不要更改。)
粘贴表现在在每个循环的末尾,可能在Select
之外,但是我将这个决定留给您。您还需要用自己的逻辑替换我的For...Next
。
这基本上是通过折叠/设置目标Range
的结尾(每次插入表或分节符之后),以便始终在之后< / em>新材料。
Sub InsertTableWithPageOrientation()
Dim newDoc As Word.Document
Dim intDoc(3) As Long, iCounter As Long
Dim rngNewTable As Word.Range
Dim iCurrPageOrientation As Long
Dim wordTbl
Set newDoc = ActiveDocument
With newDoc
.ActiveWindow.View.ShowAll = False ' Hide all formatting marks
.ActiveWindow.View.ShowHiddenText = False ' Hide all hidden text
Set rngNewTable = newDoc.content
rngNewTable.Collapse wdCollapseEnd
iCurrPageOrientation = .PageSetup.Orientation
End With
intDoc(0) = 1
intDoc(1) = 2
intDoc(2) = 3
intDoc(3) = 4
For iCounter = LBound(intDoc) To UBound(intDoc)
rngNewTable.InsertBreak Type:=wdSectionBreakNextPage 'wdPageBreak
rngNewTable.Start = newDoc.content.End
Select Case intDoc(iCounter) ' set the Word pages based on the selected document
Case 1, 4, 5, 6 ' Estimate(1), Invoice(4), Scope of Work-->Orientation = wdOrientPortrait
' paste the table into the newDoc and set some options
If iCurrPageOrientation <> wdOrientPortrait Then
With rngNewTable.Sections(1).PageSetup
.Orientation = wdOrientPortrait
.RightMargin = Application.InchesToPoints(0.75)
.LeftMargin = Application.InchesToPoints(0.75)
iCurrPageOrientation = wdOrientPortrait
End With
End If
rngNewTable.PasteExcelTable LinkedToExcel:=false, WordFormatting:=False, RTF:=True
rngNewTable.Start = newDoc.content.End
Case 2, 3 ' the Detail listing (2), or the Itemized listing(3)-->Orientation = wdOrientLandscape
' paste the table into the newDoc and set some options
If iCurrPageOrientation <> wdOrientLandscape Then
With rngNewTable.Sections(1).PageSetup
.Orientation = wdOrientLandscape
.RightMargin = Application.InchesToPoints(0.5)
.LeftMargin = Application.InchesToPoints(0.5)
iCurrPageOrientation = wdOrientLandscape
End With
End If
rngNewTable.PasteExcelTable LinkedToExcel:=false, WordFormatting:=False, RTF:=True
rngNewTable.Start = newDoc.content.End
End Select
Next
' Autofit the table to the page
Set wordTbl = newDoc.Tables(newDoc.Tables.Count)
wordTbl.AutoFitBehavior (wdAutoFitWindow)
End Sub