2009-11-12 57 views
3

此代码将读取文本文件中的一行:如何使用Power Point VBA代码逐个读取文本文件中的行?

set file = CreateObject("Scripting.FileSystemObject").OpenTextFile("c:\number.txt", 1) 
text = file.ReadLine 
MsgBox text 

我怎样才能使它先后从同一个文件中反复读取一行?我想,我应该在这里使用循环,对吧?我需要它在第一次迭代时从文件中读取第一行,在第二次迭代中读取第二行,在第三次中读取第三行,直到所有行被读取。我该怎么做?

重要的补充:我需要代码在每一行上逐一操作 - 不是一次全部操作!

回答

7

使用ReadAll()方法:

text = file.ReadAll 

(可能会感兴趣:FileSystemObject Sample Code

有了一个循环:

Const ForReading = 1, ForWriting = 2, ForAppending = 8 
Dim fso, MyFile, FileName, TextLine 

Set fso = CreateObject("Scripting.FileSystemObject") 

FileName = "c:\testfile.txt" 

Set MyFile = fso.OpenTextFile(FileName, ForReading) 

'' Read from the file 
Do While MyFile.AtEndOfStream <> True 
    TextLine = MyFile.ReadLine 

    '' Do stuff to TextLine 

Loop 
MyFile.Close 
+0

完美的答案 – AymericB 2017-11-24 10:13:45

0

您可以参考*添加到Windows脚本宿主对象模型,它将帮助你使用FileSystemObject对象,因为你可以这样说:

Dim fs As FileSystemObject 
Dim f As TextStream 

这将允许您使用智能感知来查看各种属性和对象浏览器来浏览库。

*在代码窗口中,选择工具,参考并勾选您想要的库旁边的框。

+0

我对不起,这对我来说太复杂了 - 我只是一个小菜鸟。不过,我确定,在VBA中应该有某种方式,像重新启动功能。 – brilliant 2009-11-12 01:51:35

+0

米奇小麦给了你答案,这是额外的评论,正如你在之前的文章中提到的那样,你希望学习VBA。对象浏览器和intellisense一样,对学习有很大的帮助。 – Fionnuala 2009-11-12 01:59:31

2

如果由于某种原因,你要这样使用内置的VBA文件处理程序,你可以使用代码:

Sub ReadAFileLineByLine() 
    Dim InStream As Integer 
    InStream = FreeFile() 
    Open "C:/tmp/fastsynchtoquesttry_quest.txt" For Input As InStream 

    Dim CurrLine As String 
    Do While True 
     Line Input #InStream, CurrLine 
     ' do stuff to CurrLine 
     If EOF(InStream) Then Exit Do 
    Loop 

    Close #InStream 
End Sub 
+0

谢谢你,乔恩。我感到非常惊讶 - 看来你已经到过我的另一个问题,然后来到这里分享这个代码。 (我通过我自己创建的第一行对其进行了猜测,并且此页面上的米奇小麦代码中未包含此代码。) 感谢您在此处共享的代码。它仍然需要一些时间来研究它,但我一定会这样做。 – brilliant 2009-11-12 17:03:30

+0

只是巧合,我认为......我只是尽我所能,但我很高兴别人认为我喜欢!很酷,看到你正在做powerpoint vba,当我这样做的时候总是很有趣...... – 2009-11-12 20:51:19

-1

我写了一个VBA程序读取的文本文件,并插入一个新的幻灯片对于文本中的每个句子。

首先,在幻灯片#1,添加一个按钮调用名为宏 “生成”

的源代码有云:

Const DEFAULT_SLIDE = 1 ' the slide to copy the layout style from 
Const MARGIN = 50  ' margin of the generated textbox 

Sub generate() 
    Dim txtFile As String ' text file name 
    Dim fileNo As Integer ' file handle 
    Dim buffer As String ' temporary string buffer 
    Dim sentence() As String ' the main array to save sentences 
    Dim i, total As Integer 
    Dim myLayout As CustomLayout 
    Dim mySlide As Slide 
    Dim myShape As Shape 
    Dim myWidth, myHeight As Integer 'slide width and height 


    txtFile = "text2sample.txt" 
    txtFile = ActivePresentation.Path & "\" & txtFile 'textfile should be in the same Dir as this ppt 

    If Len(Dir$(txtFile)) = 0 Then 
     MsgBox txtFile & " file not found." 
     Exit Sub 
    End If 

    'Initialize array 
    ReDim sentence(0) 

    'get file handle number 
    fileNo = FreeFile() 
    Open txtFile For Input As #fileNo 

    i = 0 
    Do While Not EOF(fileNo) 
     Line Input #fileNo, buffer 'read & save sentences line by line 
     ReDim Preserve sentence(i + 1) ' increase 1 more array 
     sentence(i) = LTrim(RTrim(buffer)) 
     i = i + 1 
    Loop 
    Close #fileNo 

    total = i 
    Randomize ' for random color 

    With ActivePresentation.PageSetup 
     myWidth = .SlideWidth - MARGIN 'get width and height 
     myHeight = .SlideHeight - MARGIN 
    End With 

    For i = 0 To total 
     Set myLayout = ActivePresentation.Slides(DEFAULT_SLIDE).CustomLayout 
     'add a slide like slide #1 
     Set mySlide = ActivePresentation.Slides.AddSlide(DEFAULT_SLIDE + 1 + i, myLayout) 
     'add a textbox with margin 
     Set myShape = ActivePresentation.Slides(DEFAULT_SLIDE + 1 + i).Shapes. _ 
     AddTextbox(msoTextOrientationHorizontal, MARGIN, MARGIN, myWidth, myHeight) 
     With myShape 
      'add a sentence 
      .TextFrame.TextRange.Text = sentence(i) 
      .TextFrame.TextRange.Font.Size = 60 
      ' color 255 is too bright. Pick a less bright color (200) 
      .TextFrame.TextRange.Font.Color.RGB = RGB(Int(Rnd * 200), Int(Rnd * 200), Int(Rnd * 200)) 
      .TextFrame.TextRange.Font.Bold = msoTrue 
      .TextFrame.TextRange.Font.Shadow = msoTrue 
      ' If you want to change the color of the shape 
      '.Fill.ForeColor.RGB = RGB(Int(Rnd * 200), Int(Rnd * 200), Int(Rnd * 200)) 
      '.Fill.BackColor.RGB = RGB(Int(Rnd * 200), Int(Rnd * 200), Int(Rnd * 200)) 
      '.Fill.Solid 
     End With 
     'add a textbox for slideshow progress ex) 1/100 
     Set myShape = ActivePresentation.Slides(DEFAULT_SLIDE + 1 + i).Shapes. _ 
     AddTextbox(msoTextOrientationHorizontal, 0, 0, 150, 20) 
     With myShape 
      .TextFrame.TextRange.Text = "(" & i & " /" & total & ")" 
      .TextFrame.TextRange.Font.Size = 20 
      .TextFrame.TextRange.Font.Color.RGB = RGB(100, 100, 100) 
     End With 
    Next 

    MsgBox total & " Slides were added.", vbInformation 

End Sub 

下载文件: http://konahn.tistory.com/attachment/[email protected]