Can someone please show me how I might set tabs in a word document in VBA so that fields in an incoming datasource will always begin in the correct column? I'm using InsertAfter to add rows to the "report" as shown below but depending on the amount of data in the fields and the size of the font, the data doesn't always line up under the headings as planned. The headings are stored in a Word document and then Access comes along with VBA to fill in the columns of data. Set rgeDoc = wrdDoc.Range With rgeDoc .InsertAfter strNetwork .InsertAfter vbTab .InsertAfter strVault .InsertAfter vbTab & vbTab .InsertAfter strCSSName .InsertAfter vbTab .InsertAfter strCSSAddress .InsertAfter vbTab .InsertAfter strAccountNumber .InsertAfter Chr$(13) End With Thanks in advance, -- Glenn
Glenn Suggs was telling us: Glenn Suggs nous racontait que : > Can someone please show me how I might set tabs in a word document in > VBA so that fields in an incoming datasource will always begin in the > correct column? I'm using InsertAfter to add rows to the "report" as > shown below but depending on the amount of data in the fields and the > size of the font, the data doesn't always line up under the headings > as planned. The headings are stored in a Word document and then > Access comes along with VBA to fill in the columns of data. > > Set rgeDoc = wrdDoc.Range > With rgeDoc > .InsertAfter strNetwork > .InsertAfter vbTab > .InsertAfter strVault > .InsertAfter vbTab & vbTab > .InsertAfter strCSSName > .InsertAfter vbTab > .InsertAfter strCSSAddress > .InsertAfter vbTab > .InsertAfter strAccountNumber > .InsertAfter Chr$(13) > End With > > Thanks in advance, I think the best approach would be to leave your code as is, but then add some more statements to convert the text to a borderless table. This way you would not have to mess about with font sizes and string lengths to adjust tab positions... Just set standard column width after converting the text to a table. -- Salut! _______________________________________ Jean-Guy Marcil - Word MVP jmarcilREMOVE@CAPSsympatico.caTHISTOO Word MVP site: http://www.word.mvps.org
Thanks, that sounds good. But I'm afraid I'm not sure how to proceed to convert to the borderless table you mentioned. Do you maybe have an example that could get me started? Thanks -- Glenn "Jean-Guy Marcil" wrote: > Glenn Suggs was telling us: > Glenn Suggs nous racontait que : > > > Can someone please show me how I might set tabs in a word document in > > VBA so that fields in an incoming datasource will always begin in the > > correct column? I'm using InsertAfter to add rows to the "report" as > > shown below but depending on the amount of data in the fields and the > > size of the font, the data doesn't always line up under the headings > > as planned. The headings are stored in a Word document and then > > Access comes along with VBA to fill in the columns of data. > > > > Set rgeDoc = wrdDoc.Range > > With rgeDoc > > .InsertAfter strNetwork > > .InsertAfter vbTab > > .InsertAfter strVault > > .InsertAfter vbTab & vbTab > > .InsertAfter strCSSName > > .InsertAfter vbTab > > .InsertAfter strCSSAddress > > .InsertAfter vbTab > > .InsertAfter strAccountNumber > > .InsertAfter Chr$(13) > > End With > > > > Thanks in advance, > > I think the best approach would be to leave your code as is, but then add > some more statements to convert the text to a borderless table. This way you > would not have to mess about with font sizes and string lengths to adjust > tab positions... Just set standard column width after converting the text to > a table. > > -- > > Salut! > _______________________________________ > Jean-Guy Marcil - Word MVP > jmarcilREMOVE@CAPSsympatico.caTHISTOO > Word MVP site: http://www.word.mvps.org > > >
Glenn Suggs was telling us: Glenn Suggs nous racontait que : > Thanks, that sounds good. But I'm afraid I'm not sure how to proceed > to convert to the borderless table you mentioned. Do you maybe have > an example that could get me started? > Thanks As an example to get you started: '_______________________________________ Dim wrdDoc As Document Dim rgeDoc As Range Dim lngRecCount As Long Dim lngFieldCount As Long Dim i As Long Set wrdDoc = ActiveDocument Set rgeDoc = wrdDoc.Range lngRecCount = 5 lngFieldCount = 5 With rgeDoc For i = 1 To lngRecCount .InsertAfter "strNetwork " & i .InsertAfter vbTab .InsertAfter "strVault " & i .InsertAfter vbTab .InsertAfter "strCSSName " & i .InsertAfter vbTab .InsertAfter "strCSSAddress " & i .InsertAfter vbTab .InsertAfter "strAccountNumber " & i .InsertAfter Chr$(13) Next .ConvertToTable Separator:=wdSeparateByTabs, _ NumColumns:=lngFieldCount, _ NumRows:=lngRecCount With .Tables(1) .Borders(wdBorderLeft).LineStyle = wdLineStyleNone .Borders(wdBorderRight).LineStyle = wdLineStyleNone .Borders(wdBorderTop).LineStyle = wdLineStyleNone .Borders(wdBorderBottom).LineStyle = wdLineStyleNone .Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone .Borders(wdBorderVertical).LineStyle = wdLineStyleNone .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone .Borders.Shadow = False .Columns(1).Width = InchesToPoints(1) .Columns(2).Width = InchesToPoints(0.75) .Columns(3).Width = InchesToPoints(2.5) .Columns(4).Width = InchesToPoints(1.25) .Columns(5).Width = InchesToPoints(0.5) End With End With '_______________________________________ -- Salut! _______________________________________ Jean-Guy Marcil - Word MVP jmarcilREMOVE@CAPSsympatico.caTHISTOO Word MVP site: http://www.word.mvps.org
Hello Jean-Guy, Thank you very much for the response. The code looks good but I seem to be having some trouble making it work. I notice you have quotes around the field names and of course I want to print the data instead of the field names so I took the quotes out since I'm reading the fields in from a table (Access). But even so, the data is lining up along the left margin instead of in a horizontal row like I wanted. Here's something that may make a difference. The first part of the document stays the same so it is saved as it is, with one info line and the headers. So the first thing my code does is to open that document. Then I read thru the Access table and try to place the data beneath those headers in the correct columns. Thanks again for your help. -- Glenn "Jean-Guy Marcil" wrote: > Glenn Suggs was telling us: > Glenn Suggs nous racontait que : > > > Thanks, that sounds good. But I'm afraid I'm not sure how to proceed > > to convert to the borderless table you mentioned. Do you maybe have > > an example that could get me started? > > Thanks > > As an example to get you started: > > '_______________________________________ > Dim wrdDoc As Document > Dim rgeDoc As Range > Dim lngRecCount As Long > Dim lngFieldCount As Long > Dim i As Long > > Set wrdDoc = ActiveDocument > Set rgeDoc = wrdDoc.Range > lngRecCount = 5 > lngFieldCount = 5 > > With rgeDoc > For i = 1 To lngRecCount > .InsertAfter "strNetwork " & i > .InsertAfter vbTab > .InsertAfter "strVault " & i > .InsertAfter vbTab > .InsertAfter "strCSSName " & i > .InsertAfter vbTab > .InsertAfter "strCSSAddress " & i > .InsertAfter vbTab > .InsertAfter "strAccountNumber " & i > .InsertAfter Chr$(13) > Next > > .ConvertToTable Separator:=wdSeparateByTabs, _ > NumColumns:=lngFieldCount, _ > NumRows:=lngRecCount > With .Tables(1) > .Borders(wdBorderLeft).LineStyle = wdLineStyleNone > .Borders(wdBorderRight).LineStyle = wdLineStyleNone > .Borders(wdBorderTop).LineStyle = wdLineStyleNone > .Borders(wdBorderBottom).LineStyle = wdLineStyleNone > .Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone > .Borders(wdBorderVertical).LineStyle = wdLineStyleNone > .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone > .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone > .Borders.Shadow = False > .Columns(1).Width = InchesToPoints(1) > .Columns(2).Width = InchesToPoints(0.75) > .Columns(3).Width = InchesToPoints(2.5) > .Columns(4).Width = InchesToPoints(1.25) > .Columns(5).Width = InchesToPoints(0.5) > End With > End With > '_______________________________________ > > -- > > Salut! > _______________________________________ > Jean-Guy Marcil - Word MVP > jmarcilREMOVE@CAPSsympatico.caTHISTOO > Word MVP site: http://www.word.mvps.org > > >
Glenn Suggs was telling us: Glenn Suggs nous racontait que : > Hello Jean-Guy, > Thank you very much for the response. The code looks good but I seem > to be having some trouble making it work. I notice you have quotes > around the field names and of course I want to print the data instead > of the field names so I took the quotes out since I'm reading the > fields in from a table (Access). But even so, the data is lining up > along the left margin instead of in a horizontal row like I wanted. > Here's something that may make a difference. The first part of the > document stays the same so it is saved as it is, with one info line > and the headers. So the first thing my code does is to open that > document. Then I read thru the Access table and try to place the > data beneath those headers in the correct columns. > Thanks again for your help. > As I wrote, my code was just a working example. You have to modify it to fit your situation. Post the relevant part of your code. -- Salut! _______________________________________ Jean-Guy Marcil - Word MVP jmarcilREMOVE@CAPSsympatico.caTHISTOO Word MVP site: http://www.word.mvps.org