Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vbs script error

Status
Not open for further replies.

kphu

MIS
May 30, 2002
346
US
Hi Everyone,

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

 
InchesToPoints is not defined in the program. It was probably a native function in VBA but not in VBS.

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
I figured it out...all I needed to do was add the object infront of it. In this case oWord.InchesToPoint, I also ran into an issue with some of the word constants such as wdOrientPortrait, etc.

To resolve that I looked up in the word VBA editor what value wdOrientPortrait corresponded to which is 0.

Here's the code.

Code:
With oDoc.PageSetup
        .LineNumbering.Active = False
        .Orientation = 0 'wdOrientPortrait
        .TopMargin = oWord.InchesToPoints(2.75)
        .BottomMargin = oWord.InchesToPoints(2.88)
        .LeftMargin = oWord.InchesToPoints(0.25)
        .RightMargin = oWord.InchesToPoints(0.5)
        .Gutter = oWord.InchesToPoints(0)
        .HeaderDistance = oWord.InchesToPoints(0.5)
        .FooterDistance = oWord.InchesToPoints(0.5)
        .PageWidth = oWord.InchesToPoints(8.5)
        .PageHeight = oWord.InchesToPoints(11)
        .FirstPageTray = wdPrinterDefaultBin
        .OtherPagesTray = wdPrinterDefaultBin
        .SectionStart = 2 'wdsectionstart
        .OddAndEvenPagesHeaderFooter = False
        .DifferentFirstPageHeaderFooter = False
        .VerticalAlignment = 0 'wdAlignVerticalTop
        .SuppressEndnotes = False
        .MirrorMargins = False
        .TwoPagesOnOne = False
        .BookFoldPrinting = False
        .BookFoldRevPrinting = False
        .BookFoldPrintingSheets = 1
        .GutterPos = 0 'wdGutterPosLeft
    End With
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top