none
VB 2010 and MS Word 2007 or later (Header and footer) RRS feed

  • Question

  • Hi all,

    I am working on a project which uses vb 2010 and word 2007 or later. I will list out my problems one by one

    1. I want to insert a picture and a table in the header, on all pages (No different first page). I somehow managed to insert both, but when i insert the table the picture got deleted.
    2. In the table which i've created in the earlier step, in a particular cell, i need page number to be inserted in the form "Page X of Y"

    Below is the code (which is not working correctly), Please help me

    oWord = CreateObject("Word.Application")
            oWord.Visible = False
            oDoc = oWord.Documents.Open(strFile2Open)

            oDoc.Sections(1).PageSetup.DifferentFirstPageHeaderFooter = False
            oDoc.PageSetup.HeaderDistance = oWord.InchesToPoints(1.5) 
            With oDoc.Sections(1).Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
                If .InlineShapes.Count > 0 Then
                    oShape = .InlineShapes(1)
                    oShape.Delete()
                End If
                If .Tables.Count > 0 Then
                    oTable = .Tables(1)
                    oTable.Delete()
                End If

                .InlineShapes.AddPicture("D:\JAISON K V\QMS\DOCS\Images\Header1.png")
                'oDoc.Sections(1).Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
                oTable = .Tables.Add(oDoc.Sections(1).Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range, 1, 2)
                oTable.Range.Font.Name = "Verdana"
                oTable.Range.Font.Size = 8
                oTable.Borders.Enable = 1
                oTable.Cell(1, 1).Range.Text = "<Table data>"
                oTable.Cell(1, 2).Range.Text = "<Table data>"
                oTable.Rows.Add()
                oTable.Columns.Add()
                oTable.Columns.Add()
                oTable.Cell(2, 1).Range.Text = "<Table data>"
                oTable.Cell(2, 2).Range.Text = "<Table data>"
                oTable.Cell(2, 3).Range.Text = "<Table data>"
                oTable.Cell(2, 4).Range.Text = "<Page number>"
                oTable.Rows.Add()
                oTable.Cell(3, 1).Range.Text = "<Table data>"
                oTable.AutoFitBehavior(WdAutoFitBehavior.wdAutoFitWindow)
                oTable.Rows(1).Cells(2).Merge(oTable.Rows(1).Cells(4))
                oTable.Rows(3).Cells(1).Merge(oTable.Rows(3).Cells(4))
                oTable = Nothing
            End With

            For Each section As Word.Section In oWord.ActiveDocument.
                Dim HeaderRange As Word.Range = section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range

                If HeaderRange.Tables.Count > 0 Then
                    oTable = HeaderRange.Tables(1)
                    Dim tRange As Word.Range = oTable.Cell(2, 4).Range
                    tRange.Fields.Add(tRange, Word.WdFieldType.wdFieldEmpty, "AUTOTEXT ""Page X of Y"" ", True)
                    tRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
                End If
            Next

            oWord.Documents.Save()
            oWord.Documents.Close()
            oWord.Quit()

    ---------------------------------------------------------------------------------

    Thanks in Advance

    Jaison

    Saturday, March 2, 2013 7:58 AM

Answers

  • I don't know what the error might be there. I don't use VB 2010 (only vba), so I basically copied it from your own code.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by JaisonKV Monday, March 4, 2013 1:49 PM
    Monday, March 4, 2013 1:07 PM

All replies

  • The basic problem with your picture/table conflict is that you're inserting both at the same location (ie, the entire header range).

    If I understand your code correctly, it seems the first thing you're doing is deleting whatever content the header already has. In that case, instead of testing for tables and in-line shapes, you can just use '.Delete' to delete everything that's already there.

    You then need to decide whether your inline shape should go in the table, or before/after it. If the former, insert the table first, then insert the inline shape into one of its cells. If the latter, insert an empty paragraph into the header, then insert the inlineshape into one and the table into the other.

    Also, when you create the table, why not create it with all of the rows & columns you require? This is a whole lot more efficient than adding them later.

    Finally, you need to check your referencing - you have a mix of oWord and Word, and I doubt both are valid. In some cases also, your oWord, oWord.Documents and oWord.ActiveDocument references should probably be to oDoc.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, March 2, 2013 9:09 PM
  • Hey..

    Thank you for your answer. My new improved (working) code is below. Just one more question though, How can I insert page number in the form "Page X of Y" in a cell?

            oWord = CreateObject("Word.Application")

            oWord.Visible = False

            oDoc = oWord.Documents.Open(strFile2Open)

            oDoc.Sections(1).PageSetup.DifferentFirstPageHeaderFooter = True

            oDoc.Sections(1).PageSetup.OddAndEvenPagesHeaderFooter = True

            oWord.ActiveWindow.Application.doc()

            oDoc.Sections(1).Headers(WdHeaderFooterIndex.wdHeaderFooterFirstPage).Range.Delete()

            oDoc.Sections(1).Headers(WdHeaderFooterIndex.wdHeaderFooterEvenPages).Range.Delete()

            oDoc.Sections(1).Headers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Delete()

            oDoc.Sections(1).Footers(WdHeaderFooterIndex.wdHeaderFooterFirstPage).Range.Delete()

            oDoc.Sections(1).Footers(WdHeaderFooterIndex.wdHeaderFooterEvenPages).Range.Delete()

            oDoc.Sections(1).Footers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Delete()

            With oWord


                .ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekFirstPageHeader

                .ActiveWindow.Selection.InlineShapes.AddPicture("<Path>")

                .ActiveWindow.Selection.Tables.Add(.ActiveWindow.Selection.Range, 3, 4)

                oTable = .ActiveWindow.Selection.Tables(1)

                oTable.Range.Font.Name = "Verdana"

                oTable.Range.Font.Size = 8

                oTable.Borders.Enable = 1

                oTable.Cell(1, 1).Range.Text = "<Text>"

                oTable.Cell(1, 2).Range.Text = "<Text>"

                oTable.Cell(2, 1).Range.Text = "<Text>"

                oTable.Cell(2, 2).Range.Text = "<Text>"

                oTable.Cell(2, 3).Range.Text = "<Text>"

                oTable.Cell(2, 4).Range.Text = " ??? Insert Pagenumber here"

                oTable.Cell(3, 1).Range.Text = "<Text>"

                oTable.Rows(1).Cells(2).Merge(oTable.Rows(1).Cells(4))

                oTable.Rows(3).Cells(1).Merge(oTable.Rows(3).Cells(4))

                oTable.AutoFitBehavior(WdAutoFitBehavior.wdAutoFitWindow)

                .ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekFirstPageFooter

                .ActiveWindow.Selection.InlineShapes.AddPicture("<Path>")

               

            End With

            oDoc.PageSetup.HeaderDistance = oWord.InchesToPoints(1.5)

            oDoc.PageSetup.FooterDistance = oWord.InchesToPoints(1.5)

            oWord.Documents.Save()

            oWord.Documents.Close()

            oWord.Quit()

    Sunday, March 3, 2013 12:49 PM
  • Using your previous code as the starting point:

    Dim tRange As Word.Range = oTable.Cell(2, 4).Range
    tRange.End = tRange.End - 1
    oDoc.Fields.Add(tRange, oWord.WdFieldType.wdFieldEmpty, "AUTOTEXT ""Page X of Y""", False)

    Note that, to insert a field into the end of a table cell you need to move the range end back one position, to exclude the end-of-cell marker.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, March 3, 2013 9:28 PM
  • Thank you,

    You helped me a lot, but the code encountered the error "Public member 'WdFieldType' on type 'ApplicationClass' not found." What did I missed?

    Monday, March 4, 2013 10:33 AM
  • I don't know what the error might be there. I don't use VB 2010 (only vba), so I basically copied it from your own code.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by JaisonKV Monday, March 4, 2013 1:49 PM
    Monday, March 4, 2013 1:07 PM
  • Thank you for your help. God Bless
    Monday, March 4, 2013 1:49 PM