如何使用VBA将excel选项卡格式化为横向模式并设置行和列的宽度? [已关闭]

问题描述 投票:0回答:1

我在 Excel 中创建了选项卡,其中包含我需要包含到各种报告中的所有行和列。但是,当我运行宏将报告插入到 Word 中时,Word 文档不会进入横向模式,并且需要调整列和宽度以使报告看起来可读。

我有需要将excel选项卡导出到word的宏,但我不知道如何将word报告设置为特定格式。

excel vba ms-word format
1个回答
0
投票

子格式ExcelTab() 将 objWord 变暗为对象 Dim objDoc 作为对象 将 objSheet 调暗为对象

' Create a new instance of Word
Set objWord = CreateObject("Word.Application")
objWord.Visible = True

' Open the Word document
Set objDoc = objWord.Documents.Open("C:\Path\To\Your\Word\Document.docx")

' Insert the Excel tab as an embedded object
Set objSheet = objDoc.Shapes.AddOLEObject(Left:=100, Top:=100, Width:=300, Height:=200, ClassName:="Excel.Sheet")
objSheet.OLEFormat.Object.Activate

' Set the Excel tab to landscape mode
objSheet.OLEFormat.Object.Application.ActiveSheet.PageSetup.Orientation = xlLandscape

' Set the width of the rows and columns
objSheet.OLEFormat.Object.Application.ActiveSheet.Columns("A:Z").ColumnWidth = 15
objSheet.OLEFormat.Object.Application.ActiveSheet.Rows("1:100").RowHeight = 15

' Save and close the Word document
objDoc.Save
objDoc.Close

' Clean up
Set objSheet = Nothing
Set objDoc = Nothing
Set objWord = Nothing

结束子

© www.soinside.com 2019 - 2024. All rights reserved.