将文本文件导入Excel会在第二轮代码中陷入困境

时间:2019-01-23 18:55:54

标签: excel vba powerpoint-vba

我试图让Powerpoint打开,以在文件夹中找到最新的文本文件,使用excel打开文本文件,格式化文本文件,然后将文件另存为xlsx。然后,该最终文档将在PowerPoint演示文稿中进行更新。

我的问题是代码将运行一次并按照预期的方式运行。然后在下一次迭代中,powerpoint崩溃。看来代码与excel息息相关,我无法在代码结论中弄清楚如何切断它。有什么想法吗?

for

2 个答案:

答案 0 :(得分:0)

您需要清除Excel变量,方法是在关闭后将其设置为空。另外,添加xlApp.DisplayAlerts = False行。

之后

    xlApp.DisplayAlerts = False

    xlApp.ActiveWorkbook.SaveAs Filename:="C:\finalfilelocation\FinalIN3Document.xlsx", 
       AccessMode:=xlExclusive, 
       ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges

    xlApp.ActiveWorkbook.Close

    xlApp.Workbooks.Close

    xlApp.Quit

输入以下内容:

Set xlApp = Nothing

那应该可以解决问题。

答案 1 :(得分:0)

我通过在大多数代码之前添加xlAPP来使其工作(请参见下文)。再次感谢您对所有人的帮助。我需要做些什么来标记为已解决吗?

With xlApp.ActiveSheet.QueryTables.Add(Connection:="TEXT;C:filepath\begin.txt", Destination:=xlApp.Cells(1, 1))

        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlFixedWidth
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileFixedColumnWidths = Array(4, 10, 10, 9, 18, 15, 23, 32, 12, 5, 7, 13, 9, 6)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
    xlApp.Rows("1:9").Select
    xlApp.Selection.Delete Shift:=xlUp
    xlApp.Columns("A:A").Select
    xlApp.Selection.Delete Shift:=xlToLeft
    xlApp.Rows("2:2").Select
    xlApp.Selection.Delete Shift:=xlUp
    xlApp.Range("M:M,N:N").Select
    xlApp.Range("N1").Activate
    xlApp.Selection.Delete Shift:=xlToLeft
    xlApp.Columns("K:K").Select
    xlApp.Selection.Delete Shift:=xlToLeft
    xlApp.Range("F20").Select


     xlApp.Columns("D").EntireColumn.Delete
     xlApp.Columns("H").EntireColumn.Delete
     xlApp.Columns("I").EntireColumn.Delete
     xlApp.Columns("G").EntireColumn.Delete
     xlApp.Columns("C").EntireColumn.Delete
     xlApp.Columns("A").EntireColumn.Delete


     xlApp.Columns("A").ColumnWidth = 25
     xlApp.Columns("B").ColumnWidth = 25
     xlApp.Columns("C").ColumnWidth = 30
     xlApp.Columns("D").ColumnWidth = 60
     xlApp.Columns("E").ColumnWidth = 15


     xlApp.Range("A1:E1").EntireRow.Insert
     xlApp.Range("A1:E1").Merge
     xlApp.Range("A:E").HorizontalAlignment = xlCenter
     xlApp.Range("A:E").Font.Size = 15
     xlApp.Range("A1").Font.Size = 30
     xlApp.Range("A1").Value = "IN3 Dispatch as of " & latestDate


    xlApp.DisplayAlerts = False

    xlApp.ActiveWorkbook.SaveAs FileName:="C:\filepath\end.xlsx", AccessMode:=xlExclusive, ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges

    xlApp.ActiveWorkbook.Close

    xlApp.Workbooks.Close


   xlApp.Quit
   Excel.Application.Quit