计划要运行的程序中具有excel的.exe文件

时间:2019-04-12 10:55:15

标签: excel vb.net windows-10

我已经使用VB.NET在Visual Studios中创建了一个应用程序,加载后,表单会加载一个Excel电子表格,并在保存和退出之前执行一系列任务。

我可以通过单击手动运行.exe文件,但是当我通过他内置的任务计划程序计划它时,它什么也没做。任务不会停止或错误,只是显示为正在运行,但从未运行

我已经在网上进行了各种搜索以寻求帮助,但是我似乎找不到任何地方。我还有另一种形式,可以打开一个文本文件,执行它的工作,然后关闭该文本文件,这工作得很好,这就是为什么它让我感到非常困惑。我尝试将我的代码添加到文本文件之一,并从文本文件任务管理器复制命令,但是它不起作用!我得出的结论是,这与excel有关,因为代码本身可以很好地工作,而调度可以通过链接到文本文件的表单来实现。

任何帮助或建议都将不胜感激

下面的代码

    Dim filepath1, filepath3, colcontents, rowcontents, txtfile As String
        Dim objExcel, objworkbook As Object
        Dim coln, rown, rowval As Integer
        Dim obj = CreateObject("Scripting.FileSystemObject")
        txtfile = FULLFILEPATH
        filepath1 = FULLFILEPATH
        filepath3 = FULLFILEPATH
        filepath3 = FULLFILEPATH
        'Define as getfile filepath
        Dim objfile = obj.getfile(filepath1)
        If ran = False Then
            objExcel = CreateObject("Excel.Application")
            objExcel.visible = False
            rown = 2
            coln = 1
            colcontents = "Text"
            objworkbook = objExcel.workbooks.open(filepath1)
            objExcel.sheets("KPIStock").activate
            Do Until colcontents = ""
                colcontents = objExcel.cells(1, coln).text
                coln = coln + 1
            Loop
            colvalglo = coln
            rown = 1
            rowcontents = "Text"
            'Counts the number of rows that have data in them
            Do Until rowcontents = ""
                rowcontents = objExcel.cells(rown, 1).text
                rown = rown + 1
                rowval = rown
            Loop
            rowvalglo = rowval
            MsgBox("Limits Found")
If obj.FileExists(filepath4) Then
            'If the file exists then delete it
            obj.DeleteFile(filepath4)
        Else
        End If
        'Pass values to set variables
        colcontents = "Text"
        param1 = Nothing
        param2 = Nothing
        'Define the object as getting a file from the selected filepath
        Dim objfile = obj.getfile(filepath1)
        objExcel = CreateObject("Excel.Application")
        objExcel.visible = False
        'Open selected file
        objworkbook = objExcel.workbooks.open(filepath1)
        colnc = 9
        count = 1
        'Loop until there are no column contents and then save this column number into a global variable
        Do Until colcontents = ""
            rown = 2
            'Activate the Parameteres sheet
            objExcel.sheets("Parameters").activate
            colcontents = objExcel.cells(rown, colnc).text
            'Set Param1 equal to what is contained in the parameters sheet and cells
            param1 = objExcel.cells(rown, colnc).text
            rown = 3
            'Set Param2 equal to what is contained in the parameters sheet and cells
            param2 = objExcel.cells(rown, colnc).text
            objExcel.sheets("KPIStock").activate
            colpaste = colvalglo - 1
            'Header
            rown = 1
            'Save the contents of the cell into the selected Variable
            objExcel.cells(rown, colpaste).value = param1
            'Formula
            rown = 2
            'Save the contents of the cell into the selected Variable
            objExcel.cells(rown, colpaste).value = param2
            'Increase various variables by 1
            colnc = colnc + 1
            colpaste = colpaste + 1
            colvalglo = colvalglo + 1
            count = count + 1
        Loop
        rowvalglo = rowvalglo - 2
        'Copy all of added data and paste to the bottom
        With objExcel
            .range(.cells(2, colvalglo - count), .cells(2, colvalglo)).copy
            .range(.cells(2, colvalglo - count), .cells(rowvalglo, colvalglo)).pastespecial
        End With
        'Set specific cell range to a certain colour
        With objExcel
            .range(.cells(1, colvalglo - count), .cells(1, colvalglo - 65)).Interior.Color = System.Drawing.ColorTranslator.ToOle(Color.LightCyan)
        End With
        'Set specific cell range to a certain colour
        With objExcel
            .range(.cells(2, colvalglo - count), .cells(rowvalglo, colvalglo - 65)).Interior.Color = System.Drawing.ColorTranslator.ToOle(Color.LightYellow)
        End With
        'Set specific cell range to a certain colour
        With objExcel
            .range(.cells(1, colvalglo - 65), .cells(1, colvalglo - 30)).Interior.Color = System.Drawing.ColorTranslator.ToOle(Color.LightGreen)
        End With
        'Set specific cell range to a certain colour
        With objExcel
            .range(.cells(2, colvalglo - 65), .cells(rowvalglo, colvalglo - 30)).Interior.Color = System.Drawing.ColorTranslator.ToOle(Color.LightSalmon)
        End With
        'Set specific cell range to a certain colour
        With objExcel
            .range(.cells(1, colvalglo - 30), .cells(1, colvalglo - 3)).Interior.Color = System.Drawing.ColorTranslator.ToOle(Color.LightSteelBlue)
        End With
        'Set specific cell range to a certain colour
        With objExcel
            .range(.cells(2, colvalglo - 30), .cells(rowvalglo, colvalglo - 3)).Interior.Color = System.Drawing.ColorTranslator.ToOle(Color.GhostWhite)
        End With
        'Copy all of added data and paste as values
        With objExcel
            .range(.cells(2, colvalglo - count), .cells(rowvalglo, colvalglo)).copy
            .range(.cells(2, colvalglo - count), .cells(rowvalglo, colvalglo)).Pastespecial(-4163, -4142, True, False)
        End With
        'Save Output workbook to Filepath4
        objExcel.ActiveWorkbook.SaveAs(filepath4)
        objworkbook.Close
        objExcel.Quit
        objExcel = Nothing
        objworkbook = Nothing
        'Set updated Boolean to TRUE
        updated = True
        rowval = rowval + 1
        'Display message box informating the task has now been complete and to review as required
        MsgBox("Defined columns have now been added, please review")

0 个答案:

没有答案