Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Other Office > Word VBA
|
Word VBA Discuss using VBA to program Word.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Word VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 16th, 2009, 03:54 PM
Registered User
 
Join Date: Dec 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Create error msg based on calculation result in formfield using VBA

I have a protected Word 03 form with a 2 column table in it (A1:B8).
In the table are 3 manual entry textbox fields that you can enter
dollar amounts into (B1, B2 & B3), and the remaining 5 fields (B4, B5,
B6, B7 & B8) have formula calculations. B4 is the sum of B2+B3. B5 is
a percentage ratio of B1/B4.

Basically what I'm trying to do is pop up an error message when B4
(the sum of B2+B3) is less than the dollar amount entered into B1. Or
another way of writing it is if B5 is greater than 100%, I need the
error message to pop up. Sometimes my code works, sometimes not, and
I've written many different ways, spending many different days (&
nights), but to no avail. I've tried declaring the variables as
currency, integer & string, but I'm just missing something with my
programming inexperience. Any help would be greatly appreciated!

Sub Main()
Dim oFld As FormFields
Dim strNum As String
Set oFld = ActiveDocument.FormFields
strNum = oFld("Text36").Result
strNum = oFld("Text39").Result

If oFld ("Text36").Result < oFld("Text39").Result Then
MsgBox "CELL B1 EXCEEDS CELL B4" & vbCrLf & "Use Alternate
Calculation"
End If
End Sub

Many thanks!
 
Old December 16th, 2009, 10:17 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

Can you try something like this:

Code:
Sub Main()
Dim oFld As FormFields
Dim dNum1 As Double
Dim dNum2 As Double
Set oFld = ActiveDocument.FormFields
dNum1 = Val(oFld("Text1").Result)
dNum2 = Val(oFld("Text2").Result)
If dNum1 < dNum2 Then
MsgBox "CELL B1 EXCEEDS CELL B4" & vbCrLf & "Use Alternate"
'Calculation ""
End If
End Sub
Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips &amp; Tricks (http://www.vbadud.blogspot.com)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Word 2007 VBA validation of formfield beth.harless Word VBA 0 August 18th, 2009 02:24 PM
error msg aspsuraj BOOK: Wrox's ASP.NET 2.0 Visual Web Developer 2005 Express Edition Starter ISBN: 978-0-7645-8807-5 1 November 27th, 2006 08:30 PM
VBA cannot create MS Word Object- ActiveX Error Dolphin Access VBA 0 February 7th, 2005 01:03 AM
Field Calculation Upgrade to VBA from Access Basic Pavesa Access VBA 2 December 23rd, 2003 12:24 PM
Query based on date calculation? skinny Access 5 September 30th, 2003 03:20 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.