[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-04-22
9
Medium Priority
?
595 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.
0
Comment
Question by:MKadric
  • 5
  • 4
9 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 35449006
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

0
 

Author Comment

by:MKadric
ID: 35451727
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
0
 

Author Comment

by:MKadric
ID: 35471237
I would really appreciate an answer on the above qustions.

Thanks
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 35471561
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

0
 

Author Comment

by:MKadric
ID: 35474741
Run-time error '91'
Object Variable or with block variable not set

Statement
Set rng = wdDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 35474828
Sorry. Missed a line out in the copying.



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 wdDoc = wdApp.ActiveDocument
    
    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

0
 

Author Comment

by:MKadric
ID: 35477378
Thank you that work.  
Question do I have to have Word active before this will work?
0
 

Author Closing Comment

by:MKadric
ID: 35477424
Thanks so much
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 35477894
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.
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question