why does my vba code see comma as new line?

[亡魂溺海] 提交于 2019-12-11 01:33:16

问题


I have this code that reads from text file, when line starts with "Q" its a question and "R" and "W" are wrong and right answers respectively that are read unto shapes. However the problem is, if there is a comma anywhere in the text, my powerpoint macro sees it as a new line. any help on how to fix this please? Here is the code

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

回答1:


Use Line Input instead of Input.

Input does some parsing, including delimiting by comma, and transforming some values such as #TRUE# and #NULL#. Line Input inputs the complete line and does no transforming.



来源:https://stackoverflow.com/questions/19768000/why-does-my-vba-code-see-comma-as-new-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!