gpt4 book ai didi

excel - VBA 设置页面设​​置

转载 作者:行者123 更新时间:2023-12-04 20:50:48 26 4
gpt4 key购买 nike

因此,通过反复试验和一些 Googlefu,我已经能够创建 VBA 代码,该代码将采用事件工作表和可见单元格并为其创建一个新工作簿,正确命名并作为电子邮件发送。但是,从电子邮件打开时新创建的电子表格设置为无缩放,因此当它打印时,现在应该是一页是 4。虽然通过在打印前手动更改新打开的工作簿中的页面设置很容易修复它,但我我试图弄清楚如何在创建新工作簿时自动设置页面属性(缩放百分比、适合所有列或我选择的任何内容)。
非常感谢 VBA 大师的任何帮助。
下面是我的代码:

Sub Mail_ActiveSheet()

Dim Source As Range
Dim Dest As Workbook
Dim wb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim OutApp As Object
Dim OutMail As Object


Set Source = Nothing
On Error Resume Next
Set Source = Range("A1:H75").SpecialCells(xlCellTypeVisible)
On Error GoTo 0

If Source Is Nothing Then
MsgBox "The source is not a range or the sheet is protected, please correct and try again.", vbOKOnly
Exit Sub
End If

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

Set Sheetname = ActiveSheet
Set wb = ActiveWorkbook
Set Dest = Workbooks.Add(xlWBATWorksheet)

Source.Copy
With Dest.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial Paste:=xlPasteValues
.Cells(1).PasteSpecial Paste:=xlPasteFormats
.Cells(1).Select
Application.CutCopyMode = False
End With

TempFilePath = Environ$("temp") & "\"
TempFileName = "Lost Items for " & Sheetname.Name & " " & Format(Now, "mm-dd-yy")

If Val(Application.Version) < 12 Then

FileExtStr = ".xls": FileFormatNum = -4143
Else

FileExtStr = ".xlsx": FileFormatNum = 51
End If

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

With Dest
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
On Error Resume Next
With OutMail
.Display
.to = "someone@somwhere.com"
.CC = ""
.BCC = ""
.Subject = "Lost Items for" & " " & Sheetname.Name
.HtmlBody = "Please see the attached spreadsheet. Have a nice day." & .HtmlBody
.Attachments.Add Dest.FullName

End With
On Error GoTo 0
.Close savechanges:=False
End With

Kill TempFilePath & TempFileName & FileExtStr

Set OutMail = Nothing
Set OutApp = Nothing

With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub

最佳答案

With Dest.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial Paste:=xlPasteValues
.Cells(1).PasteSpecial Paste:=xlPasteFormats
.Cells(1).Select
'insert your parameters here
.PageSetup.PrintArea = "$A$1:$Z$12"
.PageSetup.Zoom = False
.PageSetup.FitToPagesTall = 1
.PageSetup.FitToPagesWide = 1
'end of section
Application.CutCopyMode = False
End With

关于excel - VBA 设置页面设​​置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63005624/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com