Solved

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

Posted on 2016-09-05
6
30 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
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) …
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now