Page 1 of 1 1
Topic Options
#119808 - 2004-05-19 11:42 AM Need Help Converting VB to Kix
Grasshopper75 Offline
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 Offline
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
#119810 - 2004-05-19 01:31 PM Re: Need Help Converting VB to Kix
Co Offline
MM club member
***

Registered: 2000-11-20
Posts: 1342
Loc: NL
Maybe this UDF can help you out:
vbs2Kix() - automated VBScript to KiX-script conversion
_________________________
Co


Top
#119811 - 2004-05-19 01:34 PM Re: Need Help Converting VB to Kix
Grasshopper75 Offline
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 Offline
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
#119813 - 2004-05-19 02:10 PM Re: Need Help Converting VB to Kix
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Grass, looks to me like this script is VBA and was meant to run in the context of the Microsoft Word application model (like a macro) ... things like this:

$objDoc = Documents.Open($strFilePath + $strFileName)

are a dead give-away ... you'll need to create an instance of Word like this:

$Word = CreateObject("Word.Application")

then prefix the Application object to all the word objects, like this:

$objDoc = $Word.Documents.Open($strFilePath + $strFileName)

-Shawn

Top
#119814 - 2004-05-19 02:23 PM Re: Need Help Converting VB to Kix
Grasshopper75 Offline
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
#119815 - 2004-05-19 03:49 PM Re: Need Help Converting VB to Kix
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
ok, getting closer.
post your newist code if you can and without the double line feeds.
it's not really that long

and indeed, the vbs2kix is broken.
haven't got time to fix it yet.
_________________________
!

download KiXnet

Top
#119816 - 2004-05-19 04:13 PM Re: Need Help Converting VB to Kix
Grasshopper75 Offline
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
#119817 - 2004-05-19 04:30 PM Re: Need Help Converting VB to Kix
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Should look something like:

$objDoc = $Word.Documents.Open($strFilePath + $strFileName)
$objTemplate = $objDoc.AttachedTemplate
$dlgTemplate = $Word.Dialogs($wdDialogToolsTemplates)
$strPath = $dlgTemplate.Template

and then, your going to have to find the enumeration value for this:

$wdDialogToolsTemplates = <somenumber>

find the excel/word object browser good for that.

Top
#119818 - 2004-05-19 06:19 PM Re: Need Help Converting VB to Kix
Grasshopper75 Offline
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
#119819 - 2004-05-19 06:22 PM Re: Need Help Converting VB to Kix
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Good going - you still need to work out this enum though:

$dlgTemplate = $Word.Dialogs(wdDialogToolsTemplates)

Need any assistance in finding the number ?

-Shawn

Top
#119820 - 2004-05-19 06:25 PM Re: Need Help Converting VB to Kix
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Const wdDialogToolsTemplates = 87 (&H57)

;0)

Top
#119821 - 2004-05-19 06:26 PM Re: Need Help Converting VB to Kix
Grasshopper75 Offline
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
#119822 - 2004-05-19 06:26 PM Re: Need Help Converting VB to Kix
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
See above
Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1046 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.065 seconds in which 0.025 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org