Re: Module Help...(i feel dumb....)
Try changing Dim to Public.
When declaring variables in an event, use Dim.
When declaring variables at the form level, use Private
When declaring variables at the module level, use Public
Re: Module Help...(i feel dumb....)
Ok, that didn't work, but i have found a workaround, by simpling making the MsgBox pop-up from the module itself... a little un-orthodox but still, it works.
here is the code i am now using, anymore answers are welcome :)
VB Code:
'Form1
Private Sub Command1_Click()
Call getWinampWindow
End Sub
and the module -
VB Code:
'Module1
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public lHandle As Long
Public NowPlayingStr As String
Function getWinampWindow()
lHandle = FindWindow("Winamp v1.x", vbNullString)
If lHandle > 0 Then
Dim lRet As Long
Dim sTitle As String * 256
Dim sCaption As String
lRet = GetWindowText(lHandle, sTitle, Len(sTitle))
sCaption = Left(sTitle, InStr(1, sTitle, vbNullChar, vbTextCompare) - 10)
Do
If IsNumeric(Left(sCaption, 1)) = True Or Left(sCaption, 1) = "." Or Left(sCaption, 1) = " " Then
sCaption = Right(sCaption, Len(sCaption) - 1)
Else
Exit Do
End If
Loop
Do
If IsNumeric(Right(sCaption, 1)) = True Or Right(sCaption, 1) = ")" Or Right(sCaption, 1) = " " Or Right(sCaption, 1) = ":" Then
sCaption = Left(sCaption, Len(sCaption) - 1)
ElseIf Right(sCaption, 1) = "(" Then
sCaption = Left(sCaption, Len(sCaption) - 1)
Exit Do
Else
Exit Do
End If
Loop
NowPlayingStr = sCaption
MsgBox NowPlayingStr
Else
NowPlayingStr = "Winamp 6 Not Found"
MsgBox NowPlayingStr
End If
End Function
Re: Module Help...(i feel dumb....)
as the code you have in your module is a Funtion, it needs to return a value, but to do that you have to set the function to have a value within the function, so what you should do is get rid of the variable NowPlayingStr and wherever you mention it in the code (like NowPlayingStr = sCaption), replace it with getWinampWindow (so it would say getWinampWindow = sCaption) and then in your code instead of saying
VB Code:
Call getWinampWindow
msgbox NowPlayingStr
just use:
Re: Module Help...(i feel dumb....)
Quote:
Originally Posted by Arachnid13
as the code you have in your module is a Funtion, it needs to return a value,
Good point Arachnid13. I missed that.
thegreatone: Change your function to Public Function getWinampWindow() As String and you should get your return string.
Re: Module Help...(i feel dumb....)
Finished, your both getting good rep. thanks so much.
Final Code-
VB Code:
'in a module
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public lHandle As Long
Function getWinampWindow()
lHandle = FindWindow("Winamp v1.x", vbNullString)
If lHandle > 0 Then
Dim lRet As Long
Dim sTitle As String * 256
Dim sCaption As String
lRet = GetWindowText(lHandle, sTitle, Len(sTitle))
sCaption = Left(sTitle, InStr(1, sTitle, vbNullChar, vbTextCompare) - 10)
Do
If IsNumeric(Left(sCaption, 1)) = True Or Left(sCaption, 1) = "." Or Left(sCaption, 1) = " " Then
sCaption = Right(sCaption, Len(sCaption) - 1)
Else
Exit Do
End If
Loop
Do
If IsNumeric(Right(sCaption, 1)) = True Or Right(sCaption, 1) = ")" Or Right(sCaption, 1) = " " Or Right(sCaption, 1) = ":" Then
sCaption = Left(sCaption, Len(sCaption) - 1)
ElseIf Right(sCaption, 1) = "(" Then
sCaption = Left(sCaption, Len(sCaption) - 1)
Exit Do
Else
Exit Do
End If
Loop
getWinampWindow = sCaption
Else
getWinampWindow = "Winamp 6 Not Found"
End If
End Function
It is simply accessed like this -
Do you think this is worth posting in the codebank ?
Re: Module Help...(i feel dumb....)
Quote:
Originally Posted by thegreatone
Do you think this is worth posting in the codebank ?
If you are going to do that, make sure you include a detailed explanation of what your code does, and how it does it.