Heading In First or Second Row In Word Doc (1 Viewer)

Ashfaque

Student
Local time
Tomorrow, 00:11
Joined
Sep 6, 2004
Messages
894
Hi,

I have below vba code in a sub that opens word application and generates the data from a table based on criteria provided thru a form.

These lines are smoothly working..

But I need to place some headings in first line or second line of the document and then to start the table information to appear in the doc.

I tried to place my company name below way (see bold lines);

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim I As Integer

Set db = CurrentDb()

Set rs = db.OpenRecordset("SELECT * FROM T_CustomerMaster WHERE SALESMANCODE='" & Forms!F_ReportMaster!TxtRepCode & "' order by custcode;")

WordSetup

doc.Tables.Add Range:=doc.Range, numrows:=1, numcolumns:=5

'Trying to place main heading

'doc.Range(1, 0).Text = "fsdafds"

'doc.Tables(1).Columns(0).Cells(0).Range.Text = "My Company Name"


doc.Tables(1).Columns(1).Cells(1).Range.Text = "CODE"
doc.Tables(1).Columns(2).Cells(1).Range.Text = "CUST NAME"
doc.Tables(1).Columns(3).Cells(1).Range.Text = "REP"
doc.Tables(1).Columns(4).Cells(1).Range.Text = "CR. LIMIT"
doc.Tables(1).Columns(5).Cells(1).Range.Text = "CR. PERIOD"

I = 1

Do Until rs.EOF
doc.Tables(1).Columns(1).Cells.Add
doc.Tables(1).Columns(1).Cells(I + 1).Range.Text = rs.Fields(0)
doc.Tables(1).Columns(2).Cells(I + 1).Range.Text = rs.Fields(1)
doc.Tables(1).Columns(3).Cells(I + 1).Range.Text = rs.Fields(2)
doc.Tables(1).Columns(4).Cells(I + 1).Range.Text = rs.Fields(16)
doc.Tables(1).Columns(5).Cells(I + 1).Range.Text = rs.Fields(17)
doc.Tables(1).Columns.AutoFit

I = I + 1
rs.MoveNext
Loop

doc.Activate
......
.......

But the table starting from column 1 and cell 1 till data ends up.

Finally I converted my trial lines to remark as it is not working at all.

Can someone please help?

Thanks,
Ashfaque
 

DavidAtWork

Registered User.
Local time
Today, 19:41
Joined
Oct 25, 2011
Messages
699
TBH, I havn't tried much vba with Word, but what happens if you try adding the heading text before inserting the table, so the line
doc.Range(1, 0).Text = "My Company Name"
before
doc.Tables.Add Range:=doc.Range, numrows:=1, numcolumns:=5

BTW what is WordSetup, is this a function

David
 

Ashfaque

Student
Local time
Tomorrow, 00:11
Joined
Sep 6, 2004
Messages
894
Thanks David,

Wordsetup is sub routin as below:

Sub WordSetup()
Set WordApp = New Word.Application
WordApp.Documents.Add
Set doc = WordApp.ActiveDocument
WordApp.Visible = True
End Sub

I already tried below before those line that executes table information as mentioned in my code earlier but this produces error.

doc.Range(1, 0).Text = "My Company Name"

Run-time error '4608' Value out of range

Any idea?

Thanks,
Ashfaque
 

tfurnivall

Registered User.
Local time
Today, 11:41
Joined
Apr 19, 2012
Messages
81
I think that you probably want to select the top of the document, and then use the InsertAfter method.

Code:
ActiveDocument.Range.Select
Selection.Collapse wdCollapseStart
Selection.InsertAfter "What ever text you want to insert here, including a new line" & vbCrLf
This will give you the requested text on a separate line. Then you can go ahead and add your table.

I used to have the hardest time trying to add text to a range, but the secret is to collapse that range (using .select and .collapse), and then insert, using .insertafter. You were trying, as far as I can see, to add a whole load of text into a zero character range - which probably caused your error!

HTH

Tony
 

Ashfaque

Student
Local time
Tomorrow, 00:11
Joined
Sep 6, 2004
Messages
894
Thanks tFurnivall,

Your second line "Selection.Collapse wdCollapseStart" produces below error
Run-time error '91' - Object variable or With block variable not set.

Thanks,
Ashfaque
 

tfurnivall

Registered User.
Local time
Today, 11:41
Joined
Apr 19, 2012
Messages
81
Hmm!

I just added it to a brand new document with nothing else, and it worked like a charm.

I'm using Word 2010 - how about you?

Tony
 

tfurnivall

Registered User.
Local time
Today, 11:41
Joined
Apr 19, 2012
Messages
81
I don't know.

However, using the following code (conforming to syntax published in 1999, and hopefully, therefore, preceding Word 2003 ;-)
Code:
Dim MyRange As Range

Set MyRange = Range(0, 0)
MyRange.InsertAfter "This is the inserted text" + vbCrLf

This did it for me, too!

Good luck,

Tony
 

Ashfaque

Student
Local time
Tomorrow, 00:11
Joined
Sep 6, 2004
Messages
894
It produces error something like....

Run-time error '1004'
Method 'Range of Object'_Global failed.

Anyone have idea...?

Thanks,
Ashfaque
 

Users who are viewing this thread

Top Bottom