
Signature
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
> One more question. I did make that macro work. I had to add a little
> more
[quoted text clipped - 87 lines]
>> > >> >> > [after merge is done]
>> > >> >> > TotalDollarAmount
Erg, it's not working. It's still seeing the commas from the field in the
datasource, and somehow not adding the numbers correctly. I got one of the
columns working fine, but it doesn't contain any commas or dollar signs.
I can't remove the symbols from the the datasource, because the system we
use to export this data can't be changed easily.
If there's any other suggestions you have please let me know. Otherwise I
will just keep playing with this. Again, there are 2 columns of numbers,
column 6 and column 13, that need to be totalled after the merge, and the
totals need to show in under the last row. I've got the macro doing both
calculations, but the column 6 calculation isn't working right because, I
believe, there are commas and dollar signs in the column data.
> Move the .Start of the Range so that it excludes the $ sign
>
[quoted text clipped - 91 lines]
> >> > >> >> > [after merge is done]
> >> > >> >> > TotalDollarAmount
Peter Jamieson - 30 Jun 2006 16:11 GMT
You /may/ be able to do this by issuing a Word VBA OpenDataSource call that
opens the file using ODBC. This lets you use some of the VBA functions
available in the Jet dialect of SQL. e.g.
Sub OpenCSVviaODBC()
Dim strPathOnly As String
Dim strFileOnly As String
Dim strConnection As String
Dim strQuery As String
' Set the path to your file
strPathOnly = "C:\mypath\"
strFileOnly = "myfielcsv"
strConnection = "DSN=Delimited Text Files;DBQ=" & _
strPathOnly & _
";DriverId=27;FIL=text;MaxBufferSize=2048;PageTimeout=5;"
strQuery = "SELECT *, cdbl(mid(mynumber,2)) AS 'mynewnumber' FROM `" &
strFileOnly & "`"
ActiveDocument.MailMerge.MainDocumentType = wdNotAMergeDocument
ActiveDocument.MailMerge.MainDocumentType = wdDirectory
ActiveDocument.MailMerge.OpenDataSource _
Name:="", _
Connection:=strConnection, _
SQLStatement:=strQuery, _
SubType:=wdMergeSubTypeWord2000
End Sub
However, it is sometimes hard to get this stuff to work.
Peter Jamieson
> Erg, it's not working. It's still seeing the commas from the field in the
> datasource, and somehow not adding the numbers correctly. I got one of
[quoted text clipped - 116 lines]
>> >> > >> >> > [after merge is done]
>> >> > >> >> > TotalDollarAmount
Roy Carlson - 30 Jun 2006 17:02 GMT
Isn't there a way to tell this macro that the format of the data its adding
is currency?
Sub TotalGifts()
Dim i As Long
Dim TotalDollars As Double
Dim atable As Table
Dim currange As Range
Dim newrow As Row
Set atable = ActiveDocument.Tables(1)
TotalDollars = FormatCurrency(0)
For i = 1 To atable.Rows.Count
Set currange = atable.Cell(i, 6).Range
currange.End = currange.End - 1
TotalDollars = TotalDollars + Val(currange)
Set newrow = atable.Rows.Add
newrow.Cells(4).Range.InsertAfter "Total"
newrow.Cells(6).Range.InsertAfter Format(TotalDollars, "$#,###.00")
End Sub
I'm assuming that something could be done to:
Set currange = atable.Cell(i, 6).Range
currange.End = currange.End - 1
TotalDollars = TotalDollars + Val(currange)
to setup a format in the calculation, but I don't know what that would be.
> You /may/ be able to do this by issuing a Word VBA OpenDataSource call that
> opens the file using ODBC. This lets you use some of the VBA functions
[quoted text clipped - 146 lines]
> >> >> > >> >> > [after merge is done]
> >> >> > >> >> > TotalDollarAmount
Doug Robbins - Word MVP - 30 Jun 2006 18:59 GMT
The following uses the Replace() function to delete the commas from the
data. I have an idea though that it was only introduced with Word XP so you
may need to be using that version or later:
Dim i As Long
Dim TotalDollars As Double
Dim atable As Table
Dim currange As Range
Dim newrow As Row
Set atable = ActiveDocument.Tables(1)
TotalDollars = 0
For i = 1 To atable.Rows.Count
Set currange = atable.Cell(i, 2).Range
currange.End = currange.End - 1
currange.Start = currange.Start + 1
TotalDollars = TotalDollars + Val(Replace(currange.Text, ",", ""))
Next i
Set newrow = atable.Rows.Add
newrow.Cells(1).Range.InsertAfter "Total"
newrow.Cells(2).Range.InsertAfter Format(TotalDollars, "$#,###.00")

Signature
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
> Isn't there a way to tell this macro that the format of the data its
> adding
[quoted text clipped - 202 lines]
>> >> >> > >> >> > [after merge is done]
>> >> >> > >> >> > TotalDollarAmount
Roy Carlson - 30 Jun 2006 20:18 GMT
That did it! Thanks
> The following uses the Replace() function to delete the commas from the
> data. I have an idea though that it was only introduced with Word XP so you
[quoted text clipped - 217 lines]
> >> >> >> > >> >> > merge
> >> >> >> > >> >> > data? For example: