为什么我的VBA代码将逗号视为换行符? [英] why does my vba code see comma as new line?

查看:79
本文介绍了为什么我的VBA代码将逗号视为换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码是从文本文件中读取的,当行以"Q"开头时,它是一个问题,而"R"和"W"分别是错误的和正确的答案,它们被读入形状.但是问题是,如果文本中的任何地方都有逗号,我的powerpoint宏会将其视为新行.对如何解决此问题有帮助吗?这是代码

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

推荐答案

使用 Line Input 代替 Input .

Input 进行一些解析,包括用逗号定界,并转换一些值,例如#TRUE#和#NULL#. Line 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.

这篇关于为什么我的VBA代码将逗号视为换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆