Solved

VB script Microsoft Word Tables.  Removing all markup from numeric value in a cell

Posted on 2016-09-05
6
38 Views
Last Modified: 2016-09-05
How do I remove the extra stuff in a Word Table cell (see Screenshot) so that only the numeric value is left.

Something like  Replace(tbl.cell(r,c).Range.Text,"stuff I don't want","")

Screenshot of msgbox showing extra mark up that is causing the following script to err because it can not convert the number value in the cell to a CDbl.  The values in the cells are numbers.  Somehow Word adds bullet points and line breaks

Screenshot:
http://www.screencast.com/t/tOY9cjLi

Script:
tbl.cell(row,2).Range.Text = AccountingFormat(FormatNumber( CDbl(replace(tbl.cell(row-2,c).Range.Text, "-","0")) - CDbl(Replace(tbl.cell(row-1,c).Range.Text, "-","0")) ,0))


Here is a sample of the code that is populating the Cell in the first place:
tbl.cell(newrow.index,2).range.text = AccountingFormat(split(http.responseText,"~")(1))

AccountingFormat function is just replacing 0 with a - symbol.
0
Comment
Question by:maxdbase
  • 3
  • 2
6 Comments
 

Author Comment

by:maxdbase
ID: 41785045
Even though the screen shot shows two numbers, there is only one number and the bullet point in each cell.  I msgbox'ed two separate cells.  The bullet point and line break seem to be what is causing the issue.  I just need the number without the excess mark up stuff.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 41785129
It isn't clear what your environment is.

You say VbScript, but AccountingFormat is an Excel function

In Word VBA, this seems to work with your example:
MyNumber = val(replace(mytable.Cell(1,1).Range.Text,",",""))

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 41785133
If that doesn't work, can you post an example in a Word document, rather than a picture, please?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:maxdbase
ID: 41785146
I am using a vbs vb script.
I created my own AccountingFormat() function in the vbs file
I shall try the val idea in the vbs vb script and see if that removes
the unwanted bullet point and carriage return that shows up in the msgbox I screenshotted.
0
 
LVL 31

Accepted Solution

by:
Helen_Feddema earned 500 total points
ID: 41785206
I have a procedure that I use to strip nonalpnanumeric characters from strings, for various purposes.  I made a version of it that strips nonnumeric characters; you could use this to replace the current contents of a cell with just the numbers:

Public Function StripNonNumericChars(strText As String) As String
'Strips a variety of non-alphanumeric characters from a text string
'Created by Helen Feddema 10-15-97
'Modified by Ruud H.G. van Tol 6-18-99
'Modified by Brad Beacham 6-Feb-2005
'Last modified by Helen Feddema 5-Sep-2016

On Error GoTo ErrorHandler

   Dim strTestString As String
   Dim strChar As String
   Dim i As Integer
   Dim strStripChars As String
   Dim lngChars As Long
   
   strTestString = strText

   For i = 1 To Len(strTestString)
      strChar = Nz(Mid(strTestString, i, 1))
      Debug.Print "Testing " & strChar
      
      If strChar = "" Then
         GoTo EndString
      ElseIf Asc(strChar) < 48 Or Asc(strChar) > 57 Then
         strTestString = Replace(strTestString, strChar, vbNullString)
         Debug.Print "Test string: " & strTestString
         i = i - 1
      End If
   Next i

EndString:
   StripNonNumericChars = strTestString
   
ErrorHandlerExit:
   Exit Function

ErrorHandler:
   MsgBox "Error No: " & Err.Number _
      & " in StripNonNumericChars procedure; " _
      & "Description: " & Err.Description
   Resume ErrorHandlerExit

End Function

Open in new window

0
 

Author Closing Comment

by:maxdbase
ID: 41785281
Great option.  I modified it to fit vb script and fine tuned it a bit due to the possibility of a - which needs to become a 0.  You got me heading a direction that meets the requirements.  And, I like how you started out with the function then kept improving it over time.  Appreciate the help!

Here's what I have so far, I'll likely keep adding to it.  I ultimately want to make some sort of plug in for Microsoft Word Tables similar to jquery that can traverse the table DOM.

For now, this is a great start!

Function StripNonNumericChars(strText)

   Dim strTestString
   
   strTestString = strText

   For i = 1 To Len(strTestString)
   
     
      strChar =Mid(strTestString, i, 1)
      if strChar = "-" then
      strChar = "0"
      end if
      if Asc(strChar) >= 48 and Asc(strChar) <= 57 Then
        StripNonNumericChars = StripNonNumericChars & strChar
      End If
   Next
   

End Function
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question