Current location: Hot Scripts Forums » Programming Languages » Visual Basic » VBA to VBS conversion error

VBA to VBS conversion error

  #1 (permalink)  
New Member
Join Date: Oct 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Question VBA to VBS conversion error

Hi All

I hope sommeone might be able to assist in resolving the following problem.

I stumbled accross a VBA macro that is exactly what I need to do, but in VBScript. I have begun converting it to VBScript but I am getting a "Type Mismatch" error from:
astrHeadings = objWord.ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)
I have read a couple of things on forums and the MSDN website, the forum threads were too confusing and seemed to be indirectly related. On the MSDN website there was something about two security patches for activex that might of helped, if it was for me using the script from a usb thumb drive on different computers which might not all have these patches installed. It also demonstrates a couple of work arounds, which I have tried without success.

The code I have opens a test word document named test-doc.docx which has 10 headings using MSWords default styles (Heading 1 to Heading 5) in random order. It should then duplicate the headings found and dump them into a new word document. The following code is what I have so far.

Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objWord
Set objWord = CreateObject("Word.Application")
objWord.Visible = True

Dim docOutline 
Dim docSource 
Dim rng

Const wdRefTypeHeading = 1
Dim astrHeadings
Dim strText 
Dim intLevel 
Dim intItem 
Set docOutline = objWord.Documents.Add 
Set objSelection = objWord.Selection
   objSelection.ParagraphFormat.TabStops.Add objWord.InchesToPoints(1.0), wdAlignTabLeft
   objSelection.ParagraphFormat.TabStops.Add objWord.InchesToPoints(6.0), wdAlignTabRight, wdTabLeaderDots
Set rng = docOutline.Range
   ' Content returns only the 
    ' main body of the document, not 
    ' the headers and footer. 
    Set rng = docOutline.Content 
    Set docSource = objWord.Documents.Open ("C:\Users\Owner\Desktop\Robbbbbb\test-doc.docx", 0, False)
    astrHeadings = objWord.ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)
    'MsgBox astrHeadings
    For intItem = 1 To UBound(astrHeadings) 
        strText = Trim((astrHeadings(intItem))) 
        intLevel = GetLevel(CStr(astrHeadings(intItem))) 
        ' Add the text to the document. 
        rng.InsertAfter strText & vbNewLine 
        ' Set the style of the selected range and 
        ' then collapse the range for the next entry. 
        rng.Style = "Heading " & intLevel 
        rng.Collapse(0) 'wdCollapseEnd 

Private Function GetLevel(strItem) 
    ' Return the heading level of a header from the 
    ' array returned by Word. 
    ' The number of leading spaces indicates the 
    ' outline level (2 spaces per level: H1 has 
    ' 0 spaces, H2 has 2 spaces, H3 has 4 spaces. 
    Dim strTemp
    Dim strOriginal 
    Dim intDiff 
    ' Get rid of all trailing spaces. 
    strOriginal = RTrim(strItem) 
    ' Trim leading spaces, and then compare with 
    ' the original. 
    strTemp = LTrim(strOriginal) 
    ' Subtract to find the number of 
    ' leading spaces in the original string. 
    intDiff = Len(strOriginal) - Len(strTemp) 
    GetLevel = (intDiff / 2) + 1 
End Function
From one post that I read astrHeadings should be declared as type variant, but VBScript variables are of type variant by default, so this made little sense to me. The code in question should return an Array so I tried dumping the crossreference items into an array without success.

Any help in resolving this problem is appreciated.

Kind regards

A very confused individual (Nimbus3)
Reply With Quote
  #2 (permalink)  
New Member
Join Date: Oct 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Just a follow up to my thread.

astrHeadings becomes varType 8200 (Array + String) from the collection getcrossreference. When it needs to be vetType 8204 (Array + Variant). I have found out that this is the cause of the problem, but I am no closer to finding a resolution.

My way of thinking is that if I can catch the collection and make it a standard/multidimensional array or some other collection it would be easier to work with, but I am unsure of how to do this.

1. Is my way of thinking correct
2. Is this possible in VBScript
3. If it is not then there must be an alternative way of achieving the same function.

Could someone advise me or even better point me in the right direction please.

A little less confused (Nimbus3)
Reply With Quote


getcrossreferenceitems, vba, vba to vbs, vbs, wdreftypeheading

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Syntax error. operation error jayhighway Visual Basic 3 06-29-13 11:40 PM
C++ and MSSQL tutorials? scott2500uk C/C++ 8 05-11-09 02:33 AM
[SOLVED] 500 Internal Server Error - Please help Dawn Perl 15 07-08-08 11:08 AM
Syntax Error Nikas Database 4 05-15-08 10:48 AM
ASP upload prob minority ASP 1 06-27-05 08:35 AM

All times are GMT -5. The time now is 06:16 AM.
vBulletin® Copyright ©2000 - 2015, Jelsoft Enterprises Ltd.