printing filename of document in the footer

D

Diane

Group,
I have a VB.NET program that searches a directory for all *.docs, then
does a search & replace funtion. For each document that a search & replace
was made, I need a printed copy of the document with the "filename" included
in the footer of the printed doc. I have tried various code samples to get
the filename in the footer, but still have not been able to accomplish this.
This is the latest code I have use:

Dim section As Word.Section
Dim wordHeaderIndex As Word.WdHeaderFooterIndex =
Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage

If printdoc = "Y" Then
section.Headers.Item(wordHeaderIndex).Range.Fields.Add(Microsoft.Office.Interop.Word.WdFieldType.wdFieldEmpty, "FILENAME", True)
oDoc.PrintOut()
End If

Above code will not compile
error---->
"Value of type 'Microsoft.office.interop.word.wdFieldType' cannot be
converted to "Microsoft.Office.Interop.Word.Range'.

Any examples of filename printing in a footer would be great!
Diane
 
J

Jay Freedman

The Add method of the Fields collection takes *four* arguments, not
three. The first argument, and the only one that isn't optional, is
the Range in which to insert the field. In this case, it's
section.Headers.Item(wordHeaderIndex).Range. Although, if you want the
filename in the footer, you need to replace Headers with Footers (and
although it makes no difference how it executes, you should reduce the
confusion by changing wordHeaderIndex to wordFooterIndex).

To save a bit of room, you can rewrite this statement as

oDoc.Fields.Add(section.Footers.Item(wordHeaderIndex).Range,
Microsoft.Office.Interop.Word.WdFieldType.wdFieldEmpty, "FILENAME",
True)

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
D

Diane

Jay Freedman said:
The Add method of the Fields collection takes *four* arguments, not
three. The first argument, and the only one that isn't optional, is
the Range in which to insert the field. In this case, it's
section.Headers.Item(wordHeaderIndex).Range. Although, if you want the
filename in the footer, you need to replace Headers with Footers (and
although it makes no difference how it executes, you should reduce the
confusion by changing wordHeaderIndex to wordFooterIndex).

To save a bit of room, you can rewrite this statement as

oDoc.Fields.Add(section.Footers.Item(wordHeaderIndex).Range,
Microsoft.Office.Interop.Word.WdFieldType.wdFieldEmpty, "FILENAME",
True)

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
D

Diane

Jay,
With this code:
Dim section As Word.Section
Dim wordFooterIndex As Word.WdHeaderFooterIndex =
Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage

If printdoc = "Y" Then
oDoc.Fields.Add(section.Footers.Item(wordHeaderIndex).Range, _
Microsoft.Office.Interop.Word.WdFieldType.wdFieldEmpty,
"FILENAME", True)
oDoc.PrintOut()
End If


I have this problem....--->

If printdoc = "Y" Then
oDoc.Fields.Add(section.

"section" is underlined in green and gives the following error:
variable section has been used before it has been assigned a value.
A null reference exception could result at runtime.


I can't figure out what I should be assigning to "section". At run-time the
following error appears:
NullReferenceException was unhandled.
Object reference not set to an instance of an object instance.
etc....ect.....

Any thoughts would be appreciated!

Thanks again ffor your post
 
J

Jay Freedman

Hi Diane,

I assumed that the code you originally posted was only part of a
larger program, and that you had left out the assignment of the
variable 'section' and other stuff to shorten the post. It appears now
that you simply haven't made the assignment at all.

You already have the declaration

Dim section As Word.Section

Now you have to say which section you want to work on. For example, if
it's the first (or only) section in the document, you need this:

Set section = oDoc.Sections(1)

If the footer needs to be in every section, regardless of how many
sections there are, then you'll need a For Each loop like

For Each section In oDoc.Sections
oDoc.Fields.Add(...
Next

If that's missing, then I suspect you're also missing the declaration
and assignment of oDoc.

Another thing: I think you'll find that wdHeaderFooterFirstPage is the
wrong thing to use for wordFooterIndex. The First Page footer is
available in a section only if the section's layout has been set to
"Different first page"; otherwise you'll probably get an error. Even
if it lets you assign it, you won't see the footer unless that setting
is done. Probably what you want instead is wdHeaderFooterPrimary.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
D

Diane

Jay,
I am posting my full code, I am still having two issues indicated below. If
you would be able to "grade" me that would be great. The logic of my
program:
1) Read all docs in a given directory, search for a stopcode indicated by a
"%".
2) Any doc that is found, replace "%" with a docvariable, then print the
document with the filename in the footer.

2 ISSUES:
**1)NullReferenceException was unhandled
Use the "new" keyword to create an object instance.
THIS ERROR APPEARS ON THE LINE----> section = oDoc.Sections(1)
(note - I only have one section on each document that will print)

**2)this line gives an error:

section.Add.Footers.Item(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Fields.Add((Microsoft.Office.Interop.Word.WdFieldType.wdFieldEmpty),
"FILENAME", True)

ERROR--->"Value ot type 'Microsoft.Office.Interop.Word.WdFieldTYpe
cannot be converted to 'Microsoft.Office.Interop.Word.Range'.
*****************************
CODE:
Option Explicit On

Imports Word = Microsoft.Office.Interop.Word
Imports System.IO


Public Class Form1
Public Numberoffiles As Integer
Public StopCodes As Integer
Public oWord As New Word.Application
Public oDoc As Word.Document
Public str As String
Public sw As New StringWriter

Public Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnUpdate.Click

Call getdocs()
End Sub

Public Sub getdocs()

Try
oWord.System.Cursor = Word.WdCursorType.wdCursorWait
With oWord.FileSearch
.LookIn = "J:\test"
.FileName = "*.doc"
.Execute()
For Each str In .FoundFiles
Numberoffiles += 1
CkStopCode()
oWord.Documents.Save()
oWord.Documents.Close()
Next
End With
MessageBox.Show("Total docs " & Numberoffiles.ToString & vbCrLf _
& "Total Stop Code Docs = " & stopcodes.tostring)

oWord.Application.Quit()

Finally

End Try
End Sub


Public Sub CkStopCode()
'search each document for a stopcode, if stopcode found document should be
printed
Dim printdoc As String
Dim orng As Word.Range
orng = oWord.ActiveDocument.Range
Dim fnd As Word.Find = orng.Find
Dim section As Word.Sections
section = oDoc.Sections(1)

Dim wordFooter As Word.WdHeaderFooterIndex =
Word.WdHeaderFooterIndex.wdHeaderFooterPrimary

oWord.Documents.Open(str)
printdoc = "N"
fnd.ClearFormatting()
fnd.Forward = True
fnd.Text = "%"
fnd.Execute()
If fnd.Found = True Then
StopCodes += 1
printdoc = "Y"
End If
Do While fnd.Found
orng.Delete()
orng.Fields.Add(orng, Word.WdFieldType.wdFieldDocVariable,
"$$$", True)
fnd.Execute()
Loop

'if stopcode found, print doc w/ filename in the footer
If printdoc = "Y" Then

section.Add.Footers.Item(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Fields.Add((Microsoft.Office.Interop.Word.WdFieldType.wdFieldEmpty),
"FILENAME", True)

oDoc.PrintOut()
End If
End Sub

End Class


***
Jay,
Many thanks for your help with this!
Diane
 
J

Jay Freedman

Diane said:
Jay,
Thanks for your help on this!
Finally have it working!!
Diane

Excellent! Thanks for letting me know.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top