2013-11-04 40 views
1

我有這段代碼從文本文件中讀取,當行以「Q」開頭時,它的一個問題和「R」和「W」分別是錯誤的和正確的答案,它們被讀入形狀。 然而,問題是,如果文本中的任何位置都有逗號,我的Powerpoint宏會將其視爲一條新線。請問如何解決這個問題? 下面是代碼爲什麼我的vba代碼看到逗號爲新行?

Open ActivePresentation.Path & "\" & "questions.txt" For Input As #1 
nextSlideNum = 1 
nextAnswerNum = 1 
Do Until EOF(1) 
    Input #1, nextLine 
    If Left$(nextLine, 1) = "Q" Then 'The line starts with Q; it's a question 
     nextSlideNum = nextSlideNum + 1 
     Set oSld = _ 
      ActivePresentation.Slides.AddSlide(nextSlideNum, _ 
      ActivePresentation.SlideMaster.CustomLayouts(2)) 
     oSld.Shapes(1).TextFrame.TextRange.Text = _ 
      Trim(Right$(nextLine, Len(nextLine) - 1)) 
     nextAnswerNum = 1 
    ElseIf Left$(nextLine, 1) = "R" Then 'Right answer 
     Set oShp = ActivePresentation.Slides(nextSlideNum).Shapes _ 
      .AddShape(msoShapeActionButtonCustom, 100, _ 
      120 + (85 * (nextAnswerNum - 1)), 500, 75) 
     oShp.TextFrame.TextRange.Text = Trim(Right$(nextLine, Len(nextLine) - 1)) 
     oShp.ActionSettings(ppMouseClick).Action = ppActionRunMacro 
     oShp.ActionSettings(ppMouseClick).Run = "RightAnswerButton" 
     nextAnswerNum = nextAnswerNum + 1 
    ElseIf Left$(nextLine, 1) = "W" Then 'Wrong answer 
     Set oShp = ActivePresentation.Slides(nextSlideNum).Shapes _ 
      .AddShape(msoShapeActionButtonCustom, 100, _ 
      120 + (85 * (nextAnswerNum - 1)), 500, 75) 
     oShp.TextFrame.TextRange.Text = Trim(Right$(nextLine, Len(nextLine) - 1)) 
     oShp.ActionSettings(ppMouseClick).Action = ppActionRunMacro 
     oShp.ActionSettings(ppMouseClick).Run = "WrongAnswerButton" 
     nextAnswerNum = nextAnswerNum + 1 
    ElseIf Trim(nextLine) = "" Then 
     'Ignore blank lines 
    Else 
     MsgBox _ 
      "Sorry, I don't know what to do with: " _ 
      & Chr$(13) & nextLine 
    End If 
Loop 

回答

2

使用Line Input而不是Input

Input會進行一些解析,包括用逗號分隔以及轉換一些值,如#TRUE#和#NULL#。 Line Input輸入完整的行,不做轉換。

+0

哇!謝謝@Joe ......我不知道這一點......你真的爲我節省了很多工作......它工作得很好。 – Kwame

相關問題