如何冻结输出工作表的窗格

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

我目前有一些用Access编写的VBA代码来输出Excel文件。目前,没有为电子表格指定格式,只有使用我创建的查询提取的原始数据。我的问题是,如何冻结输出电子表格的顶行?请参阅下面的代码。

    Option Compare Database
    Public TimeStamp As String
    Public TimeStamp2 As String

    Function DailyMTDMail()

    If Weekday(Date) = 7 Or Weekday(Date) = 1 Then
    'do nothing
    Else

    TimeStamp = Month(Date) & "." & Day(Date) & "." & Year(Date)


    DoCmd.OutputTo acOutputQuery, "001 Extract Sales in Period", 
    acFormatXLSX, "\\xxx\xxx\xxx\MTD Sales @ " & TimeStamp & ".xlsx", False

    Dim filename As String

    filename = "\\xxx\xxx\xxx\MTD Sales @ " & TimeStamp & ".xlsx"

    Dim xl As Excel.Application
    Dim wb As Excel.Workbook
    Dim ws As Excel.Worksheet
    Set xl = CreateObject("Excel.Application")
    Set wb = xl.Workbooks.Open(filename)
    Set ws = wb.Sheets("001 Extract Sales in Period")         ' change to 
    the name of your sheet

    wb.Application.ActiveWindow.FreezePanes = False
    ws.Range("a2").Select                ' change to the range you want to 
    freeze

    wb.Application.ActiveWindow.FreezePanes = True
    wb.Save
    Set objMessage = CreateObject("CDO.Message")

    objMessage.Subject = " Inc - MTD Sales @ " & TimeStamp

    objMessage.From = "[email protected]"

    'objMessage.To = "[email protected]"
    objMessage.To = "[email protected]"    'test


    objMessage.Textbody = "Please find attached MTD sales @ " & TimeStamp & 
    vbCr & vbCr & "Regards" & vbCr & vbCr & "Name"

    objMessage.AddAttachment filename

    'This section provides the configuration information for the remote SMTP 
    server.
    'Normally you will only change the server name or IP.
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

    'Name or IP of Remote SMTP Server
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = 
    "word"

    'Server port (typically 25)
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

    objMessage.Configuration.Fields.Update

    'End remote SMTP server configuration section==

    objMessage.Send



    End If
ms-access access-vba
1个回答
0
投票

在docmd.output之后你必须打开excelfile,冻结你想要的单元格,保存并关闭文件,例如:

Function FreezeMe(strfile As String)

Dim xl As Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Set xl = CreateObject("Excel.Application")
Set wb = xl.Workbooks.Open(strfile)
Set ws = wb.Sheets("sheet1")         ' change to the name of your sheet

wb.Application.ActiveWindow.FreezePanes = False
ws.Range("a2").Select                ' change to the range you want to freeze 

wb.Application.ActiveWindow.FreezePanes = True
wb.Save
wb.Close


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