Change documents in word with VBA

C

Cookie

I have an existing VBA application that takes a flat file from and AS/400
appl and formats into a word document/PDF. There is a table created on the
cover page that I would like to copy to a new document. I have been able to
get the data I need into the table and create the additional document.
However, I have not been able to get the table into the new document. Please
help!
 
J

Jay Freedman

Cookie said:
I have an existing VBA application that takes a flat file from and
AS/400 appl and formats into a word document/PDF. There is a table
created on the cover page that I would like to copy to a new
document. I have been able to get the data I need into the table
and create the additional document. However, I have not been able to
get the table into the new document. Please help!

Dim sourceDoc As Document
Dim destinationDoc As Document

Set sourceDoc = ActiveDocument

' make sure there's at least one table
If sourceDoc.Tables.Count = 0 Then
Set sourceDoc = Nothing
Exit Sub
End If

' create a new document
Set destinationDoc = Documents.Add

' copy the first table from
' the source to the destination
destinationDoc.Range.FormattedText = _
sourceDoc.Tables(1).Range.FormattedText

' cleanup
Set sourceDoc = Nothing
Set destinationDoc = Nothing

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
C

Cookie

Jay, Thanks for the response. It helped some but I need to expand on my
problem.
The info I need to put in a seprate file is not exactly the same info that
is in the table on the cover page. The code below takes data from a listbox
that is created earlier in the program and depending if if it is selected or
not puts it into the table on the cover sheet. I need to keep this code so
I can still create the cover page and also create code that will put ALL the
data (not just selected) from the list box into a table in a new documentand
save it. It maybe possible to do it all in one procedure but that is over my
head at this point.
Thanks again,


Dim intSelectedRows As Integer

' Dim intColumnIndex As Integer
'
'
' intSelectedRows = 0
' intCounter = 0
' Do Until intCounter = ListBox1.ListCount
' If ListBox1.Selected(intCounter) = True Then
' intSelectedRows = intSelectedRows + 1
' End If
' intCounter = intCounter + 1
' Loop
' Set rng = .Range.Bookmarks("bmkCoverTable").Range
' .Tables.Add Range:=rng, NumRows:=intSelectedRows,
NumColumns:=ListBox1.ColumnCount, _
' DefaultTableBehavior:=wdWord9TableBehavior,
AutoFitBehavior:=wdAutoFitContent
' 'Now add the data to the table
' intCounter = 0 ' number of rows in ListBox1
' intCounter2 = 0 'number of rows in Table
' Do Until intCounter = ListBox1.ListCount
' If ListBox1.Selected(intCounter) = True Then 'this list row has
a check mark
' intCounter2 = intCounter2 + 1 'set table row number
' intColumnIndex = 0 'Listbox column number
' Do Until intColumnIndex + 1 > rng.Tables(1).Columns.Count
' If intColumnIndex = 0 Then 'Unique case for product
data titles
'
rng.Tables(1).Rows(intCounter2).Range.Cells(intColumnIndex + 1).Range.Text = _
' SetProductDataTitle(RTrim(ListBox1.List(intCounter,
intColumnIndex)))
' intColumnIndex = intColumnIndex + 1
' Else
'
rng.Tables(1).Rows(intCounter2).Range.Cells(intColumnIndex + 1).Range.Text = _
' RTrim(ListBox1.List(intCounter, intColumnIndex))
' intColumnIndex = intColumnIndex + 1
' End If
' Loop
' End If
' intCounter = intCounter + 1
' Loop
' 'this area creates the table on the page
' With rng.Tables(1)
' .TopPadding = InchesToPoints(0)
' .BottomPadding = InchesToPoints(0)
' .LeftPadding = InchesToPoints(0)
' .RightPadding = InchesToPoints(0)
' .Rows.Alignment = wdAlignRowCenter
' If intModelCount > 6 Then
' .Range.Paragraphs.Style =
ActiveDocument.Styles("ModelDetails_Small")
' .Rows(1).Range.Paragraphs.Style =
ActiveDocument.Styles("ModelDetails_Small Heading")
' .Columns(1).Select
' Selection.Style = ActiveDocument.Styles("ModelDetails_Small
Heading")
' Else
' .Range.Paragraphs.Style =
ActiveDocument.Styles("ModelDetails")
' .Rows(1).Range.Paragraphs.Style =
ActiveDocument.Styles("ModelDetails Heading")
' .Columns(1).Select
' Selection.Style = ActiveDocument.Styles("ModelDetails
Heading")
' End If
' End With
' End With
'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top