【问题标题】:Save Excel attachment as .txt - opened with a vba macro from Outlook 2010将 Excel 附件另存为 .txt - 使用 Outlook 2010 中的 vba 宏打开
【发布时间】:2016-08-17 15:17:46
【问题描述】:

我在 Outlook 中有 VBA 代码,可以下载特定电子邮件的 Excel 附件。保存后,我打开 Excel 文件并更改了一些内容,然后我想将其另存为 .txt 而不是 excel。

这是我的代码:

' Save the attachment as a file.
objAttachments.Item(i).SaveAsFile strFile
'Open the attachment file
Set xlApp = CreateObject("Excel.Application")
xlApp.Workbooks.Open (strFile)
xlApp.Visible = True
xlApp.Workbooks.Item(1).activesheet.cells(1, 1).Value = "whatever"

**xlApp.SaveAs strFile, FileFormat:=xlText**
xlApp.Workbooks.Close

有谁知道如何在 Outlook 上将 Excel 保存在 VBA 中?


对不起,如果 a 没有我想要的那么清楚。

不工作的部分是:

xlApp.SaveAs strFile, FileFormat:=xlText

我从 Outlook 2010 运行此程序,但我不明白为什么当我尝试将 excel 保存为纯文本(以制表符分隔)时无法正常工作,我做错了吗?

感谢大家的回复。

【问题讨论】:

  • 这个有什么问题?
  • @JorgeRoldanSilla - 这段代码有什么问题,“如何在 Outlook 上的 vba 中保存该 excel”是什么意思?我想英语不是你的第一语言——也许找一个能翻译你的意思的朋友?

标签: excel vba outlook


【解决方案1】:

这会将其保存为 csv。我不确定你真的想要一个纯文本文件,因为你会丢失所有的列结构..

ActiveWorkbook.SaveAs filename:="C:\Temp\" & ActiveSheet.name & ".csv", FileFormat:=xlCSV

这是我在某个时候编写的用于导出整个工作簿的函数。它将每个工作表放入它自己的文本文件中。

Sub SaveWorksheetsAsCsv()
Dim ws As Excel.Worksheet
Dim SaveToDirectory As String
Dim CurrentWorkbook As String
Dim CurrentFormat As Long
Dim cell As Range

    CurrentWorkbook = ThisWorkbook.FullName
    CurrentFormat = ThisWorkbook.FileFormat
    ' Store current details for the workbook
    SaveToDirectory = "C:\Temp\"

    For Each ws In ThisWorkbook.Worksheets

        'This was a check on the worksheet name for excluding some worksheets that I didn't want to export
        'If ws.name <> "Instructions" And ws.name <> "Parameters" And ws.name <> "BI Data & Worksheet" Then

            'This makes the current sheet its own workbook so it can be saved.
            Sheets(ws.name).Copy

            'Not sure what I was doing here
            'For Each cell In [b:b]
            'If cell.Value = "~" Then cell.ClearContents
            ''put any value you want here
            'Next cell


            ActiveWorkbook.SaveAs filename:=SaveToDirectory & ws.name & ".csv", FileFormat:=xlCSV
            ActiveWorkbook.Close SaveChanges:=False
            ThisWorkbook.Activate
        'End If
    Next

    Application.DisplayAlerts = False
    ThisWorkbook.SaveAs filename:=CurrentWorkbook, FileFormat:=CurrentFormat
    Application.DisplayAlerts = True
    ' Temporarily turn alerts off to prevent the user being prompted
    '  about overwriting the original file.
End Sub

【讨论】: