Adding footer, Word document, Vb .NET

gilou28
07-02-2004, 08:39 AM
Through VB .NET, I want to add a footer with the document name and page number (page x of y) to a document that has no footer (the document is dynamically imported , modified by changing it's font and adding the footer, printed and closed without saving)

I've looked at MSDN, but could not find what I was looking for.

Thanks for your appreciated help.

herilane
07-02-2004, 10:58 AM
Are you in general familiar with automating Word from VB.Net, and need help just with the specific syntax for modifying the footer, or do you need help with the whole thing including opening, printing, closing etc?

Could you post the code that you have at the moment?

gilou28
07-02-2004, 11:29 AM
Here is my code. I have multiple .TXT files that need to either be emailed or faxed.

For each file, I load them into word, modify the font then print to PDF Distiller, then send the file.

I need to add a footer to each pages with the document name on the left and the page x of y on the right.

Thanks.
Private Sub GetTXT()
Try
Dim Client As String
Dim index As Integer = 1
Dim currdest As destinataires

ClientInfo.Text = "En Traitement"
Me.Refresh()
' Liste des fichiers
If Not Directory.Exists(Repertoire) Then
MessageBox.Show("Répertoire inexistant: " + Repertoire)
ClientInfo.Text = "Erreur"
Me.Refresh()
Exit Sub
End If

ListeTXT = Directory.GetFiles(Repertoire, FileMask)
Dim Fichier As String

If ListeTXT.Length <= 0 Then
MessageBox.Show("Aucun fichier à traiter: " + Repertoire)
ClientInfo.Text = "Terminé"
Me.Refresh()
Exit Sub
End If

Dim oWord As Word.ApplicationClass
oWord = CreateObject("Word.Application")
oWord.Visible = False
oWord.WindowState = Word.WdWindowState.wdWindowStateMinimize
Me.BringToFront()

If Not imp.Checked Then oWord.Application.ActivePrinter = PDFPrinter

For Each Fichier In ListeTXT
Client = Microsoft.VisualBasic.Mid(Fichier, Repertoire.Length + 2, 4)
dest = SQLGetClientInfo(Client)
If dest(0).FaxEmail > "" Then
ClientInfo.Text = "Traitement: " + index.ToString + " de " + ListeTXT.Length.ToString _
+ ", " + "Client: " + Client
Me.Refresh()
oWord.Documents.Open(Fichier)

Dim rng As Word.Range = oWord.ActiveDocument.Range

'Dim rng As Word.Range = oWord.ActiveDocument.Range(0, oWord.ActiveDocument.Characters.Count)
With rng
.Font.Bold = True
.Font.Size = 8
.Font.Name = "Courier"
End With
rng.Select()

With oWord
.Selection.EndKey()
.Selection.Fields.Add(oWord.ActiveDocument.Range _
(.Selection.End, .Selection.End), , "NUMPAGES", )
.Selection.Fields.Add(oWord.ActiveDocument.Range _
(.Selection.End, .Selection.End), , "FILENAME", )
End With


'oWord.ActiveDocument.Footnotes.Add(oWord.ActiveDocument, Client + ".TXT")

oWord.PrintOut()
oWord.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
System.Threading.Thread.Sleep(3000)
If Not imp.Checked Then
For Each currdest In dest
If currdest.Destinataire <> "" Then
EnvoyerListe(currdest.Langue, currdest.FaxEmail = "F", currdest.Destinataire)

End If
Next
End If
Else
ClientInfo.Text = "Exception, pas de liste pour: " + Client
Me.Refresh()
End If

index = index + 1
Next

oWord.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord)
oWord = Nothing
ClientInfo.Text = "Terminé"
Me.Refresh()

Catch e As Exception
MessageBox.Show("Erreur à la lecture des fichiers .txt: " + e.ToString())
End Try
End Sub

herilane
07-02-2004, 12:13 PM
Here's how I would do it in VBA. You know more than I do about converting it to VB.Net (but let us know if you run into problems with that) :)Dim rng As Range
Set rng = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
With rng
.Fields.Add Range:=rng, Type:=wdFieldFileName
.InsertAfter Text:=vbTab & vbTab
.Collapse wdCollapseEnd
.Fields.Add Range:=rng, Type:=wdFieldPage
.EndOf wdStory, wdMove
.InsertAfter Text:=" of "
.Collapse wdCollapseEnd
.Fields.Add Range:=rng, Type:=wdFieldNumPages
End With

gilou28
07-02-2004, 12:17 PM
Thanks for editing my post.


I now know how to put code ;-)

gilou28
07-02-2004, 12:22 PM
I had to prefix ActiveDocument with oWord.

I have the following errors:

On oWord.ActiveDocument.Sections(1) I get:
Interface 'Word.Sections' cannot be indexed because it has no default property.

ON wdHeaderFooterPrimary I get:
Name 'wdHeaderFooterPrimary' is not declared.

I have the same error on: wdCollapseEnd, wdFieldPage, wdStory, wdMove, wdCollapseEnd, wdFieldNumPages

I may have to import something....

herilane
07-02-2004, 01:34 PM
OK, the following comes with all kinds of caveats - I don't have .Net and have never seen it up close - but here goes...

For the first problem, try oWord.ActiveDocument.Sections.Items(1).

For the undeclared constants, you can either import all the relevant enumerated constants, or reference the constants fully.'either imports, and "short" referencing:
Imports Word.WdCollapseDirection
'...
rng.Collapse wdCollapseEnd


'or full referencing:
rng.Collapse Word.WdCollapseDirection.wdCollapseEndThe easiest way to find out how each enumerated constant should be declared, is through the object browser in Word VBA. (Or in the object browser in the IDE, if the object browser still exists in .Net - I don't know...) Search for the constant (for example wdCollapseEnd) and you'll see this down in the declarations section:
Const wdCollapseEnd = 0
Member of Word.WdCollapseDirectionEdit: I've been told by reliable sources (that is, by Mike_R) that the object browser is present in the .Net IDE. That'll have all the information.

gilou28
07-03-2004, 09:37 AM
OK Thanks, I'll try that monday

gilou28
07-03-2004, 10:45 AM
Monday came earlier ;-)

I had to download the Office XP PIA

It's now working prefectly... Thanks.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum