Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2006

Tip: Looking for answers? Try searching our database.

printing filename of document in the footer

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Diane - 21 Nov 2006 20:34 GMT
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
Jay Freedman - 22 Nov 2006 02:24 GMT
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        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

>Group,
>I have a VB.NET program that searches a directory for all *.docs,  then  
[quoted text clipped - 20 lines]
>Any examples of filename printing in a footer would be great!
>Diane
Diane - 22 Nov 2006 21:05 GMT
> The Add method of the Fields collection takes *four* arguments, not
> three. The first argument, and the only one that isn't optional, is
[quoted text clipped - 41 lines]
> >Any examples of filename printing in a footer would be great!
> >Diane
Diane - 22 Nov 2006 21:30 GMT
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

> > The Add method of the Fields collection takes *four* arguments, not
> > three. The first argument, and the only one that isn't optional, is
[quoted text clipped - 41 lines]
> > >Any examples of filename printing in a footer would be great!
> > >Diane
Jay Freedman - 27 Nov 2006 03:18 GMT
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        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

>Jay,
>With this code:
[quoted text clipped - 73 lines]
>> > >Any examples of filename printing in a footer would be great!
>> > >Diane
Diane - 27 Nov 2006 19:29 GMT
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
Diane - 28 Nov 2006 21:14 GMT
Jay,
Thanks for your help on this!
Finally have it working!!
Diane
Jay Freedman - 28 Nov 2006 21:39 GMT
> Jay,
> Thanks for your help on this!
> Finally have it working!!
> Diane

Excellent! Thanks for letting me know.

Signature

Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.


Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.