Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating Word documents using C#

Status
Not open for further replies.

MarkGreen

Technical User
Oct 4, 2002
40
GB
Hi Guys,

I've been going mad trying to get this to work - there seems to be very little c# resource available on these Office libraries.

Anyway I'm trying to update a Word document to insert autotext into the footer of a document - the "page x of y" autotext entry. I can get the autotext to be entered into the footer but it will overwrite any existing text that is already present in the buffer.
This is (a summary of) my existing code:

//Add Page Numbers
Word.Document docWord;
Word.AutoTextEntries docAutotextEntries;
Word.AutoTextEntry docAutotextEntry;
Word.Section wordSection;


docWord = WordApp.ActiveDocument;

docAutotextEntries = WordApp.NormalTemplate.AutoTextEntries;
object indexitem = 26; //page x of y
docAutotextEntry = docAutotextEntries.get_Item(ref indexitem);

object dirstart = Word.WdCollapseDirection.wdCollapseEnd;
object richtext = true;

wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.InsertAfter("\r");
wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Collapse(ref dirstart);
docAutotextEntry.Insert(wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range, ref richtext);


I've also tried using the range.fields.add method, instead of the above, and the same thing happens.

Any ideas would be greatfully received! Thanks.
 
I guess nobody has any advice on this then? I've tried many different methods but none seems to work. Regardless of my own code listed below all I am really looking how to do is update a Microsoft Word footer in C#.

Just concatenating text onto the section.range.text property does not work properly, as the text formatting, alignment etc is lost.
 
Hi Mark,
im trying to solve same problem without any success. Do you have anything new to this matter? Or anyone else? :)

Thanks a lot

Greetings
 
shot in the dark (I've never done anything like this).... figure out how a VBA Macro would do it, then port to c#.

VBA is like programming lite so if you can do it with VBA, you should defantly be able to minipulate it via c#.

another thought (depending on the version of MS Word) open the file, save as a Word/XML file. append the proper XML nodes to the document. Saves as, Word .doc.

this link points to a tool that generates Excel workbooks via XML. I think the concept is similar with Word.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
HI Mark,

I spent a few hours rack'n my brain trying to solve my own Footer issue, which is somewhat related to yours.

You mentioned the fact that you tried the Fields.Add() method.... That is the method I am using and I am able to modify any Footer in any section.

It's really quite simple...
foreach through the fields looking for the Field.Type
Once you find the field your looking for, you can update the text for that field ex: Field.Result.Text +=


foreach (Word.Field f in section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Fields)
{
if (f.Type == WdFieldType.wdFieldAutoText && f.Result.Text.StartsWith("Page"))
{
f.Result.Text += "- some other text";
}
}

Let me know if this helps or not.
 
Thanks for your response. The code that you suggest is (I think..) for adding text to a footer AFTER you have entered your autotext "page x of y" entry.

What I require is to open a Word document and (amongst other things), add the autotext entry to the footer, leaving all other text the same. The field.add method seems to overwrite any existing text in the footer.

I have tried concatenating the text back in after the autotext field has been added but this means that the original formatting alignment etc is not retained.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top