Hi Everyone,
I have a vbs file named splitworddocs.vbs and run it by entering the following into a cmd line.
When the vbs file runs I get the following error:
Window Script Host Error
Script: c:\splitworddocs.vbs
Line: 59
Char: 9
Error: Type mismatch: 'InchesToPoints'
Code: 800A000D
Source: Microsoft VBScript Runtime Error
I originally wrote the code within the Microsoft Word VBA editor and it ran fine but I was then asked to create it as a vbs file so I can past 2 parameters to it.
Here's the code within the splitworddocs.vbs file
Any help you can provide would be much appreciated!
I have a vbs file named splitworddocs.vbs and run it by entering the following into a cmd line.
Code:
wscript c:\splitworddocs.vbs "c:\users\kap\my documents\consulting\cmayberry" 450401.doc
When the vbs file runs I get the following error:
Window Script Host Error
Script: c:\splitworddocs.vbs
Line: 59
Char: 9
Error: Type mismatch: 'InchesToPoints'
Code: 800A000D
Source: Microsoft VBScript Runtime Error
I originally wrote the code within the Microsoft Word VBA editor and it ran fine but I was then asked to create it as a vbs file so I can past 2 parameters to it.
Here's the code within the splitworddocs.vbs file
Any help you can provide would be much appreciated!
Code:
dim p
dim f
dim objArgs
Set objArgs = Wscript.Arguments
count = objArgs.count
p = objArgs(0)
f = objArgs(1)
splitworddocs p, f
Sub splitworddocs(pth,fname)
Dim fso
Dim outfile
Dim lCurrentStart
Dim lCurrentEnd
Dim lDocumentEnd
Dim lOutputCount
Dim fn
Dim yn
Dim oWord
Set fso = CreateObject("Scripting.FileSystemObject")
lOutputCount = 0
'Launch Word and make it visible
Set oWord = CreateObject("Word.Application")
oWord.Visible = False
'check path
if right(pth,1) = "\" then
else
pth = pth & "\"
end if
'check file is a word document.
if right(fname,3) = "doc" then
fn = left(fname,len(fname)-4)
yn = "y"
else
if right(fname,4) = "docx" then
fn = left(fname,len(fname)-5)
yn = "y"
else
yn = "n"
end if
end if
if yn = "y" then
'Open the test document
Set oDoc = oWord.Documents.Open (pth & fname)
'Set the margins in the document so it spaces the data into predefined sizes.
With oDoc.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = InchesToPoints(2.75)
.BottomMargin = InchesToPoints(2.88)
.LeftMargin = InchesToPoints(0.25)
.RightMargin = InchesToPoints(0.5)
.Gutter = InchesToPoints(0)
.HeaderDistance = InchesToPoints(0.5)
.FooterDistance = InchesToPoints(0.5)
.PageWidth = InchesToPoints(8.5)
.PageHeight = InchesToPoints(11)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosLeft
End With
'Find the beginning end of the document
oDoc.Select
lCurrentStart = oWord.Selection.Start
lCurrentEnd = lCurrentStart
lDocumentEnd = oWord.Selection.End
'Move the insertion point to the beginning of the document
oWord.Selection.Collapse wdCollapseStart
Do While (lCurrentEnd < lDocumentEnd)
'Move the insertion pointer to the bottom of this page
oWord.Browser.Target = wdBrowsePage
oWord.Browser.Next
lCurrentEnd = oWord.Selection.End
'On the last page, the start and end will be the same
If (lCurrentStart = lCurrentEnd) Then
lCurrentEnd = lDocumentEnd
End If
'Capture the Range of the current page
Set oRange = oDoc.Range(lCurrentStart, lCurrentEnd)
'Create a new document and copy the range to it
Set oNewDoc = oWord.Documents.Add
oRange.Copy
oNewDoc.Range(0, 0).Paste
'Release the Range so we don't leak references
Set oRange = Nothing
'Save the new document and close it
oNewDoc.SaveAs (pth & fn & "_" & lOutputCount + 1 & ".docx")
' You can save as another FileFormat. If so, change the
' file extension accordingly.
oNewDoc.Close
Set oNewDoc = Nothing
'Increment the output counter so we don't overwrite this file later
lOutputCount = lOutputCount + 1
'Reset the current start position
lCurrentStart = oWord.Selection.End
Loop
oDoc.Close False
oWord.Quit
msgbox "Finished"
Set outfile = fso.CreateTextFile(pth & fn & ".txt", True)
outfile.writeline "Success|" & lOutputCount + 1
outfile.Close True
Set fso = Nothing
Set outfile = Nothing
Else
msgbox "Didn't run"
End if
End Sub