Visual Basic编译错误 - 无效字符

时间:2013-05-21 05:06:50

标签: vba outlook outlook-vba

我从互联网上获得了VB SCRIPT,以便在Outlook(2010)中为辅助电子邮件帐户创建新邮件提醒。

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

“编译错误:字符无效”

调试器在下一行中为_字符加下划线:“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

更新:新错误已经上升:(在“sndPlaySoundA”之后修改了第1行的新行问题),如下面Adrian所说:

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

UPDATE2 :下一个错误:

编译错误:未定义用户定义的类型(对于“邮箱 - 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

1 个答案:

答案 0 :(得分:1)

根据您提供的代码示例,您需要在_之后立即使用新行。下划线字符是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
你应该

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

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