We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Create Tabe in Word Header in VB6 with an active document arelady opened

Medium Priority
661 Views
Last Modified: 2012-05-11
I need to create a table in the header of a word document that is already opened, and fill the table with data.

I just need and example.

P.S.

The file may or may not be saved.
Comment
Watch Question

GrahamSkanRetired
CERTIFIED EXPERT
Top Expert 2012

Commented:
OK. Try this.
Sub CreateHeaderTable()

    Dim wdDoc As Word.Document
    Dim wdApp As Word.Application
    Dim tbl As Table
    
    Set wdApp = GetObject(, "Word.Application")
    wdApp.Visible = True
    
    Set wdDoc = wdApp.ActiveDocument
    Set tbl = wdDoc.Tables.Add(wdDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range, 4, 6)
    tbl.Cell(1, 2).Range.Text = Format(Now, "dd.MM.yyyy")
    tbl.Cell(1, 4).Range.Text = Format(Now, "hh:nn")
    tbl.Cell(1, 6).Range.Text = Format(DateAdd("n", 18, Now), "hh:nn")
    
End Sub

Open in new window

Author

Commented:
I had to put the code in a timer so I had time to make word the active app.  

I have a button on a form to perform this fuction so I would realy like not
having to make the word app active.  But if I have to I will do this in code.

I had to   Dim tbl As Table  Dim tbl As word.Table  because I am using ADO

But when it got to the line below

Set tbl = wdDoc.Tables.add(wdDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range, 4, 6)

I got a Run-time error '6028'
The Range cannot be deleted.

My Process was
I  opened up a word doc.  
Did not save.
Hit button VB program.  
Got msg

The document does not have a header. Just a brand new document. called  Doc1.doc

Author

Commented:
I would really appreciate an answer on the above qustions.

Thanks
GrahamSkanRetired
CERTIFIED EXPERT
Top Expert 2012

Commented:
It is trying to replace something already in  the header.

This code avoids that by inserting the table after the existing text.

Sub CreateHeaderTable()

    Dim wdDoc As Word.Document
    Dim wdApp As Word.Application
    Dim tbl As Word.Table
    Dim rng as Word.Range
    
    Set wdApp = GetObject(, "Word.Application")
    wdApp.Visible = True
    
    Set rng = wdDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range
    rng.Collapse wdCollapseEnd
    Set tbl = wdDoc.Tables.Add(rng, 4, 6)
    tbl.Cell(1, 2).Range.Text = Format(Now, "dd.MM.yyyy")
    tbl.Cell(1, 4).Range.Text = Format(Now, "hh:nn")
    tbl.Cell(1, 6).Range.Text = Format(DateAdd("n", 18, Now), "hh:nn")
    
End Sub

Open in new window

Author

Commented:
Run-time error '91'
Object Variable or with block variable not set

Statement
Set rng = wdDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range
Retired
CERTIFIED EXPERT
Top Expert 2012
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thank you that work.  
Question do I have to have Word active before this will work?

Author

Commented:
Thanks so much
GrahamSkanRetired
CERTIFIED EXPERT
Top Expert 2012

Commented:
The code expects (as you mentioned in the question)  that the document is already the Active document in a current instance of the Word Application. The Application itself needn't be the one in focus.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.