Power Point VBA 코드를 사용하여 텍스트 파일의 줄을 하나씩 읽는 방법은 무엇입니까?
-
19-09-2019 - |
문제
이 코드는 텍스트 파일에서 줄을 읽습니다.
set file = CreateObject("Scripting.FileSystemObject").OpenTextFile("c:\number.txt", 1)
text = file.ReadLine
MsgBox text
같은 파일에서 한 줄을 반복적으로 읽게하려면 어떻게해야합니까? 여기서 루프를 사용해야합니다. 첫 번째 반복에서 파일에서 첫 번째 줄을 읽고, 두 번째 줄은 두 번째 반복에서, 세 번째 라인은 세 번째 줄을 읽어야합니다. 어떻게하니?
중요한 추가 : 각 줄에서 하나씩 작동하려면 코드가 필요합니다. 한 번에 모두가 아닙니다!
해결책
사용 ReadAll()
방법:
text = file.ReadAll
(관심이있을 수 있습니다 : FileSystemObject 샘플 코드)
루프로 :
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
다른 팁
어떤 이유로 든 빌드 된 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
Windows 스크립트 호스트 객체 모델에 참조*를 추가 할 수 있습니다. 파일 시스템 객체를 사용하는 데 도움이됩니다.
Dim fs As FileSystemObject
Dim f As TextStream
이를 통해 INTELLISENSE를 사용하여 다양한 속성과 객체 브라우저가 라이브러리를 탐색 할 수 있습니다.
* 코드 창에서 도구, 참조를 선택하고 원하는 라이브러리 옆에 상자를 체크하십시오.
텍스트 파일을 읽고 텍스트에 각 문장에 새 슬라이드를 삽입하는 VBA 루틴을 작성했습니다.
먼저 슬라이드 #1에서 "Generate"라는 매크로를 호출하는 버튼을 추가합니다.
소스 코드는 다음과 같습니다.
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/cfile8.uf@2175154c573d3bc02a2dfa.pptm
제휴하지 않습니다 StackOverflow