Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2006

Tip: Looking for answers? Try searching our database.

Arithmetic in Visual Basic

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ben K. Bullock - 18 Feb 2006 06:25 GMT
I know next to nothing about Visual Basic, but due to the extreme tedium and
frequent mistakes in typing in very repetitive sequences of
invoices/estimates/delivery notes involving the same numbers in several
places, I made the following function to make the documents automatically
using fields in a Word template. Basically I grabbed some code off the web
somewhere and messed around with it until it did more or less what I wanted.
There is also a pop up box where I can type in the numbers, and it is set to
open automatically when the template is clicked for a new document.

This works very well as far as it goes, and saves me several minutes of
fiddling and possible typing errors or forgetting to change numbers when
typing the complicated multipage repetitive invoices. However, in the
following, NoTaxCost should be UnitCost multiplied by NoOfPages, and
TotalCost should be that multiplied by 1.05 for the sales tax. So to be even
more lazy I would like this to calculate automatically for me and only have
to input two numbers, the unit cost and the number of pages I'm invoicing
for. It sounds simple enough, and I tried some things, as commented out
below, but they didn't work. What should I be doing?

Thanks for any assistance.

B. Bullock.

>>>>>>>>>>>>>>>>>>>>>>>>>> Begin function

Private Sub CommandButton1_Click()

Dim UC As Integer

UC = Val(UnitCost.Text)
'NoTaxCost As Integer
'TaxCost As Integer
'TotalCost As Integer

'UnitCost := 2500

'NoTaxCost = UnitCost * NoOfPages
'TaxCost = NoTaxCost * 0.05
'TotalCost = NoTaxCost + TaxCost

With ActiveDocument
   .Bookmarks("TaxCost").Range _
   .InsertBefore TaxCost
   .Bookmarks("CompanyName").Range _
   .InsertBefore CompanyName
   .Bookmarks("NoOfPages").Range _
   .InsertBefore NoOfPages
   .Bookmarks("NoTaxCost").Range _
   .InsertBefore NoTaxCost
   .Bookmarks("TotalCost").Range _
   .InsertBefore TotalCost
   .Bookmarks("UnitCost").Range _
   .InsertBefore UnitCost
End With
'select all text

  Selection.WholeStory

  'update fields

  Selection.Fields.Update

  'move cursor to deselect

  Selection.MoveUp Unit:=wdLine, Count:=1
UserForm1.Hide
End Sub

Private Sub TotalCost_Change()

End Sub

Private Sub UserForm_Click()

End Sub

>>>>>>>>>>>>>>>>>>>> end
Jezebel - 18 Feb 2006 06:33 GMT
This line leaps out --

'UnitCost := 2500

You use colons in assignments only in function arguments, not in ordinary
code.

UnitCode = 2500

Also, the one declaration you have is Integer, which you certainly don't
want for currencies. (Integers have no decimal places.) Use

Dim UC as currency

and declare all your other variables also. Make it a rule always to include
Option Explicit at the top of each module. This is general good practice,
and it's CRITICAL in code that deals with money.

>I know next to nothing about Visual Basic, but due to the extreme tedium
>and frequent mistakes in typing in very repetitive sequences of
[quoted text clipped - 74 lines]
>
>>>>>>>>>>>>>>>>>>>>> end
Ben K. Bullock - 18 Feb 2006 06:55 GMT
> This line leaps out --
>
[quoted text clipped - 4 lines]
>
> UnitCode = 2500

Thank you. I changed the things as you suggest and also put "Dim" there, and
now the thing is doing what I wanted it to. Problem solved!

> Also, the one declaration you have is Integer, which you certainly don't
> want for currencies. (Integers have no decimal places.) Use
[quoted text clipped - 4 lines]
> include Option Explicit at the top of each module. This is general good
> practice, and it's CRITICAL in code that deals with money.

Thanks, I'll bear that in mind for future projects.

Thanks again!

B. Bullock.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.