?
Solved

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

Posted on 2016-09-05
6
Medium Priority
?
50 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

762 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