vba Visual Basic 编译错误 - 无效字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16662485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:12:37  来源:igfitidea点击:

Visual Basic Compile Error - Invalid Character

vbaoutlookoutlook-vba

提问by DextrousDave

I got a VB SCRIPToff the internet to create new mail alerts for secondary email accountsin Outlook(2010).

我从 Internet 上获得了一个VB 脚本用于为Outlook(2010)中的辅助电子邮件帐户创建新邮件警报

Now this is the first part of the code, and when running Outlook, it gives me the following error:

现在这是代码的第一部分,在运行 Outlook 时,它给了我以下错误:

"Compile Error: Invalid Character"

“编译错误:无效字符”

The debugger underlines the _ character in the following line: "sndPlaySoundA" _

调试器在以下行中为 _ 字符加下划线:"sndPlaySoundA" _

'On the next line change the file name and path of the sound you want to play.'
Public Const SOUND_TO_PLAY = "C:\Windows\Media\Speech On.wav"
Public Const SND_ASYNC = &H1

Public Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _ 
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long Public Declare Function    MessageBox _
    Lib "User32" Alias "MessageBoxA" _
        (ByVal hWnd As Long, _
        ByVal lpText As String, _
        ByVal lpCaption As String, _
        ByVal wType As Long) _
    As Long


Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder
    ' Purpose: Opens an Outlook folder from a folder path.'
    ' Written: 4/24/2009'
    ' Author:  BlueDevilFan'
    ' Outlook: All versions'
    Dim arrFolders As Variant, _
        varFolder As Variant, _
        bolBeyondRoot As Boolean
    On Error Resume Next
    If strFolderPath = "" Then
        Set OpenOutlookFolder = Nothing
    Else
        Do While Left(strFolderPath, 1) = "\"
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        Loop
        arrFolders = Split(strFolderPath, "\")
        For Each varFolder In arrFolders
            Select Case bolBeyondRoot
                Case False
                    Set OpenOutlookFolder = Outlook.Session.Folders(varFolder)
                    bolBeyondRoot = True
                Case True
                    Set OpenOutlookFolder = OpenOutlookFolder.Folders(varFolder)
            End Select
            If Err.Number <> 0 Then
                Set OpenOutlookFolder = Nothing
                Exit For
            End If
        Next
    End If
    On Error GoTo 0
End Function

UPDATE:A new error has risen: (After I fixed the New line issue on line 1 after "sndPlaySoundA") as refered to by Adrian below)

更新:出现了一个新错误:(在我在“sndPlaySoundA”之后的第 1 行修复了新行问题之后),如下面的 Adrian 所指)

"Compile Error Expected: End of statement" and the following word is highlighted: "Public"

“预期编译错误:语句结束”并突出显示以下单词:“公共”

UPDATE2: Next error:

UPDATE2:下一个错误:

Compile Error: User defined type not defined(For "Mailbox - supportdesk\Inbox")

编译错误:未定义用户定义的类型(对于“邮箱 - supportdesk\Inbox”)

Dim objFM1 As FolderMonitor

Private Sub Application_Quit()
    Set objFM1 = Nothing
End Sub

Private Sub Application_Startup()
    Set objFM1 = New FolderMonitor
    'Edit the folder path on the next line as needed.'
    objFM1.FolderToWatch OpenOutlookFolder("Mailbox - supportdesk\Inbox")
    End Sub

回答by Adrian

According to the code sample you've provided there you need a new line immediately after the _. The underscore character is a line continuation in VBA (which is what you're using, not VBScript. Slightly different beasts) and so requires that you continue on the next line, not the same line. So instead of

根据您在那里提供的代码示例,您需要在_. 下划线字符是 VBA 中的换行符(这是您使用的,而不是 VBScript。稍微不同的野兽),因此要求您继续下一行,而不是同一行。所以代替

Public Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _ (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long Public Declare Function MessageBox _
Lib "User32" Alias "MessageBoxA" _
    (ByVal hWnd As Long, _
    ByVal lpText As String, _
    ByVal lpCaption As String, _
    ByVal wType As Long) _
As Long

you should have

你应该有

Public Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _ 
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long 

Public Declare Function MessageBox _
Lib "User32" Alias "MessageBoxA" _
    (ByVal hWnd As Long, _
    ByVal lpText As String, _
    ByVal lpCaption As String, _
    ByVal wType As Long) _
As Long

EDIT: I obviously didn't read all the way to the end of that example line, or else I would have seen that the example somehow managed to mash two function declarations onto one line as well as using the invalid positioning of the line separator. I've fixed that up now.

编辑:我显然没有一直读到该示例行的末尾,否则我会看到该示例以某种方式设法将两个函数声明混合到一行中,并使用了行分隔符的无效定位。我现在已经解决了。