#119808 - 2004-05-19 11:42 AM
Need Help Converting VB to Kix
|
Grasshopper75
Getting the hang of it
Registered: 2002-01-15
Posts: 99
Loc: Melbourne, Australia
|
We have just discovered a problem with thousands of Word documents after switching off an old server. The problem relates to old Word 97 documents still trying to reference the old template location. The Microsoft KB article 830561 describes the problem and a workaround solution via method 4.
I am wondering if I can somehow port this VB script into a Kix script to run against our files. While I am familiar with most of the VB commands there are some that I do not know the Kix exquivalent if any.
This is the VB code supplied by Microsoft
Code:
Sub Test()
Dim strFilePath As String
Dim strPath As String
Dim intCounter As Integer
Dim strFileName As String
Dim OldServer As String
Dim objDoc As Document
Dim objTemplate As Template
Dim dlgTemplate As Dialog
Dim nServer As Integer
'hardcode the name of the old server.
OldServer = "{enter the name of the Old Server}"
nServer = Len(OldServer)
strFilePath = InputBox("What is the folder location that you want to use?")
If Right(strFilePath, 1) <> "\" Then strFilePath = strFilePath & "\"
strFileName = Dir(strFilePath & "*.doc")
Do While strFileName <> ""
Set objDoc = Documents.Open(strFilePath & strFileName)
Set objTemplate = objDoc.AttachedTemplate
Set dlgTemplate = Dialogs(wdDialogToolsTemplates)
strPath = dlgTemplate.Template
If LCase(Left(strPath, nServer)) = LCase(OldServer) Then
objDoc.AttachedTemplate = NormalTemplate
End If
strFileName = Dir()
objDoc.Save
objDoc.Close
Loop
Set objDoc = Nothing
Set objTemplate = Nothing
Set dlgTemplate = Nothing
End Sub
This is my attempt, have not tested it as I know there are likely to be errors.
Code:
Function Test()
Dim $strFilePath
Dim $strPath
Dim $intCounter
Dim $strFileName
Dim $OldServer
Dim $objDoc
Dim $objTemplate
Dim $dlgTemplate
Dim $nServer
;'hardcode the name of the old server.'
$Form = CreateObject("Kixtart.Form")
$OldServer = "old server name here"
$nServer = Len($OldServer)
$strFilePath = $Form.InputBox("What is the folder location that you want to use?")
If Right($strFilePath, 1) <> "\" $strFilePath = $strFilePath + "\" EndIf
$strFileName = Dir($strFilePath + "*.doc")
Do While $strFileName <> ""
$objDoc = Documents.Open($strFilePath + $strFileName)
$objTemplate = objDoc.AttachedTemplate
$dlgTemplate = Dialogs(wdDialogToolsTemplates)
$strPath = dlgTemplate.Template
If Lcase(Left($strPath, $nServer)) = Lcase($OldServer)
$rc = objDoc.AttachedTemplate = NormalTemplate
EndIf
$strFileName = Dir()
$rc = objDoc.Save
$rc = objDoc.Close
Loop
$objDoc = ""
$objTemplate = ""
$dlgTemplate = ""
EndFunction
Any help would be appreciated.
Cheers,
Grasshopper75
|
|
Top
|
|
|
|
#119809 - 2004-05-19 12:58 PM
Re: Need Help Converting VB to Kix
|
Grasshopper75
Getting the hang of it
Registered: 2002-01-15
Posts: 99
Loc: Melbourne, Australia
|
I've almost got it working, my only issue is that it stops after one file. Obviously it's something to do with the Do/Until or While/Loop.
Will keep pressing on. Here's the code so far
Code:
Function Test()
Dim $strFilePath
Dim $strPath
Dim $intCounter
Dim $strFileName
Dim $OldServer
Dim $objDoc
Dim $objTemplate
Dim $dlgTemplate
Dim $nServer
$Form = CreateObject("Kixtart.Form")
$OldServer = $Form.InputBox("What is the server name?")
$nServer = Len($OldServer)
$strFilePath = $Form.InputBox("What is the folder location that you want to use?")
If Right($strFilePath, 1) <> "\"
$strFilePath = $strFilePath + "\"
EndIf
$strFileName = Dir($strFilePath + "*.doc")
While $strFileName <> ""
$objDoc = Documents.Open($strFilePath + $strFileName)
$objTemplate = objDoc.AttachedTemplate
$dlgTemplate = Dialogs(wdDialogToolsTemplates)
$strPath = dlgTemplate.Template
If Lcase(Left($strPath, $nServer)) = Lcase($OldServer)
$rc = objDoc.AttachedTemplate = NormalTemplate
EndIf
$strFileName = Dir()
$rc = objDoc.Save
$rc = objDoc.Close
Loop
$objDoc = ""
$objTemplate = ""
$dlgTemplate = ""
EndFunction
Test ()
|
|
Top
|
|
|
|
#119811 - 2004-05-19 01:34 PM
Re: Need Help Converting VB to Kix
|
Grasshopper75
Getting the hang of it
Registered: 2002-01-15
Posts: 99
Loc: Melbourne, Australia
|
Yeah have already tried that UDF. No luck unfortunately.
Thanks anyway.
|
|
Top
|
|
|
|
#119812 - 2004-05-19 01:48 PM
Re: Need Help Converting VB to Kix
|
Grasshopper75
Getting the hang of it
Registered: 2002-01-15
Posts: 99
Loc: Melbourne, Australia
|
OK, I've gathered the problem is something to do with the COM objects. I'm trying (ie. RTFM) but could do with a little help.
Cheers
|
|
Top
|
|
|
|
#119814 - 2004-05-19 02:23 PM
Re: Need Help Converting VB to Kix
|
Grasshopper75
Getting the hang of it
Registered: 2002-01-15
Posts: 99
Loc: Melbourne, Australia
|
Thanks Shawn, just figured it out minutes after I posted. It seems to open the document now but not doing the other functions. Still figuring it out...
Edited by Grasshopper75 (2004-05-19 03:10 PM)
|
|
Top
|
|
|
|
#119816 - 2004-05-19 04:13 PM
Re: Need Help Converting VB to Kix
|
Grasshopper75
Getting the hang of it
Registered: 2002-01-15
Posts: 99
Loc: Melbourne, Australia
|
Code:
Function Test()
Dim $strFilePath
Dim $strPath
Dim $intCounter
Dim $strFileName
Dim $OldServer
Dim $objDoc
Dim $objTemplate
Dim $dlgTemplate
Dim $nServer
$Form=CreateObject("Kixtart.Form")
$OldServer = $Form.InputBox("What is the server name?")
$nServer = Len($OldServer)
$strFilePath = $Form.InputBox("What is the folder location that you want to use?")
If Right($strFilePath, 1) <> "\" $strFilePath = $strFilePath + "\" EndIf
$strFileName = Dir($strFilePath + "*.doc")
Do While $strFileName <> ""
$Word = CreateObject("Word.Application") $objDoc = $Word.Documents.Open($strFilePath + $strFileName)
$objTemplate = $Word.objDoc.AttachedTemplate
$dlgTemplate = $Word.Dialogs(wdDialogToolsTemplates)
$strPath = $Word.dlgTemplate.Template
If Lcase(Left($strPath, $nServer)) = Lcase($OldServer)
$Word.objDoc.AttachedTemplate = $Word.NormalTemplate
EndIf
$strFileName = Dir("")
$Save = $Word.objDoc.Save
$Close = $Word.objDoc.Close
Loop
$objDoc = ""
$objTemplate = ""
$dlgTemplate = ""
EndFunction
Test ()
Its been a frustrating lession in COM objects that's for sure !!
|
|
Top
|
|
|
|
#119818 - 2004-05-19 06:19 PM
Re: Need Help Converting VB to Kix
|
Grasshopper75
Getting the hang of it
Registered: 2002-01-15
Posts: 99
Loc: Melbourne, Australia
|
I'm sooo close now, it works but gives an error after it finishes, will try and work it out but for the moment, here it is..
Code:
Function WordTemplateFix()
Dim $strFilePath,$strPath,$intCounter,$strFileName,$OldServer,$objDoc,$objTemplate,$dlgTemplate,$nServer
$Form=CreateObject("Kixtart.Form")
$OldServer = $Form.InputBox("What is the server name?")
$nServer = Len($OldServer)
$strFilePath = $Form.InputBox("What is the folder location that you want to use?")
If Right($strFilePath, 1) <> "\"
$strFilePath = $strFilePath + "\"
EndIf
$strFileName = Dir($strFilePath + "*.doc")
Do While $strFileName <> ""
$Word = CreateObject("Word.Application")
$objDoc = $Word.Documents.Open($strFilePath + $strFileName)
$objTemplate = $objDoc.AttachedTemplate
$dlgTemplate = $Word.Dialogs(wdDialogToolsTemplates)
$strPath = $dlgTemplate.Template
If Lcase(InStr($strPath, $nServer)) = Lcase($OldServer)
$objDoc.AttachedTemplate = $Word.NormalTemplate
EndIf
$strFileName = Dir("")
$Save = $objDoc.Save
$Close = $objDoc.Close
Loop
$objDoc = ""
$objTemplate = ""
$dlgTemplate = ""
EndFunction
WordTemplateFix ()
Thanks for your help Shawn and Lonkero
Cheers,
Grasshopper75
|
|
Top
|
|
|
|
#119821 - 2004-05-19 06:26 PM
Re: Need Help Converting VB to Kix
|
Grasshopper75
Getting the hang of it
Registered: 2002-01-15
Posts: 99
Loc: Melbourne, Australia
|
Shawn,
I looked for it in the chm files supplied with Office, but couldn't find anything. In the end I tried without setting it as a variable and it appears to work, so if you can find something else then I'm happy for your assistance.
It's taken me ages to figure this out, but at the end of the day I have learned loads so thanks again guys.
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 1336 anonymous users online.
|
|
|