![]() |
MS-DEFCON 3:
Patch reliability is unclear, but widespread attacks make patching prudent. Go ahead and patch, but watch out for potential problems.
|
-
Calculate string expression – Word2003 VBA
Home › Forums › AskWoody support › Productivity software by function › Visual Basic for Applications › Calculate string expression – Word2003 VBA
Tagged: VBA evaluate string expression
- This topic has 11 replies, 3 voices, and was last updated 2 weeks, 1 day ago.
Viewing 5 reply threads-
AuthorPosts
-
-
March 23, 2021 at 1:07 pm #2352565
Chris Greaves
AskWoody PlusSub test()
Dim sng As Single
Dim strExpression As String
strExpression = “60+144”
‘ Dim strVar As Variant
‘ strVar = strExpression
‘ sng = evaluate(strVar)
sng = CSng(strExpression)
End SubI have been struggling off and on throughout the day to resolve the arithmetic value of a presented string.
I am given an x,y co-ordinate as a line of a text file:-
ORIGIN 60+144,111
and I want to obtain the horizontal co-ordinate as 204. For input into a Shapes.AddLine statement, since you ask.
Excel has an Evaluate function – sng = Evaluate(strExpression) – which works very well in Excel2003.
But I am staggered that I cannot perform a straight arithmetic calculation from a string in Word2003 VBA.
I have previously used =Formula to insert a crafted SUM function in a Word2003 Table, but would appreciate any tips on how to do simple arithmetic from a string expression within VBA.
The application is basically simulating a flatbed plotter, and moves the pen (raised or lowered) between pairs of points. If a room is two rooms down and the rooms are sixty inches and one hundred forty four inches, it is convenient for the user to transcribe this data as “ORIGIN 60+144,111” rather than trust to the user’s mental arithmetic.
Besides which, computers are good at doing boring and repetitive work.
Thank you
Chris Greaves
Unless you're in a hurry, just wait.
-
March 23, 2021 at 2:13 pm #2352578
Kirsty
ManagerI suspect you might be asking Word to do something that only Excel is designed to do. These references may be of some assistance:
https://docs.microsoft.com/en-us/office/vba/word/concepts/miscellaneous/concepts-word-vba-reference
https://docs.microsoft.com/en-us/office/vba/library-reference/concepts/getting-started-with-vba-in-officeWould it be possible to embed an Excel spreadsheet into the Word Document, rather than trying to use a table?
-
March 24, 2021 at 1:02 am #2352622
anonymous
GuestI don’t think that you can evaluate “60+144” to get 204.
Instead, extract out the digits as numbers then evaluate.It unclear to me why you want to convert from double to single precision in the last line.
This code works in Word 2019, not tested in Word 2003
Sub test()
Dim sng As Single
Dim strExpression As String
Dim x1, x2 As Double
strExpression = “60 + 144”
x1 = CDbl(Left(strExpression, InStr(strExpression, “+”) – 1))
x2 = CDbl(Right(strExpression, Len(strExpression) – InStr(strExpression, “+”)))
sng = CSng(x1 + x2)
End Sub1 user thanked author for this post.
-
March 24, 2021 at 6:53 am #2352687
Chris Greaves
AskWoody PlusI suspect you might be asking Word to do something that only Excel is designed to do. These references may be of some assistance:
https://docs.microsoft.com/en-us/office/vba/word/concepts/miscellaneous/concepts-word-vba-reference
https://docs.microsoft.com/en-us/office/vba/library-reference/concepts/getting-started-with-vba-in-officeWould it be possible to embed an Excel spreadsheet into the Word Document, rather than trying to use a table?
Thanks for the response, Kirsty.
I think that the two references are a bit too generic for me. I really thought I knew Word/VBA after 20+ years, but yesterday was surprised to find no example of this sort of calculation in my 900-function library UW.dot nor in my 37MB searchable library of VBA code. That made me scratch my head. On top of that I used to work in compiler development and still can’t believe I haven’t written a parser to evaluate arithmetic expressions!Would it be possible to embed an Excel spreadsheet into the Word Document, rather than trying to use a table?
Yes it would, except that I am not using a table. The user walks around the house with a steel tape measure and records the lengths of each wall segment. Then the user keys that data into a text file. Where two rooms run along the other side of the wall and the lengths of the rooms are, say, 60 inches and 144 inches it makes more sense for the user to key in the data as “60+144” rather than get the mental arithmetic wrong.
Besides which, if we can’t use computers to do arithmetic, then why are we using them? (grin).
In the main I was staggered that visual BASIC couldn’t evaluate a simple expression for me!
More in my next response…
Thanks
Chris
Unless you're in a hurry, just wait.
-
March 24, 2021 at 8:21 am #2352697
Chris Greaves
AskWoody PlusI don’t think that you can evaluate “60+144” to get 204.
Instead, extract out the digits as numbers then evaluate.It unclear to me why you want to convert from double to single precision in the last line.
This code works in Word 2019, not tested in Word 2003
Sub test()
Dim sng As Single
Dim strExpression As String
Dim x1, x2 As Double
strExpression = “60 + 144”
x1 = CDbl(Left(strExpression, InStr(strExpression, “+”) – 1))
x2 = CDbl(Right(strExpression, Len(strExpression) – InStr(strExpression, “+”)))
sng = CSng(x1 + x2)
End Sub“It unclear to me why you want to convert from double to single precision in the last line.”
Hi, and thanks for the response.
I am coercing to single because the Word2003/VBA .AddShapes requires single precision as parameters – and I was trying to follow the rules (for once!)
“This code works in Word 2019, not tested in Word 2003”
And thank you too for this; I had not explored CDbl in all my years. I have your code working in Word2003/VBA and shall write a small parser for myself that deals with addition and subtraction only for now; this has got to be shipped out of the door.
Cheers
Chris
Unless you're in a hurry, just wait.
-
March 24, 2021 at 8:22 am #2352698
doriel
AskWoody LoungerSorry for posting as picture, but spam filter does not like my IF clause repetition.
Its evaluated as spam, thats why I posted the picture of my code. It definatelly needs some debugging, but you can get the idea.Dell Latitude E6530, Intel Core i5 @ 2.6 GHz, 4GB RAM, W10 1809 Enterprise
HAL3000, AMD Athlon 200GE @ 3,4 GHz, 8GB RAM, Fedora 29
Attachments:
You must be logged in to access attached files.
-
March 24, 2021 at 8:30 am #2352701
-
March 24, 2021 at 11:35 am #2352722
Chris Greaves
AskWoody PlusHi Doriel, Your reply tried to cross with mine; mine failed:-Apparently too many of my posts are considered spam! So I have attached some code withouyt any commentary. Let’s see if this gets through.
Cheers
Chris
Unless you're in a hurry, just wait.
Attachments:
You must be logged in to access attached files.
1 user thanked author for this post.
-
March 24, 2021 at 1:03 pm #2352748
doriel
AskWoody LoungerExactly the same issue as mine. Maybe we could start a topic to ask if other contribbutors face the same problems. Spam filter seems to be set very aggressively. I have also problem posting links.
Dell Latitude E6530, Intel Core i5 @ 2.6 GHz, 4GB RAM, W10 1809 Enterprise
HAL3000, AMD Athlon 200GE @ 3,4 GHz, 8GB RAM, Fedora 29
-
March 24, 2021 at 2:36 pm #2352756
Kirsty
ManagerPosting code has already been addressed – you may find these tips make your attempts to post more successful:
https://www.askwoody.com/forums/topic/the-code-feature-is-practically-useless/#post-104645You are also less likely to have problems using the Text tab in the text editor (instead of the Visual tab) as it is promoted as better for posting code.
As to the number of links in one post, that is a spam-filter setting, designed to send posts for moderation (rather than directly to spam) if the links themselves aren’t deemed potentially dangerous, where there are more than a set number (information limited due to spam activities).
1 user thanked author for this post.
-
March 24, 2021 at 2:55 pm #2352764
Chris Greaves
AskWoody Plus“You are also less likely to have problems using the Text tab in the text editor (instead of the Visual tab) as it is promoted as better for posting code.”
Hi Kirsty.
Are you suggesting that we NOT type the body of a reply in Visual mode, but only in Text mode?
If so, please and thank you, what is the purpose of the Visual tab?
Thanks
Chris
Unless you're in a hurry, just wait.
-
March 26, 2021 at 2:26 am #2353015
doriel
AskWoody LoungerThe topic is closed already.
I use the text editor from the first time I went to askwoody, never visual.
I prefer to see whats happening with my text.
Neverteless, I have also problems posting links. Maybe little patience could be useful for me, but this happens A LOT recently. I have to wait like 30 mnutes before it works again. Also my posts are evaluated as spam. Does your AI does not like my posts or what? 🙂Dell Latitude E6530, Intel Core i5 @ 2.6 GHz, 4GB RAM, W10 1809 Enterprise
HAL3000, AMD Athlon 200GE @ 3,4 GHz, 8GB RAM, Fedora 29
-
-
March 24, 2021 at 2:54 pm #2352761
Chris Greaves
AskWoody Plus“Exactly the same issue as mine. Maybe we could start a topic to ask if other contributors face the same problems. Spam filter seems to be set very aggressively. I have also problem posting links.”
Hi Doriel. It’s not the first time for me, but it has been one of the reasons I have been reluctant to post here – spending far too much time trying to get something to actually GO. This time around the spam message indicated something along the lines of “too many posts in too short a time”, but Tom and Fergus dropped in for coffee and dog-biscuits while I was composing that reply to your posts,, so we are talking at least 60 minutes between posts –Â and that is spam?
On another board there is a 60-second repeat rate, and I sometimes fall afoul of that, because some answers require a short acknowledgement, but 60 minutes seems a bit much IMNSHO.
Cheers,
“Let’s see if this works” of Bonavista.
Unless you're in a hurry, just wait.
-
-
-
-
-
AuthorPosts
Viewing 5 reply threads -
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments. Click here for details and to sign up.
Search Newsletters
Search Forums
Recent Replies
Susan Bradley on 20H2 and Adobe Flash Player
12 minutes agoSusan Bradley on Very slow and erratic data transfer to and from NAS used for backup
18 minutes agoanonymous on Very slow and erratic data transfer to and from NAS used for backup
19 minutes agoWCHS on 20H2 and Adobe Flash Player
42 minutes agoHamsa Vicerra on How can I locate Bitlocker key in OEM refurb HP laptop?
52 minutes agoHamsa Vicerra on How can I locate Bitlocker key in OEM refurb HP laptop?
56 minutes agoSlowpoke47 on Editing a PDF in Mint
1 hour, 18 minutes agoHamsa Vicerra on How can I locate Bitlocker key in OEM refurb HP laptop?
1 hour, 23 minutes agoPKCano on Grandma, what big updates you have!
1 hour, 26 minutes agob on Outlook cannot read the calendar
2 hours, 14 minutes agoanonymous on Outlook cannot read the calendar
2 hours, 25 minutes agoanonymous on Standalone installer script for Windows 7 ESU, regardless the license
3 hours, 11 minutes agoanonymous on How much RAM does your computer have?
3 hours, 13 minutes agoanonymous on 20H2 and Adobe Flash Player
3 hours, 41 minutes agoLHiggins on Question about allowing/stopping laptop from turning off USB device
4 hours, 15 minutes agoOscarCP on Good animated movies and shows for ages ten to one hundred and ten.
4 hours, 46 minutes agoMrGreen365 on Importing multiple XML files into Excel
4 hours, 48 minutes agoCWBillow on Local or Microsoft account?
4 hours, 51 minutes agoOscarCP on Good animated movies and shows for ages ten to one hundred and ten.
5 hours, 8 minutes agoAlex5723 on Office 2010 Installer “Wanted”
5 hours, 10 minutes agoBobT on Inside tech support scams
5 hours, 39 minutes agoAlex5723 on Fickle File Explorer Column Headings
6 hours, 5 minutes agoEP on 20H2 and Adobe Flash Player
6 hours, 22 minutes agoEP on 20H2 and Adobe Flash Player
6 hours, 24 minutes agoEP on Office 2010 Installer “Wanted”
6 hours, 30 minutes agoEP on How much RAM does your computer have?
6 hours, 37 minutes agoWCHS on 20H2 and Adobe Flash Player
6 hours, 39 minutes agoEP on Removal of Chromium Edge in 20H2?
6 hours, 41 minutes agoEP on 20H2 and Adobe Flash Player
6 hours, 44 minutes agoWCHS on 20H2 and Adobe Flash Player
6 hours, 48 minutes ago
Recent Topics
-
Grandma, what big updates you have!
1 hour, 26 minutes ago
-
Mapping a drive
4 hours, 43 minutes ago
-
vssvc?
6 hours, 21 minutes ago
-
Inside tech support scams
5 hours, 39 minutes ago
-
Hackers hacked Swarmshop stolen credit cards database
16 hours, 3 minutes ago
-
DuckDuckGo updates its plugin to block Google’s creepy FLoC
8 hours, 5 minutes ago
-
Initial Apple M1 SoC Support Aims For Linux 5.13 Kernel
15 hours, 29 minutes ago
-
How much RAM does your computer have?
3 hours, 14 minutes ago
-
odd optional update
22 hours, 56 minutes ago
-
Editing a PDF in Mint
1 hour, 18 minutes ago
-
20H2 and and OOB optional March 18 printer problem update
19 hours, 53 minutes ago
-
20H2 and 2020-02 CU for .NET
21 hours, 24 minutes ago
-
20H2 and Adobe Flash Player
13 minutes ago
-
How to set MLB homepage in Edge Chromium
1 day, 5 hours ago
-
Scraped data of 500 million LinkedIn users being sold online
1 day, 6 hours ago
-
Question about allowing/stopping laptop from turning off USB device
4 hours, 16 minutes ago
-
Office 2010 Installer “Wanted”
5 hours, 10 minutes ago
-
Subscribed topics
17 hours, 29 minutes ago
-
New age olympics – hacking contest
1 day, 1 hour ago
-
Questions about installing Brave on Linux Mint
23 hours, 35 minutes ago
-
Testing if I could post a thread
7 hours, 18 minutes ago
-
File Explorer shortcut 30 – 45 seconds to open a window
1 day, 2 hours ago
-
Basic Asset Management Software
1 day, 9 hours ago
-
My first OEM Linux PC (laptop)!
1 day, 3 hours ago
-
Are you a Consultant or IT Pro?
10 hours, 20 minutes ago
-
Removal of Chromium Edge in 20H2?
6 hours, 41 minutes ago
-
How determine what Seach Engine is being used?
2 days, 18 hours ago
-
Outlook cannot read the calendar
2 hours, 15 minutes ago
-
Windows 10 Insider Preview build 21354 released to DEV Channel
3 days, 4 hours ago
-
Printer Offline After Every Reboot
17 hours, 27 minutes ago
Search for Topics
Recent blog posts
- Inside tech support scams
- How much RAM does your computer have?
- Are you a Consultant or IT Pro?
- Windows Update for Business isn’t just
- April 2021 Office non-Security Updates are now available
- Touchscreen misregisters point-and-clicks
- AskWoody Improvements
- The best laptop docking stations and hubs for 2021
Key Links
Copyright © 2004 – 2021 AskWoody Tech LLC. All rights reserved.