+ -
当前位置:首页 → 问答吧 → vba读取txt文档指定列至Word中,并按照12*8的矩阵排列。求高人帮助。

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)

2011-1-7 11:12, 下载次数: 1

VBA求助

作者: xiaofeiwa   发布时间: 2011-01-07

可以试试如下代码,导入的文本依次置于相应文本框中:
复制内容到剪贴板
代码:
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