Reading cmd line options from VBA 
Author Message
 Reading cmd line options from VBA

Good Morning,
How can I read command line options passed to Word from within my VBA
scripting. Like if I run word with

Word.exe Custom.dot -load <filename>

I need to know what option was used and what was passed filename.

Thanks!

- shridhar -



Wed, 28 Jul 2004 00:16:14 GMT  
 Reading cmd line options from VBA
Hi Shridhar,

You'll need API calls to do that. This was discussed some time ago. Here's a copy of the message Will Rickards posted back then:

<copy>
here is an example that uses the commandline

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

Public Function LPTSTRtoString(ByVal lngPtr As Long) As String
Dim strReturn As String
Dim lngStrLen As Long

'get the length of the string (not including the terminating null character)
lngStrLen = lstrlen(lngPtr)

'initialize our string so it has enough characters including the null character
strReturn = String$(lngStrLen + 1, 0)

'copy the string we have a pointer to into our new string
lstrcpy strReturn, lngPtr

'now strip off the null character at the end
strReturn = Left$(strReturn, lngStrLen)

'return the string
LPTSTRtoString = strReturn

End Function

Public Sub AutoExec()
'runs everytime word starts

Dim objCurFile As RecentFile
Dim strCommandLine As String
Dim strDirectory As String

'get the commandline
strCommandLine = LPTSTRtoString(GetCommandLine())

Debug.Print "The CommandLine = " & strCommandLine

'strip off everything before the /m
strCommandLine = Right(strCommandLine, Len(strCommandLine) - InStr(1, strCommandLine, "/m", vbBinaryCompare) + 1)

Debug.Print "The /m commandline = " & strCommandLine

'check for FileOpen
If InStr(1, strCommandLine, "/mFileOpen") <> 0 Then

    Debug.Print "Found FileOpen"

    'read the directory
    strDirectory = Right(strCommandLine, Len(strCommandLine) - Len("/mFileOpen"))

    Debug.Print "Directory = " & strDirectory

    If Len(Dir(strDirectory, vbDirectory)) > 0 Then

        Debug.Print "changing the fileopen directory"
        ChangeFileOpenDirectory strDirectory
        Application.Run "FileOpen"

    End If

End If

End Sub
--
Will Rickards  - Word MVP - http://members.home.net/wrickard
Please reply only to newsgroup - Use of any info/code provided is at your own risk
<end copy>

Hope this helps,
regards,
Astrid

So that all can benefit from the discussion, please post all follow-ups to the newsgroup.
Visit the MVP Word FAQ site at http://www.mvps.org/word/


Quote:
> Good Morning,
> How can I read command line options passed to Word from within my VBA
> scripting. Like if I run word with

> Word.exe Custom.dot -load <filename>

> I need to know what option was used and what was passed filename.

> Thanks!

> - shridhar -



Thu, 29 Jul 2004 07:19:00 GMT  
 Reading cmd line options from VBA
Thanks Astrid


Hi Shridhar,

You'll need API calls to do that. This was discussed some time ago. Here's a
copy of the message Will Rickards posted back then:

<copy>
here is an example that uses the commandline

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

Public Function LPTSTRtoString(ByVal lngPtr As Long) As String
Dim strReturn As String
Dim lngStrLen As Long

'get the length of the string (not including the terminating null character)
lngStrLen = lstrlen(lngPtr)

'initialize our string so it has enough characters including the null
character
strReturn = String$(lngStrLen + 1, 0)

'copy the string we have a pointer to into our new string
lstrcpy strReturn, lngPtr

'now strip off the null character at the end
strReturn = Left$(strReturn, lngStrLen)

'return the string
LPTSTRtoString = strReturn

End Function

Public Sub AutoExec()
'runs everytime word starts

Dim objCurFile As RecentFile
Dim strCommandLine As String
Dim strDirectory As String

'get the commandline
strCommandLine = LPTSTRtoString(GetCommandLine())

Debug.Print "The CommandLine = " & strCommandLine

'strip off everything before the /m
strCommandLine = Right(strCommandLine, Len(strCommandLine) - InStr(1,
strCommandLine, "/m", vbBinaryCompare) + 1)

Debug.Print "The /m commandline = " & strCommandLine

'check for FileOpen
If InStr(1, strCommandLine, "/mFileOpen") <> 0 Then

    Debug.Print "Found FileOpen"

    'read the directory
    strDirectory = Right(strCommandLine, Len(strCommandLine) -
Len("/mFileOpen"))

    Debug.Print "Directory = " & strDirectory

    If Len(Dir(strDirectory, vbDirectory)) > 0 Then

        Debug.Print "changing the fileopen directory"
        ChangeFileOpenDirectory strDirectory
        Application.Run "FileOpen"

    End If

End If

End Sub
--
Will Rickards  - Word MVP - http://members.home.net/wrickard
Please reply only to newsgroup - Use of any info/code provided is at your
own risk
<end copy>

Hope this helps,
regards,
Astrid

So that all can benefit from the discussion, please post all follow-ups to
the newsgroup.
Visit the MVP Word FAQ site at http://www.mvps.org/word/



Quote:
> Good Morning,
> How can I read command line options passed to Word from within my VBA
> scripting. Like if I run word with

> Word.exe Custom.dot -load <filename>

> I need to know what option was used and what was passed filename.

> Thanks!

> - shridhar -



Sat, 31 Jul 2004 01:28:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Reading a Word Doc Line by Line in VBA

2. Reading in Command Line options into a VB program

3. Link and Lib CMD options

4. Reading position on the line by using VBA

5. Orphan Lines of records in multi-line option

6. Using CreateProcessA (with CREATE_FORCEDOS) to run PVCS cmd-line app

7. Starting automacros with parameters from cmd-line

8. Different results, macro from cmd line vs app

9. Cmd line args?

10. Compiling multi-project solution at cmd line

11. basica cmd line params - HELP!

12. Cmd Line Interface to VB IDE?

 

 
Powered by phpBB® Forum Software