自动化文件编辑

时间:2015-11-20 17:27:20

标签: excel vba automation

我正在尝试访问文件夹中的Excel文件,更改第一张工作表(工作表1)以缩放80%。我已经设置了所有引用,但只是编译和语法错误:

   Function XCEL_FILE_EDIT()

       Dim file
       Dim path As String
       Dim Documents As Excel.Worksheets
       Dim ws As Excel.Worksheet, lngZoom As Long

       path = "C:\TEST\TEST\TEST\"

       file = Dir(path & "*.xlsx")

       Do While file <> ""

          Documents.Open FileName:=(path & "*.xlsx")

          For Each ws In ThisWorkbook.Worksheets

             Select Case Documents.Name

                Case "Sheet 1": lngZoom = 80

             End Select

             With ws
                .Select
                ActiveWindow.Zoom = lngZoom
             End With

          Documents.Save
          Documents.Close

          file = Dir()

      Loop

   End Function

1 个答案:

答案 0 :(得分:0)

采取其他人的说法并进行一些修改,试试这个:

Sub XCEL_FILE_EDIT()

    Dim file As String
    Dim path As String
    Dim WS As Worksheet

    path = "C:\TEST\TEST\TEST\"
    file = Dir(path & "*.xlsx")

    Do While file <> ""

        Workbooks.Open Filename:=file
        For Each WS in Workbooks(file).Sheets
            WS.Select
            Application.PrintCommunication = False
            WS.PageSetup.Zoom = 80
            Application.PrintCommunication = True
        Next WS
        Workbooks(file).Close True
        file = Dir()

    Loop

End Sub