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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting from Word 97 to Word 2002

Status
Not open for further replies.

pcsmurf

IS-IT--Management
Apr 26, 2002
47
0
0
GB
The following code is a snippet of code used in a macro that we successfully run under Word 97. The purpose of this part of the macro is to take the text from each cell and write it into a text file (which becomes an HTML file eventually). However running this through Word 2002 I get the following error message:

Run-time error 5: Invalid procedure call or argument.

This occurs on the line : If Asc(am$) = 13) then
and the value of am$ for some reason is set to "" instead of the value of the cell.

The script seems to run correctly for the first cell but then the problem occurs when it tries to read the second cell. Any ideas?

Code:
For i = 1 To NoRows
Print #1, "<tr>"
j = 1
For m = 1 To WordBasic.SelInfo(18)
WordBasic.WW7_EditGoTo Destination:="\Cell"
WordBasic.CharLeft 1, 1
am$ = WordBasic.[Selection$]()
If (Asc(am$) = 13) Then
If (Len(am$) < 3) Then
am$ = " "
End If
End If
WordBasic.CharRight 1, 1
WordBasic.CurValues.TableColumnWidth Dlg
SpBr = WordBasic.Val(Dlg.ColumnWidth)
k = 1
SpBr = SpBr - Width___(j)
j = j + 1
While ((Abs(SpBr) > 0.5))
SpBr = SpBr - Width___(j)
k = k + 1
j = j + 1
Wend
If k > 1 Then
DStart$ = "<" + TD$ + " colspan=" + WordBasic.[LTrim$](Str(k))
Else
DStart$ = "<" + TD$
End If
WordBasic.CurValues.FormatParagraph Dlg1
Select Case Dlg1.Alignment
Case 1: DStart$ = DStart$ + " Align=CENTER>"
Case 2: DStart$ = DStart$ + " Align=RIGHT>"
Case 3: DStart$ = DStart$ + " Align=JUSTIFY>"
Case Else: DStart$ = DStart$ + ">"
End Select
If am$ = " " Then
Print #1, DStart$ + "&nbsp;</" + TD$ + ">"
Else
b$ = ""
a$ = am$
For n = 1 To Len(a$)
If Mid(a$, n, 1) = Chr(13) Then
b$ = b$ + "<br>"
Else
b$ = b$ + Mid(a$, n, 1)
End If
Next n
Print #1, DStart$ + b$;
Print #1, "</" + TD$ + ">"
End If
If m < WordBasic.SelInfo(18) Then WordBasic.NextCell
Next m
WordBasic.TableSelectRow
WordBasic.CharRight 1, 0
Print #1, "</tr>"
TD$ = "td"
Next i
 
I see no one has answered here, the reason for that might be that the code is from versions prior to 97, it's Word Basic, converted to VBA.

In the few projects where I've worked with such, in stead of trying to (understand and) use the "old code", which also showed similar signs of not working entirely correct in later versions, I converted the stuff to VBA.

I don't know much of these methods by sight, but in the Word VBA Help files, there's a topic "Converting WordBasic Macros to Visual Basic", with an overwiev of the "automatic conversion", and the topic "Visual Basic Equivalents for WordBasic Commands", which will list the VBA equivalent of every WordBasic command.

I think I'll recommend going that path, to make the code easier to maintain with current versions.

In this case, I believe you are correct, it bombs because the variable am$ (which in newer versions would perhaps have been declared with Dim strAm as String) contains an empty string (not carriage return, as is tested for). But if this is because the code works differently on 2002, or if it's new usage of the template, is hard to say. You could perhaps test if anything is present in the variable, prior to testing for carriage return (len(am$)>0).

Perhaps there are others being more fluent in WordBasic, and can help you addressing the challenge with WordBasic alterations, but I think I'll recommend conversion.

Roy-Vidar
 
This following gets the text out of each cell in Table 1.
Code:
Sub GetCellText()
Dim aCell As Cell
Dim ThisCellText As String

For Each aCell In ActiveDocument.Tables(1).Range.Cells
    ThisCellText = Left(aCell.Range.Text, Len(aCell.Range.Text) - 1)
    MsgBox ThisCellText
Next
End Sub

In this case of course it simply displays each cell content in a message box. The string dumps the last character, which is the cell/paragraph separator. This is actually a unique "double" character of Chr(7) and Chr(13). From VBA perspective however, you can strip it off as 1 character.

The order is by row, then column.

I did not quite get if you wanted EACH cell content to go into separate text files, so did not include that. Also, if you needed to go through all the tables in a document, you would have to loop through the Tables collection.

Gerry
 
Thanks for both your replies. I am hoping to convert all our macros in due course but I need to get them all working as quickly as possible. Most of the ones we use run as is.

I'll try out your suggestions and hopefully I'll end up with a working macro.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top