Hello,
I would like to know if this is possible to open a file with the "Open and
Repair" option with some command line argument.
It would be something like:
Winword Mydoc.doc /[open and repair tag]
Thanks
There isn't any built-in command that could be used this way. The
"Open and Repair" option is a parameter of the Documents.Open command.
One way to use it is shown in Method 2 at
http://support.microsoft.com/?id=893672, but I don't understand why
you'd want to run Open and Repair *every* time you open a document.
The way to do it from the command line is to write a macro, store it
in Normal.dot, and use the /m switch to run the macro on startup.
Because the document listed on the command line isn't yet open at the
time the /m switch runs the macro, you have to use some fancy
programming to get the document's name from the command line. I swiped
mine from
http://word.mvps.org/FAQs/MacrosVBA/CheckHowWordLaunched.htm
and embellished it a bit.
After pasting the following code into a module in Normal.dot and
saving it, run Word with a command line like
winword MyDoc.doc /mFileOpenAndRepair
or
winword "C:\docs\MyDoc.doc" /mFileOpenAndRepair
If you omit the path, the macro will supply the path to the folder
listed as the Documents folder in Tools > Options > File Locations.
The first thing you get is an error message saying Word couldn't open
the file. Just hit OK and let the macro continue. If anything in the
document needed to be repaired, you'll see a dialog that lists the
repairs.
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
Private Function CmdLinetoString(ByVal lngPtr As Long) As String
Dim strReturn As String
Dim StringLength As Long
'get the length of the string (not including the
'terminating null character)
StringLength = lstrlen(lngPtr)
'initialize our string so it has enough characters including
'the null character
strReturn = String$(StringLength + 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, StringLength)
'return the string
CmdLinetoString = strReturn
End Function
Private Function ExtractFileName(ByVal strIn As String) As String
Dim pos As Long
Const qt = """"
Const sp = " "
' remove program name
If Left$(strIn, 1) = qt Then
pos = InStr(2, strIn, qt)
strIn = LTrim$(Right$(strIn, Len(strIn) - pos))
Else
pos = InStr(strIn, sp)
strIn = LTrim$(Right$(strIn, Len(strIn) - pos))
End If
' remove switch(es)
pos = InStr(strIn, "/")
If pos Then
strIn = RTrim$(Left$(strIn, pos - 1))
End If
' trim off quotes
If Left$(strIn, 1) = qt Then
strIn = Replace(strIn, qt, "")
End If
' if path isn't explicit, assume default
pos = InStr(strIn, "\")
If pos = 0 Then
strIn = Options.DefaultFilePath(wdDocumentsPath) _
& "\" & strIn
End If
ExtractFileName = strIn
End Function
Public Sub FileOpenAndRepair()
' intended to be run from the
' command line with the /m switch
Dim FileToRepair As String
Dim strCommandLine As String
strCommandLine = CmdLinetoString(GetCommandLine())
FileToRepair = ExtractFileName(strCommandLine)
If Len(FileToRepair) Then
Documents.Open FileName:=FileToRepair, _
OpenAndRepair:=True
End If
End Sub
--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.