0

我正在学习Visual Basic 6.0,并且正在构建一个应用程序来捕获屏幕截图。

我找到了一些示例代码来研究应用程序。但是,我得到:

语句不在有效的命名空间中

但是,它将错误指向“Private Sub Main()”。

我将在下面附上截图:

在此处输入图像描述

Option Explicit

Private Sub Main()

    ' Declare Variables used in the program
    Dim status As Long       'VISA function status return code
    Dim defrm As Long        'Session to Default Resource Manager
    Dim vi As Long           'Session to instrument
    Dim x As Integer         'Loop Variable
    Dim ArrayPtr(1) As Long  'Array of Pointers
    Dim length As Long       'Number of bytes returned from instrument
    Dim fnum As Integer      'File number to used to open file to store data
    Dim isOpen As Boolean    'Boolean flag used to keep track of open file
    Dim headerlength As Long 'length of header
    Dim ResultsArray(50000) As Byte 'Results array, Big enough to hold a GIF

    'Set the default number of bytes that will be contained in the
    'ResultsArray to 50,000 (50 kB)
    length = 50000

    'Set the array of pointers to the addresses of the variables
    ArrayPtr(0) = VarPtr(length)
    ArrayPtr(1) = VarPtr(ResultsArray(0))

    'Delete picture.gif file if it exists
    On Error Resume Next
    Kill "picture.gif"

    On Error GoTo Error_Handler

    'Open the default resource manager session
    status = viOpenDefaultRM(defrm)

    'Open the session. Note: For PSA, to use LAN, change the string to
    ' "TCPIP0::xxx.xxx.xxx.xxx::inst0::INSTR" where xxxxx is the IP address
    status = viOpen(defrm, "GPIB0::18::INSTR", 0, 0, vi)
    If (status < 0) Then GoTo VisaErrorHandler

    'Set the I/O timeout to fifteen seconds
    status = viSetAttribute(vi, VI_ATTR_TMO_VALUE, 15000)
    If (status < 0) Then GoTo VisaErrorHandler

    'Store the current screen image on flash as C:PICTURE.GIF
    status = viVPrintf(vi, ":MMEM:STOR:SCR 'C:PICTURE.GIF'" + Chr$(10), 0)
    If (status < 0) Then GoTo VisaErrorHandler

    'Grab the screen image file from the instrument
    status = viVQueryf(vi, ":MMEM:DATA? 'C:PICTURE.GIF'" + Chr$(10), _
             "%#y", ArrayPtr(0))

    'Delete the tempory file on the flash named C:PICTURE.GIF
    status = viVPrintf(vi, ":MMEM:DEL 'C:PICTURE.GIF'" + Chr$(10), 0)
    If (status < 0) Then GoTo VisaErrorHandler

    'Close the vi session and the resource manager session
    Call viClose(vi)
    Call viClose(defrm)

    'Store the results in a text file
    fnum = FreeFile()  'Get the next free file number
    Open "picture.gif" For Binary As #fnum
    isOpen = True
    headerlength = 2 + (Chr$(ResultsArray(1)))
    For x = headerlength To length - 2
        Put #fnum, , ResultsArray(x)
    Next x

    ' Intentionally flow into Error Handler to close file
Error_Handler:
    'Raise the error (if any), but first close the file
    If isOpen Then Close #fnum
    If Err Then Err.Raise Err.Number, , Err.Description
    Exit Sub

VisaErrorHandler:
   Dim strVisaErr As String * 200
   Call viStatusDesc(defrm, status, strVisaErr)
   MsgBox "*** Error : " & strVisaErr, vbExclamation, "VISA Error Message"
   Exit Sub
End Sub

我不确定为什么以及如何解决此问题以构建 EXE 文件...

4

1 回答 1

1

您不能将方法(Sub Main)直接放在命名空间内。它必须在模块或类中。

尝试这样的事情:

Option Explicit

Module MyModule
    Private Sub Main()
        '... The rest of the code here
    End Sub
End Module

您的错误还表明文件名是screen.bas。我不认为 .bas 文件在 VB.NET 中受支持。更改文件名,使其具有 .vb 扩展名。

最后,不要使用On Error... GoTo....你应该使用Try ... Catch

于 2014-09-02T13:57:38.367 回答