+ Reply to Thread
Results 1 to 5 of 5

Problem using shell32.dll in VBA

Hybrid View

  1. #1
    Forum Contributor PingPing's Avatar
    Join Date
    02-19-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    158

    Problem using shell32.dll in VBA

    I'm having problems with the below code.
    I have incorporated it into the Code Window commandbar which simply calls PrintProcedure.
    The call is working fine, it's just that whenever I right click over a VBA procedure I want to print and select the "Print this procedure" entry (which calls PrintProcedure), Notepad opens up and always complains:
    Cannot find the [full path and filename] file. Do you want to create a new file?
    However, if I set a break point in PrintProcedure, call it as above and then step through it I don't get the complaint. Can anyone suggest what I'm doing wrong?
    Option Explicit
    
    Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
      (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
      ByVal lpParameters As String, ByVal lpDirectory As String, _
      ByVal nShowCmd As Long) As Long
    
    Public Sub PrintProcedure()
              
              Dim objModule As CodeModule, objPane As CodePane
              Dim lngStartLine As Long, lngEndLine As Long
              Dim lngStartCol As Long, lngEndCol As Long
              Dim lngCountLines As Long
              Dim strProcName As String, strPath As String
              Dim intCount As Integer
              Dim lngN As Long, FileNum As Long
              
              Set objPane = Application.VBE.ActiveCodePane
              Set objModule = objPane.CodeModule
              
              ' get current selection
              objPane.GetSelection lngStartLine, lngStartCol, lngEndLine, lngEndCol
              
              ' get procedure name
              strProcName = objModule.ProcOfLine(lngStartLine, vbext_pk_Proc)
              
              ' get first line of procedure
              lngStartLine = objModule.ProcBodyLine(strProcName, vbext_pk_Proc)
              
              ' count lines
              lngCountLines = objModule.ProcCountLines(strProcName, vbext_pk_Proc)
              
              ' get output file name
              strPath = Application.VBE.ActiveVBProject.fileName & "_" & strProcName & "_temp.txt"
    
              If strPath <> "False" Then
                        ' write to file
                        FileNum = FreeFile
                        Open strPath For Output As #FileNum
                        Print #FileNum, objModule.Lines(lngStartLine, lngCountLines)
                        Close #FileNum
              End If
    
              ShellExecute 0, "Print", strPath, "", "", 1
    
              Kill (strPath)
              
    End Sub

  2. #2
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Problem using shell32.dll in VBA

    assuming that you do have a text file named .....
    Option Explicit
    
    Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
      (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
      ByVal lpParameters As String, ByVal lpDirectory As String, _
      ByVal nShowCmd As Long) As Long
    
    Public Sub PrintProcedure()
              
              Dim objModule As CodeModule, objPane As CodePane
              Dim lngStartLine As Long, lngEndLine As Long
              Dim lngStartCol As Long, lngEndCol As Long
              Dim lngCountLines As Long
              Dim strProcName As String, strPath As String
              Dim intCount As Integer
              Dim lngN As Long, FileNum As Long
              
              Set objPane = Application.VBE.ActiveCodePane
              Set objModule = objPane.CodeModule
              
              ' get current selection
              objPane.GetSelection lngStartLine, lngStartCol, lngEndLine, lngEndCol
              
              ' get procedure name
              strProcName = objModule.ProcOfLine(lngStartLine, vbext_pk_Proc)
              
              ' get first line of procedure
              lngStartLine = objModule.ProcBodyLine(strProcName, vbext_pk_Proc)
              
              ' count lines
              lngCountLines = objModule.ProcCountLines(strProcName, vbext_pk_Proc)
           Debug.Print lngCountLines
              ' get output file name
              strPath = Application.VBE.ActiveVBProject.Filename & "_" & strProcName & "_temp.txt"
    Debug.Print strPath
              If strPath <> "False" Then
                        ' write to file
                        FileNum = FreeFile
                        Open strPath For Input As #1
                        Debug.Print objModule.Lines(lngStartLine, lngCountLines)
                        Close #FileNum
              End If
    
            '  ShellExecute 0, "Print", strPath, "", "", 1
    
            '  Kill (strPath)
              
    End Sub
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  3. #3
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Problem using shell32.dll in VBA

    ops... reading the code better change "input" to "output"
    still have to all ready have the file created that what the orgonial code was looking for
    and this
     Debug.Print objModule.Lines(lngStartLine, lngCountLines)
    to
    Print #FileNum, objModule.Lines(lngStartLine, lngCountLines)

  4. #4
    Forum Contributor PingPing's Avatar
    Join Date
    02-19-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    158

    Re: Problem using shell32.dll in VBA

    Pike, there is nothing unexpected appearing in the Immediate Window. As I said, if I step through (F8) the proc then everything works fine. It's only when I call it without slowing it down (via using F8), does it produce the error I previously mentioned.

    It seems to be the last line: Kill(strPath)
    If I remove that line, I don't experience any problems.
    What's the best way of deleting the text file after it's printed?
    Last edited by PingPing; 05-14-2010 at 06:04 AM.

  5. #5
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Problem using shell32.dll in VBA

    oh..
       Dim fs As FileSystemObject
       Set fs = New FileSystemObject
       
       fs.DeleteFile strPath
       MsgBox "The requested file was deleted."

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1