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 2007

Tip: Looking for answers? Try searching our database.

Setting tabs in VBA

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Glenn Suggs - 26 Feb 2007 15:53 GMT
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,
Signature

Glenn

Jean-Guy Marcil - 26 Feb 2007 17:46 GMT
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
[quoted text clipped - 19 lines]
>
> 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.

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Glenn Suggs - 26 Feb 2007 18:16 GMT
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
Signature

Glenn

> Glenn Suggs was telling us:
> Glenn Suggs nous racontait que :
[quoted text clipped - 28 lines]
> tab positions... Just set standard column width after converting the text to
> a table.
Jean-Guy Marcil - 26 Feb 2007 20:24 GMT
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
'_______________________________________

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Glenn Suggs - 27 Feb 2007 15:33 GMT
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.
Signature

Glenn

> Glenn Suggs was telling us:
> Glenn Suggs nous racontait que :
[quoted text clipped - 53 lines]
> End With
> '_______________________________________
Jean-Guy Marcil - 27 Feb 2007 19:53 GMT
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
[quoted text clipped - 9 lines]
> 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.

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

 
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.