简体   繁体   English

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

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

I have VBA code in Outlook which downloads an Excel attachment of specific emails. 我在Outlook中有VBA代码,可下载特定电子邮件的Excel附件。 Once saved I open the Excel file and change a couple of things, and then I would like to save it as .txt instead of excel. 保存后,我打开Excel文件并进行了一些更改,然后将其另存为.txt而不是excel。

This is my code: 这是我的代码:

' 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

Does anyone know how to save that Excel in VBA on outlook? 有谁知道如何在Outlook的VBA中保存该Excel?


Sorry if a was not as clear as i wanted. 对不起,如果不是我想要的那么清晰。

The part that is not working is: 不起作用的部分是:

xlApp.SaveAs strFile, FileFormat:=xlText xlApp.SaveAs strFile,FileFormat:= xlText

I'm running this from Outlook 2010, and i don't understand why is not working when i'm trying to save the excel as a plain text (wit tabs delimited), am i doing something wrong? 我正在Outlook 2010中运行此程序,当我尝试将Excel保存为纯文本格式(用机智制表符分隔)时,我不明白为什么不起作用,我在做错什么吗?

Thanks all for your responses. 感谢大家的回应。

This will save it as a csv. 这会将其另存为csv。 I'm not sure you really want a plain text file as you would lose all column structure.. 我不确定您是否真的想要纯文本文件,因为您会丢失所有列的结构。

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

Here is a function I wrote at some point to export a whole workbook. 这是我在某个时候编写的用于导出整个工作簿的函数。 It puts each worksheet into it's own text file. 它将每个工作表放入其自己的文本文件中。

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