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

Re: Command line arguments for "Open and repair" ? by Jay

Jay
Wed Feb 15 22:13:32 CST 2006

On Wed, 15 Feb 2006 07:55:27 -0800, Drinkmilk
<Drinkmilk@discussions.microsoft.com> wrote:

>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 FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

Re: Command line arguments for "Open and repair" ? by Drinkmilk

Drinkmilk
Fri Feb 17 03:40:27 CST 2006

Thanks a lot for this really complete answer !

I tried and it works. (I don't know how to check wether it is actually
repaired because I don't have any corrupted file)

Do you know if this would also work for other Office documents (escpecially
Excel) ?

"Jay Freedman" wrote:

> On Wed, 15 Feb 2006 07:55:27 -0800, Drinkmilk
> <Drinkmilk@discussions.microsoft.com> wrote:
>
> >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 FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the
> newsgroup so all may benefit.
>



Re: Command line arguments for "Open and repair" ? by Jay

Jay
Fri Feb 17 11:50:20 CST 2006

I'm glad I could help. (As a matter of fact, my first response when I read
your question was that it wasn't possible. It caught my interest, though, so
I looked some more.)

I think the same macro will work in Excel if you change one statement.
Replace

>> Documents.Open FileName:=FileToRepair, _
>> OpenAndRepair:=True

with

>> Workbooks.Open FileName:=FileToRepair, _
>> CorruptLoad:=xlRepairFile

At least, I think that's the equivalent. I'm not really much of an Excel
programmer. :-) And I know nothing at all about similar commands in other
Office programs.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

Drinkmilk wrote:
> Thanks a lot for this really complete answer !
>
> I tried and it works. (I don't know how to check wether it is actually
> repaired because I don't have any corrupted file)
>
> Do you know if this would also work for other Office documents
> (escpecially Excel) ?
>
> "Jay Freedman" wrote:
>
>> On Wed, 15 Feb 2006 07:55:27 -0800, Drinkmilk
>> <Drinkmilk@discussions.microsoft.com> wrote:
>>
>>> 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 FAQ: http://word.mvps.org
>> Email cannot be acknowledged; please post all follow-ups to the
>> newsgroup so all may benefit.