Deleting current footer and replacing

I

Irr

Hello,

I've recently dug into Word interop using c#.
The first thing I would like to see accomplished is to change the current
footers of a huge pile of .dots.
I've now succeeded in opening the Word Application and a Document in the
background and setting the focus on the footer of the document.
using:
WordDocument = WordApplication.Documents.Add(ref TemplatePath, ref Missing,
ref Missing, ref Missing);
// select de footer

WordApplication.ActiveWindow.ActivePane.View.SeekView =
Word.WdSeekView.wdSeekCurrentPageFooter;

Next step would be to delete the current footer and replace it by:
VoettekstImage =
WordApplication.Selection.HeaderFooter.Shapes.AddPicture(LogoLocatie, ref
WordFalse, ref WordTrue, ref Missing, ref Missing, ref Missing, ref Missing,
ref Missing);

Works, more or less, but some strange things occurs, sometimes the current
footer text doenst get deleted at all, sometimes it pastes the image so far
down only a small part of it is visible.

What I eventually would want to achieve is loading the picture into the
footer by using INCLUDEPICTURE, but when I try to do this using:
WordApplication.ActiveWindow.Selection.Fields.Add(WordApplication.ActiveWindow.Selection.Range,
Word.WdFieldType.wdFieldEmpty, "INCLUDEPICTURE
""C:\\mijnmap\\bedrijfslogo.png"" ", ref missing)


I get a literal, not a Field.

So my question in short;

How can I change the current footer into a Field which let a picture
include, any help would be greatly appreciated!

Regards.
 
S

Shauna Kelly

Hi Lrr

I find the easiest way to get around these problems is to avoid them. First,
I avoid actually selecting the footer. And then, I avoid mucking around with
Word's object model trying to manipulate the content in the footer.

You can do this using an AutoText saved in the relevant template. Type all
the content you want to go into the footer (text, fields, images, whatever).
Apply the appropriate styles. Save it (including the final paragraph mark)
as an AutoText in the template to which your document is attached. That's
much easier and quicker than manipulating paragraphs and fields and images
in code. And, it has the advantage that you can change the content of the
AutoText any time you like without having to change the code.

To insert the AutoText you can use something like the following.

Sub InsertFooter()

Dim oTemplate As Word.Template
Dim oFooter As Word.HeaderFooter
Dim oAT As Word.AutoTextEntry


'Identify the footer you want to change
Set oFooter = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)

'Get the AutoText to insert into the footer
Set oTemplate = ActiveDocument.AttachedTemplate
Set oAT = oTemplate.AutoTextEntries("MyFooter")

'Delete the content of the existing footer
oFooter.Range.Delete

'Insert the new one
oAT.Insert _
Where:=oFooter.Range, _
RichText:=True

'And delete the extraneous paragraph that Word leaves behind
With oFooter.Range
If .Paragraphs.Count > 1 Then
.Paragraphs.Last.Range.Delete
End If
End With

End Sub

For background, see the following:
Using AutoText
http://www.word.mvps.org/FAQs/Customization/AutoText.htm

Working with sections
http://www.word.mvps.org/FAQs/Formatting/WorkWithSections.htm


Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
I

Irr

Hi Shauna, sorry for my late reaction, but thank you.

But let me clarify a bit further, I wanted to traverse a few different
directories looking for .DOTs and change their footers, into a mergefield
with a link to an image file (that way only the file has to change in future,
not every footer).

I did, however, accomplish this using a small program I've written in C#,
using the Word dlls.

It's only a little program, but feel free to use/modify/whatever it.

It's a somewhat stripped down version for easy op copying.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Word = Microsoft.Office.Interop.Word;

namespace Footer_001
{
class Program
{
// TODO
// ********
// Exceptielijst bijhouden
static void Main(string[] args)
{
#region lokalevariabelen
string BestandLocatieOriginelen = "L:\\CCS\\word\\sebas_dev\\paar";
string BestandLocatieNieuwe =
"L:\\CCS\\word\\sebas_dev\\paarchanged\\";
string FileNaam = "";

Object Missing = System.Reflection.Missing.Value;
Object WordTrue = true;
Object WordFalse = false;

Object IncludePicture = "INCLUDEPICTURE
\"L:\\\\CCS\\\\word\\\\Bedrijf 1\\\\LOGOs\\\\voettekst.jpg\"";

Object TemplatePath = "";
#endregion lokalevariabelen
try
{
// loop door de directory
foreach (string directory in
Directory.GetFiles(BestandLocatieOriginelen))
{


// bepaal de bestandsnaam en het template path
FileNaam = Path.GetFileName(directory);

// geen tempfiles
if (!FileNaam.StartsWith("~"))
{
#region setup

// creer een nieuwe wordapplicatie
Word.Application WordApplication = new
Word.Application();

// creer een nieuw document
Word.Document WordDocument = new Word.Document();

// Word zichtbaar tijdens verwerking
WordApplication.Visible = true;

// print naar de console
Console.WriteLine(" Bestand: " + directory);

// cast string directory naar een object
TemplatePath = (Object)directory;

// open het word document
WordDocument = WordApplication.Documents.Add(ref
TemplatePath, ref Missing, ref Missing, ref Missing);
#endregion setup

#region footerbewerking
// focus op de footer

WordApplication.ActiveWindow.ActivePane.View.SeekView =
Word.WdSeekView.wdSeekCurrentPageFooter;

// selecteer de tekst in de footer
WordDocument.Select();

// delete de tekst in de footer
WordDocument.ActiveWindow.Selection.Delete(ref
Missing, ref Missing);

// plaats een field in de footer

WordDocument.ActiveWindow.Selection.Fields.Add(WordApplication.ActiveWindow.Selection.Range, ref Missing, ref IncludePicture, ref Missing);
#endregion footerbewerking




#region bestandopslaan
// bestand opslaan als
Object SaveAsFile = (Object)BestandLocatieNieuwe +
FileNaam;
WordDocument.SaveAs(ref SaveAsFile, ref Missing, ref
Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref
Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref
Missing, ref Missing);

// afsluiten document en applicatie
WordDocument.Close(ref WordFalse, ref WordFalse, ref
WordFalse);
WordApplication.Quit(ref Missing, ref Missing, ref
Missing);

#endregion bestandopslaan

}

}
}
catch (Exception ex)
{
Console.WriteLine("Exceptie: " + ex);
}

Console.Read();
}
}
}
 

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