从模板

时间:2017-08-14 16:43:09

标签: excel-vba access-vba vba excel

我正在尝试使用Access从启用宏的模板中保存启用宏的文件。

我收到以下错误:
1-使用Thisworkbook部分中的Application.visible = False隐藏Excel模板

This is the error message that displays

2-然后,如果我单击是以制作无宏工作簿,我会收到以下运行时错误 Yes-Runtime error

3-如果我点击否,我会收到此运行时错误 NO-Runtime error

这是我正在使用的代码:

Workorder = Me.TxtWorkOrder & "_" & Me.TxtActOrder

Set xlapp = New Excel.Application

xlapp.Visible = False
Excel.Application.EnableEvents = False

Set Wb = xlapp.Workbooks.Open("H:\Template and testers\Template\TabletTemplate.xltm")
Set ws = xlapp.Worksheets("Profile")

ws.Activate
    If Me.TxtWorkOCheck = 1 Then
        'workorder
        With ws
            .Range("B1") = Me.TxtWorkOrder
            .Range("B2") = Me.TxtUserID
            .Range("B3") = Me.TxtJobsiteID
            .Range("B4") = Me.CboPlant.Value
            .Range("B5") = Me.CboArea.Value
            .Range("B6") = Me.TxtScaffoldID
            .Range("B7") = Me.TxtActOrder
            .Range("B10") = output
            .Range("B11") = Me.TxtFuncLocation

        End With
    Else
        'sub-workorder
        With ws
            .Range("B8") = Me.TxtWorkOrder
            .Range("B2") = Me.TxtUserID
            .Range("B3") = Me.TxtJobsiteID
            .Range("B4") = Me.CboPlant.Value
            .Range("B5") = Me.CboArea.Value
            .Range("B6") = Me.TxtScaffoldID
            .Range("B9") = Me.TxtActOrder
            .Range("B1") = Me.TxtAssoWorkOrder
            .Range("B10") = output
            .Range("B11") = Me.TxtFuncLocation
            .Range("F1") = 0
        End With
    End If

xlapp.Worksheets("ScaffoldList").Activate
Wb.SaveAs ("H:\ActiveWorkOrders\ToGather\" & Workorder & ".xlsm")
Excel.Application.EnableEvents = True

Wb.Close
Set xlapp = Nothing

1 个答案:

答案 0 :(得分:1)

您需要实际指定要将其保存为的类型,指定扩展名是不够的。

用以下内容替换给出错误的行:

contentWidth = grid.Columns.GetColumnsWidth(DataGridViewElementStates.Displayed);

有关Wb.SaveAs "H:\ActiveWorkOrders\ToGather\" & Workorder & ".xlsm", xlOpenXMLWorkbookMacroEnabled方法

的文档,请参阅this MSDN page