怎样用vba创建一个.txt文件并保存到D:\temp\下

gqianshan77 2006-08-09 03:12:26
请高手给出代码。
谢谢!
...全文
1193 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
awenjie 2006-08-10
  • 打赏
  • 举报
回复
文件名得生成主要有以下代码:

myDate = Format(Date, "yyyymmdd")
myTime = Format(Time, "hhmmss")
Randomize
myRnd = Format(100 * Rnd + 1, "00#")


timeS = myDate & myTime & myRnd
Fname = curUserName & timeS & ".csv"

subDirectory = "\仿真结果\"
myPath = ThisWorkbook.Path & subDirectory
If PathExists(myPath) = False Then MkDir (myPath)

myFileName = myPath & Fname
awenjie 2006-08-10
  • 打赏
  • 举报
回复
以下代码是我程序中一个导出仿真结果的sub,其中文件名是由当前用户名+当前日期+当前时间+3位随机数字组成。

Sub ExportRange()
Dim NumRows As Long, NumCols As Integer
Dim r As Long, c As Integer
Dim Data
Dim OutputRng As Range
Dim InputRng As Range

Dim Filt As String
Dim FilterIndex As Integer
Dim myFileName As Variant
'Dim DefaultPath As String
Dim InitFileName As String
Dim Title As String
Dim i As Integer
Dim Msg As String

Dim CreateTiem As String, curAuthor As String, ModifiedInfomation As String
Dim Fname As String, suDictionary As String, myPath As String

Set InputRng = Sheets("输入").Range(Sheets("输入").Cells(1, 1), Sheets("输入").Cells(ROWSCOUNT + 1, 39))
Set OutputRng = Sheets("输出").Range(Sheets("输出").Cells(1, 1), Sheets("输出").Cells(ROWSCOUNT + 1, 25))

myDate = Format(Date, "yyyymmdd")
myTime = Format(Time, "hhmmss")
Randomize
myRnd = Format(100 * Rnd + 1, "00#")

curUserName = Worksheets("用户数据").Range("IK501").Value
timeS = myDate & myTime & myRnd
Fname = curUserName & timeS & ".csv"

subDirectory = "\仿真结果\"
myPath = ThisWorkbook.Path & subDirectory
If PathExists(myPath) = False Then MkDir (myPath)

myFileName = myPath & Fname


Open myFileName For Output As #1

NumCols = InputRng.Columns.Count
NumRows = InputRng.Rows.Count

For r = 1 To NumRows
For c = 1 To NumCols
Data = InputRng.Cells(r, c).Value

If IsNumeric(Data) Then Data = Val(Data)
If IsEmpty(InputRng.Cells(r, c)) Then Data = ""

If c <> NumCols Then
Write #1, Data;
Else
Write #1, Data
End If
Next c
Next r

NumCols = OutputRng.Columns.Count
NumRows = OutputRng.Rows.Count

For r = 1 To NumRows
For c = 1 To NumCols
Data = OutputRng.Cells(r, c).Value

If IsNumeric(Data) Then Data = Val(Data)
If IsEmpty(OutputRng.Cells(r, c)) Then Data = ""

If c <> NumCols Then
Write #1, Data;
Else
Write #1, Data
End If
Next c
Next r



'写入本次仿真的信息 包括生成时间,作者,修改变量
CreateTime = Now
curAuthor = curUserName
ModifiedInfomation = Worksheets("用户数据").Range("II501")
Write #1, "生成时间";
Write #1, CreateTime;
Write #1, "用户";
Write #1, curAuthor;
Write #1, "修改的变量";
Write #1, ModifiedInfomation;
Close #1

MsgBox "导出仿真结果 " & myFileName & " 成功!", vbInformation, PROGRAMTITLE

End Sub
gqianshan77 2006-08-09
  • 打赏
  • 举报
回复
在outlook中,要根据每次收到的不同mail的时间yyyymmdd作为文件名,即yyyymmdd.txt 应该怎么写?
一笑拔剑 2006-08-09
  • 打赏
  • 举报
回复
Open "D:\temp\文件名.txt" For Output As #1
print #1,内容
Close #1
一笑拔剑 2006-08-09
  • 打赏
  • 举报
回复
vba的
读取文件
Sub test()
Open "D:\temp\文件名.txt" For Output As #1
Close #1

End Sub
读取
Sub test()
Open "D:\temp\文件名.txt" For Input As #1
While Not EOF(1)
Line Input #1, s
If Trim(s) <> "" Then
Selection.TypeText Text:=s + vbCrLf
End If
Wend
Close #1
End Sub
of123 2006-08-09
  • 打赏
  • 举报
回复
Open "D:\temp\test.txt" For Output As #1
Close #1

2,462

社区成员

发帖
与我相关
我的任务
社区描述
VBA(Visual Basic for Applications)是Visual Basic的一种宏语言,是在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言。
社区管理员
  • VBA
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧