PDA

View Full Version : Getting arguments passed to opening document



ill_comms
07-28-2008, 04:21 AM
Hi, I have been attempting to load a word document with vba behind it. that code is run upon document_open() function executing.

This document is started from a javascript call to the ShellExecute("c:\documents\trial.doc","trialno=12345;trialtxt=qwerty","","open",1) activeX object which actually loads my word document with a parameter list.

What I'm wondering is how I retrieve this parameter list when running document_open?
Any help greatly appreciated!

Regards Hayden

Oorang
07-28-2008, 07:34 AM
This should get the command line for you:

Option Explicit

Private Declare Function GetCommandLineA Lib "kernel32" () As Long
Private Declare Function lstrlenA Lib "kernel32" (ByVal lpString As Long) As Long
Private Declare Sub RtlMoveMemory Lib "kernel32" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)

Public Function GetCommandLine() As String
Dim lngCmdLnPntr As Long
Dim lngCmdLnLen As Long
Dim strCmdLnVal As String
'Get a pointer to a string containing command line:
lngCmdLnPntr = GetCommandLineA
'Get length of command line to build buffer:
lngCmdLnLen = lstrlenA(lngCmdLnPntr)
If lngCmdLnLen Then 'Don't bother if string is empty.
'Build the buffer:
strCmdLnVal = String$(lngCmdLnLen, vbNullChar)
'Copy command line string to buffer:
RtlMoveMemory ByVal strCmdLnVal, ByVal lngCmdLnPntr, ByVal lngCmdLnLen
End If
'Set return value:
GetCommandLine = strCmdLnVal
End Function

ill_comms
07-28-2008, 08:46 PM
Hi Oorang,

lngCmdLnPntr came back with "C:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE" - Embedding

Which is fine, however, I do the following statement in my javascript:
objShell.ShellExecute(document_object.path, "trial=12345;trial2=67890", "", "open", 1);

As far as I understand it I can pass parameters that I want to retrieve in as the second argument on the ShellExecute function

Then somehow retrieve them when the document opens. Maybe I'm barking up the wrong tree.

Regards
Hayden

Oorang
07-29-2008, 09:57 AM
The problem with this approach is that while you can pass arguments to winword via ShellExecute, winword still has to translate them. I actually did manage to get the full parameter list over to word by using winword.exe as the file and then file name as a paramater to winword (with the arguments parameter). But even though you can get the document to launch word will always display an error complaining that it doesn't understand the rest of the parameters. You can still pull the parameter info out of word with the above procedure (provided the file name is the last argument) but you can't suppress the error. As an alternative approach, have you considered just taking control of a word instance from the script itself. Here is a rudimentary example:

<script type="text/javascript">
<!-- Don't Render
function Launch(){
alert("Press OK to Launch word")
w = new ActiveXObject("Word.Application")
w.Visible = -1
d = w.Documents.Open("C:\\Test\\Test.Doc")
w.Selection.WholeStory()
w.Selection.TypeText("foo bar baz")
d.Range(0,3).Bold=-1
}
-->
</script>

ill_comms
08-01-2008, 02:05 AM
Hi, thanks Ooorang, your right that worked a treat, I've opened the document then the macro used a public sub that I call from javascript to pass through the criteria.

Don't suppose you know how to loop through StoryRanges in javascript as you would in VBA?

eg:
Dim myStoryRange As Range
For Each myStoryRange In Documents(newDocumentName).StoryRanges

Next myStoryRange

Regards Hayden