Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word macro from VBA code

Status
Not open for further replies.

denmark42

Programmer
Oct 5, 2001
5
US
I am currently building an application that creates a word document as its target for data that is collected. This data must be put in tables on the form. I have found no clean way to navigate from one table to another. originally I tried using..

ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

in an attempt to exit the current table before creating the next table, this unfortunatly seems to do nothing. Then I tried...

Selection.InsertBreak Type:=wdColumnBreak

this was more successful as it did exit the current table, but it puts additional table cells (garbage) at the end of the document on an added page. Has anyone found a way to do this? I have tried getting the macro recorder in word to create the code without success.

Thank you,

Mark Needham
Sr. Software Developer
Orange County's United Way
 
I'm not sure I understand what you need to accomplish.

Are you creating several different tables in one Word document, based on several different corresponding external data sources?

If so, is your problem trying to create each successive table? If not, then is it trying to jump from table to table programmatically, after they've been created? Or both?

In either case, I think the following code might be helpful.


ActiveDocument.tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _
5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
Selection.tables(1).Select
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.TypeParagraph
ActiveDocument.tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _
5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
Selection.tables(1).Select
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.TypeParagraph
Selection.Find.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Count:=1, Name:=""
Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Count:=2, Name:=""
Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Count:=3, Name:=""
Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Name:=""
Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Count:=5, Name:=""
Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Count:=2, Name:=""
Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Count:=1, Name:=""
Selection.Find.ClearFormatting


Also to facilitate navigation from table to table, you could add bookmarks and use the GoTo method to go to each bookmark subsequently:

With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="TableTwo"
.DefaultSorting = wdSortByLocation
End With

Let me know if you have any further questions.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top