Intercept a switch with VBA

S

Steve

Hi everyone,

I want to intercept a switch put at the opening of a
word document. Ex.: "C:\Document.doc" /test. I want to
intercept it in the Sub Document_Open in order to do a
validation.

Is it possible ?

If it is how can I do it.

Thank you everyone for your response.
 
P

Peter Hewett

Hi Steve

You this code to get the command line that Word was started with. Then just parse out the
command line.

Public Declare Function GetCommandLine Lib "kernel32" _
Alias "GetCommandLineA" () As Long
Public Declare Function lstrlen Lib "kernel32" Alias _
"lstrlenA" (ByVal lpString As Long) As Long
Public Declare Function lstrcpy Lib "kernel32" _
Alias "lstrcpyA" (ByVal lpStringTo As String, _
ByVal lpStringFrom As Long) As Long

Public Sub ShowCommandLine()
MsgBox Trim$(StringFormPointer(GetCommandLine()))
End Sub

Public Function StringFormPointer(ByVal lngStrPointer As Long) As String
Dim lngCmdLength As Long
Dim strOut As String

' Get the length of the string the pointer points to
lngCmdLength = lstrlen(lngStrPointer)

' Preallocated the correct size buffer. add 1 as the string is null terminated
strOut = Space$(lngCmdLength + 1)

' Copy string to VBA buffer
lstrcpy strOut, lngStrPointer

' Trim off the null character that terminates the string
StringFormPointer = TrimN(strOut)
End Function

Function TrimN(ByVal strValue As String) As String
Dim lngPos As Long

lngPos = InStr(strValue, vbNullChar)
If lngPos > 0 Then

' Return everthing before the null character
TrimN = Left$(strValue, lngPos - 1)
Else
' Not found, so just return the original value.
TrimN = strValue
End If
End Function

HTH + Cheers - Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top