How could I insert a formula in a Word document header? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How do you display code snippets in MS Word preserving format and syntax highlighting?Add an Image to Word Document and Scale it using VBAHow do I remedy the “The breakpoint will not currently be hit. No symbols have been loaded for this document.” warning?Tag spot in word documentMicrosoft Dynamics CRM - Insert Fields into Word DocumentWhy not inherit from List<T>?how to insert numbering that is linked to styles ms word c#Get pages of word documentCounting words in Word document, including footnoresOR Formula in Word document not returning a value
How to tell that you are a giant?
Do square wave exist?
Extracting terms with certain heads in a function
Compare a given version number in the form major.minor.build.patch and see if one is less than the other
How to answer "Have you ever been terminated?"
How do I find out the mythology and history of my Fortress?
Is there any way for the UK Prime Minister to make a motion directly dependent on Government confidence?
Withdrew £2800, but only £2000 shows as withdrawn on online banking; what are my obligations?
How do I make this wiring inside cabinet safer? (Pic)
How would a mousetrap for use in space work?
Denied boarding although I have proper visa and documentation. To whom should I make a complaint?
What would be the ideal power source for a cybernetic eye?
Fantasy story; one type of magic grows in power with use, but the more powerful they are, they more they are drawn to travel to their source
What does the "x" in "x86" represent?
2001: A Space Odyssey's use of the song "Daisy Bell" (Bicycle Built for Two); life imitates art or vice-versa?
Is there a kind of relay only consumes power when switching?
What does "lightly crushed" mean for cardamon pods?
First console to have temporary backward compatibility
What do you call the main part of a joke?
Fundamental Solution of the Pell Equation
Can a party unilaterally change candidates in preparation for a General election?
Do I really need to have a message in a novel to appeal to readers?
Significance of Cersei's obsession with elephants?
Chinese Seal on silk painting - what does it mean?
How could I insert a formula in a Word document header?
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How do you display code snippets in MS Word preserving format and syntax highlighting?Add an Image to Word Document and Scale it using VBAHow do I remedy the “The breakpoint will not currently be hit. No symbols have been loaded for this document.” warning?Tag spot in word documentMicrosoft Dynamics CRM - Insert Fields into Word DocumentWhy not inherit from List<T>?how to insert numbering that is linked to styles ms word c#Get pages of word documentCounting words in Word document, including footnoresOR Formula in Word document not returning a value
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm trying to add a formula/expression in my Word document programatically. In my scenario I need to get the number of pages minus 1. If you try to do it in the document itself it should look like this:
=NUMPAGES *MERGEFORMAT -1
Now that I'm trying to do it via code I'm struggling... I've tried to do this:
RangeWord.Fields.Add(this.Range, Interop.WdFieldType.wdFieldExpression, "NUMPAGES *MERGEFORMAT -1", true)
But in that case NUMPAGES *MERGEFORMAT
doesn't work as a field but as a plain text. How could I achieve what I want ? Should I add a new field for NUMPAGES *MERGEFORMAT
?
c# ms-word interop
add a comment |
I'm trying to add a formula/expression in my Word document programatically. In my scenario I need to get the number of pages minus 1. If you try to do it in the document itself it should look like this:
=NUMPAGES *MERGEFORMAT -1
Now that I'm trying to do it via code I'm struggling... I've tried to do this:
RangeWord.Fields.Add(this.Range, Interop.WdFieldType.wdFieldExpression, "NUMPAGES *MERGEFORMAT -1", true)
But in that case NUMPAGES *MERGEFORMAT
doesn't work as a field but as a plain text. How could I achieve what I want ? Should I add a new field for NUMPAGES *MERGEFORMAT
?
c# ms-word interop
add a comment |
I'm trying to add a formula/expression in my Word document programatically. In my scenario I need to get the number of pages minus 1. If you try to do it in the document itself it should look like this:
=NUMPAGES *MERGEFORMAT -1
Now that I'm trying to do it via code I'm struggling... I've tried to do this:
RangeWord.Fields.Add(this.Range, Interop.WdFieldType.wdFieldExpression, "NUMPAGES *MERGEFORMAT -1", true)
But in that case NUMPAGES *MERGEFORMAT
doesn't work as a field but as a plain text. How could I achieve what I want ? Should I add a new field for NUMPAGES *MERGEFORMAT
?
c# ms-word interop
I'm trying to add a formula/expression in my Word document programatically. In my scenario I need to get the number of pages minus 1. If you try to do it in the document itself it should look like this:
=NUMPAGES *MERGEFORMAT -1
Now that I'm trying to do it via code I'm struggling... I've tried to do this:
RangeWord.Fields.Add(this.Range, Interop.WdFieldType.wdFieldExpression, "NUMPAGES *MERGEFORMAT -1", true)
But in that case NUMPAGES *MERGEFORMAT
doesn't work as a field but as a plain text. How could I achieve what I want ? Should I add a new field for NUMPAGES *MERGEFORMAT
?
c# ms-word interop
c# ms-word interop
edited Mar 22 at 21:35
Cindy Meister
16.1k102537
16.1k102537
asked Mar 22 at 9:44
Juan Carlos RodriguezJuan Carlos Rodriguez
664323
664323
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I don't think it will work this way. Entering braces into code does not work in any way.
This, however, should do the trick just fine:
doc.Variables.Add("myNumPages", doc.ComputeStatistics(Interop.WdStatistic.wdStatisticPages) - 1);
RangeWord.Fields.Add(this.Range, Interop.WdFieldType.wdFieldDocVariable, "myNumPages");
.
Edit/AddIt: There is another way, with the advantage of then having a dynamic field that can be updated:
- Insert a normal
NUMPAGES
field somewhere and give it a white font or make it hidden - Select the field, add a bookmark "numpages" covering this entire field
- Now add a Formula field with the formula
=numpages-1
Example:
var hidField = RangeWord.Fields.Add(this.Range, Interop.WdFieldType.wdFieldExpression, "NUMPAGES");
hidField.Result.Bookmarks.Add("numpages");
hidField.Result.Font.Hidden = 1;
RangeWord.Fields.Add(this.Range, Interop.WdFieldType.wdFieldFormula, "numpages-1");
You might have to play around with the range variable so as to not overwrite your hidden field.
I know the next question may seem offtopic but it's something I need to know... AreVariables
references ? I mean if I decide to change my "myNumPages" variable (if it could be done), will it update all the fields which contain that variable?
– Juan Carlos Rodriguez
Mar 22 at 11:51
You would have to recalculate the variable. I have just tried adding a few pages and then updating the field - no change. i.e. you must reassign the value to the variable, since it is static.
– LocEngineer
Mar 22 at 12:04
@JuanCarlosRodriguez I have amended the code. The second version should be the way to go for you.
– LocEngineer
Mar 22 at 13:50
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55296822%2fhow-could-i-insert-a-formula-in-a-word-document-header%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I don't think it will work this way. Entering braces into code does not work in any way.
This, however, should do the trick just fine:
doc.Variables.Add("myNumPages", doc.ComputeStatistics(Interop.WdStatistic.wdStatisticPages) - 1);
RangeWord.Fields.Add(this.Range, Interop.WdFieldType.wdFieldDocVariable, "myNumPages");
.
Edit/AddIt: There is another way, with the advantage of then having a dynamic field that can be updated:
- Insert a normal
NUMPAGES
field somewhere and give it a white font or make it hidden - Select the field, add a bookmark "numpages" covering this entire field
- Now add a Formula field with the formula
=numpages-1
Example:
var hidField = RangeWord.Fields.Add(this.Range, Interop.WdFieldType.wdFieldExpression, "NUMPAGES");
hidField.Result.Bookmarks.Add("numpages");
hidField.Result.Font.Hidden = 1;
RangeWord.Fields.Add(this.Range, Interop.WdFieldType.wdFieldFormula, "numpages-1");
You might have to play around with the range variable so as to not overwrite your hidden field.
I know the next question may seem offtopic but it's something I need to know... AreVariables
references ? I mean if I decide to change my "myNumPages" variable (if it could be done), will it update all the fields which contain that variable?
– Juan Carlos Rodriguez
Mar 22 at 11:51
You would have to recalculate the variable. I have just tried adding a few pages and then updating the field - no change. i.e. you must reassign the value to the variable, since it is static.
– LocEngineer
Mar 22 at 12:04
@JuanCarlosRodriguez I have amended the code. The second version should be the way to go for you.
– LocEngineer
Mar 22 at 13:50
add a comment |
I don't think it will work this way. Entering braces into code does not work in any way.
This, however, should do the trick just fine:
doc.Variables.Add("myNumPages", doc.ComputeStatistics(Interop.WdStatistic.wdStatisticPages) - 1);
RangeWord.Fields.Add(this.Range, Interop.WdFieldType.wdFieldDocVariable, "myNumPages");
.
Edit/AddIt: There is another way, with the advantage of then having a dynamic field that can be updated:
- Insert a normal
NUMPAGES
field somewhere and give it a white font or make it hidden - Select the field, add a bookmark "numpages" covering this entire field
- Now add a Formula field with the formula
=numpages-1
Example:
var hidField = RangeWord.Fields.Add(this.Range, Interop.WdFieldType.wdFieldExpression, "NUMPAGES");
hidField.Result.Bookmarks.Add("numpages");
hidField.Result.Font.Hidden = 1;
RangeWord.Fields.Add(this.Range, Interop.WdFieldType.wdFieldFormula, "numpages-1");
You might have to play around with the range variable so as to not overwrite your hidden field.
I know the next question may seem offtopic but it's something I need to know... AreVariables
references ? I mean if I decide to change my "myNumPages" variable (if it could be done), will it update all the fields which contain that variable?
– Juan Carlos Rodriguez
Mar 22 at 11:51
You would have to recalculate the variable. I have just tried adding a few pages and then updating the field - no change. i.e. you must reassign the value to the variable, since it is static.
– LocEngineer
Mar 22 at 12:04
@JuanCarlosRodriguez I have amended the code. The second version should be the way to go for you.
– LocEngineer
Mar 22 at 13:50
add a comment |
I don't think it will work this way. Entering braces into code does not work in any way.
This, however, should do the trick just fine:
doc.Variables.Add("myNumPages", doc.ComputeStatistics(Interop.WdStatistic.wdStatisticPages) - 1);
RangeWord.Fields.Add(this.Range, Interop.WdFieldType.wdFieldDocVariable, "myNumPages");
.
Edit/AddIt: There is another way, with the advantage of then having a dynamic field that can be updated:
- Insert a normal
NUMPAGES
field somewhere and give it a white font or make it hidden - Select the field, add a bookmark "numpages" covering this entire field
- Now add a Formula field with the formula
=numpages-1
Example:
var hidField = RangeWord.Fields.Add(this.Range, Interop.WdFieldType.wdFieldExpression, "NUMPAGES");
hidField.Result.Bookmarks.Add("numpages");
hidField.Result.Font.Hidden = 1;
RangeWord.Fields.Add(this.Range, Interop.WdFieldType.wdFieldFormula, "numpages-1");
You might have to play around with the range variable so as to not overwrite your hidden field.
I don't think it will work this way. Entering braces into code does not work in any way.
This, however, should do the trick just fine:
doc.Variables.Add("myNumPages", doc.ComputeStatistics(Interop.WdStatistic.wdStatisticPages) - 1);
RangeWord.Fields.Add(this.Range, Interop.WdFieldType.wdFieldDocVariable, "myNumPages");
.
Edit/AddIt: There is another way, with the advantage of then having a dynamic field that can be updated:
- Insert a normal
NUMPAGES
field somewhere and give it a white font or make it hidden - Select the field, add a bookmark "numpages" covering this entire field
- Now add a Formula field with the formula
=numpages-1
Example:
var hidField = RangeWord.Fields.Add(this.Range, Interop.WdFieldType.wdFieldExpression, "NUMPAGES");
hidField.Result.Bookmarks.Add("numpages");
hidField.Result.Font.Hidden = 1;
RangeWord.Fields.Add(this.Range, Interop.WdFieldType.wdFieldFormula, "numpages-1");
You might have to play around with the range variable so as to not overwrite your hidden field.
edited Mar 22 at 13:49
answered Mar 22 at 11:23
LocEngineerLocEngineer
2,31211023
2,31211023
I know the next question may seem offtopic but it's something I need to know... AreVariables
references ? I mean if I decide to change my "myNumPages" variable (if it could be done), will it update all the fields which contain that variable?
– Juan Carlos Rodriguez
Mar 22 at 11:51
You would have to recalculate the variable. I have just tried adding a few pages and then updating the field - no change. i.e. you must reassign the value to the variable, since it is static.
– LocEngineer
Mar 22 at 12:04
@JuanCarlosRodriguez I have amended the code. The second version should be the way to go for you.
– LocEngineer
Mar 22 at 13:50
add a comment |
I know the next question may seem offtopic but it's something I need to know... AreVariables
references ? I mean if I decide to change my "myNumPages" variable (if it could be done), will it update all the fields which contain that variable?
– Juan Carlos Rodriguez
Mar 22 at 11:51
You would have to recalculate the variable. I have just tried adding a few pages and then updating the field - no change. i.e. you must reassign the value to the variable, since it is static.
– LocEngineer
Mar 22 at 12:04
@JuanCarlosRodriguez I have amended the code. The second version should be the way to go for you.
– LocEngineer
Mar 22 at 13:50
I know the next question may seem offtopic but it's something I need to know... Are
Variables
references ? I mean if I decide to change my "myNumPages" variable (if it could be done), will it update all the fields which contain that variable?– Juan Carlos Rodriguez
Mar 22 at 11:51
I know the next question may seem offtopic but it's something I need to know... Are
Variables
references ? I mean if I decide to change my "myNumPages" variable (if it could be done), will it update all the fields which contain that variable?– Juan Carlos Rodriguez
Mar 22 at 11:51
You would have to recalculate the variable. I have just tried adding a few pages and then updating the field - no change. i.e. you must reassign the value to the variable, since it is static.
– LocEngineer
Mar 22 at 12:04
You would have to recalculate the variable. I have just tried adding a few pages and then updating the field - no change. i.e. you must reassign the value to the variable, since it is static.
– LocEngineer
Mar 22 at 12:04
@JuanCarlosRodriguez I have amended the code. The second version should be the way to go for you.
– LocEngineer
Mar 22 at 13:50
@JuanCarlosRodriguez I have amended the code. The second version should be the way to go for you.
– LocEngineer
Mar 22 at 13:50
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55296822%2fhow-could-i-insert-a-formula-in-a-word-document-header%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown