繁体   English   中英

将Excel附件另存为.txt-使用Outlook 2010中的vba宏打开

[英]Save Excel attachment as .txt - opened with a vba macro from Outlook 2010

我在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的VBA中保存该Excel?


对不起,如果不是我想要的那么清晰。

不起作用的部分是:

xlApp.SaveAs strFile,FileFormat:= xlText

我正在Outlook 2010中运行此程序,当我尝试将Excel保存为纯文本格式(用机智制表符分隔)时,我不明白为什么不起作用,我在做错什么吗?

感谢大家的回应。

这会将其另存为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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM