How do I open the Header/Footer in a Word programatically from MATLAB?

I have code to open up a word document and edit the body of the document; however I don't know how to open/edit the header/footer. Does anyone know how to do this?

2 Comments

Does this question have any connection to Matlab? If so, please explain any details.
Yes it does, currently have an M-File which opens up a word document to edit the body of the active document; however, I am unsure as to how I can access the header/footer. Fangjun Jiang's answer below is getting me on the right track, but I am unsure as to how I need to define the Header/Footer(Index)

Sign in to comment.

 Accepted Answer

I assume you want to access the header/footer in a Word document through Matlab m-script.
You can find these info in the Word help document, Microsoft Word Visual Basic Reference.
Here is the text regarding the Herder/Footer object:
Using the HeaderFooter Object Use Headers(index) or Footers(index), where index is one of the WdHeaderFooterIndex constants (wdHeaderFooterEvenPages, wdHeaderFooterFirstPage, or wdHeaderFooterPrimary), to return a single HeaderFooter object. The following example changes the text of both the primary header and the primary footer in the first section of the active document.
With ActiveDocument.Sections(1) .Headers(wdHeaderFooterPrimary).Range.Text = "Header text" .Footers(wdHeaderFooterPrimary).Range.Text = "Footer text" End With

7 Comments

I have looked through the word help document and I did see the above text regarding the Header/Footer object; however, I am not sure how to define the Object Headers/Footers(index) within the M-file. If I were to just use what you have above I would get the following error: "Undefined function or variable 'wdHeaderFooterPrimary'". Do I need to use a GoTo Function?
Yes, it is sometimes very hard to figure out the exact syntax. Use the following as a starting poitn:
hApp=actxserver('Word.application');
set(hApp,'Visible',true);
hDoc=hApp.Documents.Add;
hSections=hDoc.Sections.Add;
hSections.Headers.Item(1).Range.Text='my headers';
hSections.Footers.Item(1).Range.Text='my footers';
This definitely works out. I appreciate your help. I was wondering if you were just wanting to open the header and search for text within the header, how would that affect the above syntax? For Example: "Date:" is already contained in header, now I must add the current date after the above text to then read "Date: Current Date"
You can get the existing header text and then do whatever string process.
OldHeader=hSections.Headers.Item(1).Range.Text;
Yea, I thought of trying to assign the old header to a variable and then editing from there; however, I am having to deal with Cells within the header. The Cells aren't preserved when you assign the old header to a variable. I was thinking of a way to just either open the header ONLY and then locate the cell I am trying to edit or to redefine what the range is. What do you think would be the easiest method. I would have thought just opening the header, but I am not sure if there is really a method of JUST opening the header and then searching...
Try this:
R1=hSections.Headers.Item(1).Range;
get(R1)
I see there is a property called "Cells".
The point is that you probably need to drill down the structure of objects and properties of the Word application and find the right object and property to modify.
How to extract header and footer for scanned documents? Any idea? please suggest.

Sign in to comment.

More Answers (1)

I am trying to use this code, but there are some issues.
I have an existing word template which I want to reuse. It has already an header inside it.
When coding hSections=hDoc.Sections.Add; I get an unwanted new section into my word file.
Without it Headers is an unrecognized funciton or variable.
Maybe you can help on how to get access to an existing header without changing the rest of the word file?

Products

Asked:

Joe
on 7 Feb 2011

Edited:

on 14 Oct 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!