vba读取txt文档指定列至Word中,并按照12*8的矩阵排列。求高人帮助。
时间:2011-01-07
来源:互联网
各位大侠:
小弟需要从txt文件中读取第一列中的字符串至word中,word有个模板,需要将读取的数据顺次放入文档中的96个圆圈中。txt文档是其他专业软件生成的,输出格式不能调整,其中的列是用引号分割的。
1 是不是需要word文档中需要写入的位置作一些标记?找了很多域的教材,但是不知道从何入手
2 txt文档第一列读入后,有很多信息依然重复,如何选择特定的内容呢
请各位高手帮帮小弟
附件已上传
[ 本帖最后由 xiaofeiwa 于 2011-1-7 11:15 编辑 ]
vba求助.rar(23.71 KB)
小弟需要从txt文件中读取第一列中的字符串至word中,word有个模板,需要将读取的数据顺次放入文档中的96个圆圈中。txt文档是其他专业软件生成的,输出格式不能调整,其中的列是用引号分割的。
1 是不是需要word文档中需要写入的位置作一些标记?找了很多域的教材,但是不知道从何入手
2 txt文档第一列读入后,有很多信息依然重复,如何选择特定的内容呢
请各位高手帮帮小弟
附件已上传
[ 本帖最后由 xiaofeiwa 于 2011-1-7 11:15 编辑 ]
附件

2011-1-7 11:12, 下载次数: 1
VBA求助
作者: xiaofeiwa 发布时间: 2011-01-07
可以试试如下代码,导入的文本依次置于相应文本框中:
复制内容到剪贴板
Dim fs, f, aCell As Cell, myShape As Shape
Dim l As Single, t As Single, w As Single
Dim info() As String, i As Integer, a As String
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "请指定数据源文本文件"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile(.SelectedItems(1), 1, -1)
End With
info = Split(f.readall, vbCrLf)
w = ActiveDocument.InlineShapes(1).Width
Application.ScreenUpdating = False
For Each aCell In ActiveDocument.Tables(1).Range.Cells
If i > UBound(info) Then Exit For
If aCell.Range.InlineShapes.Count = 1 Then
i = i + 1
l = aCell.Range.Characters.First.Information(wdHorizontalPositionRelativeToTextBoundary)
t = aCell.Range.Characters.First.Information(wdVerticalPositionRelativeToTextBoundary) + 2 ''微调文本框垂直位置
Set myShape = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, l, t, w, w, aCell.Range.Characters.First)
myShape.Fill.Visible = msoFalse
myShape.Line.Visible = msoFalse
a = Split(info(i), Chr(9))(0) ''提取数据
a = Mid(a, 2, Len(a) - 2)
With myShape.TextFrame.TextRange ''插入提取到的文本,并设置段落格式和字符大小
.Text = a
With .ParagraphFormat
.SpaceBefore = 5
.LineSpacingRule = wdLineSpaceExactly
.LineSpacing = 9
.Alignment = wdAlignParagraphCenter
End With
.Font.Size = 7
End With
End If
Next
MsgBox "处理完毕! 共导入了 " & i & "个数据。"
Application.ScreenUpdating = True
End Sub
代码:
Sub test()Dim fs, f, aCell As Cell, myShape As Shape
Dim l As Single, t As Single, w As Single
Dim info() As String, i As Integer, a As String
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "请指定数据源文本文件"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile(.SelectedItems(1), 1, -1)
End With
info = Split(f.readall, vbCrLf)
w = ActiveDocument.InlineShapes(1).Width
Application.ScreenUpdating = False
For Each aCell In ActiveDocument.Tables(1).Range.Cells
If i > UBound(info) Then Exit For
If aCell.Range.InlineShapes.Count = 1 Then
i = i + 1
l = aCell.Range.Characters.First.Information(wdHorizontalPositionRelativeToTextBoundary)
t = aCell.Range.Characters.First.Information(wdVerticalPositionRelativeToTextBoundary) + 2 ''微调文本框垂直位置
Set myShape = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, l, t, w, w, aCell.Range.Characters.First)
myShape.Fill.Visible = msoFalse
myShape.Line.Visible = msoFalse
a = Split(info(i), Chr(9))(0) ''提取数据
a = Mid(a, 2, Len(a) - 2)
With myShape.TextFrame.TextRange ''插入提取到的文本,并设置段落格式和字符大小
.Text = a
With .ParagraphFormat
.SpaceBefore = 5
.LineSpacingRule = wdLineSpaceExactly
.LineSpacing = 9
.Alignment = wdAlignParagraphCenter
End With
.Font.Size = 7
End With
End If
Next
MsgBox "处理完毕! 共导入了 " & i & "个数据。"
Application.ScreenUpdating = True
End Sub
作者: sylun 发布时间: 2011-01-08
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28