Created
February 2, 2019 09:04
-
-
Save jedjohan/fb121ca43bb83dd76b0c1f03804c0335 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using Microsoft.Office.Interop.Word; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
List<string> path = new List<string>(args); | |
string filePath = string.Join(" ", path.ToArray()); | |
string folderPath = Path.GetDirectoryName(filePath); | |
try | |
{ | |
Application ap = new Application(); | |
Document document = ap.Documents.Open(filePath); | |
document.Fields.Update(); | |
foreach (Section section in document.Sections) | |
{ | |
document.TrackRevisions = false; | |
HeadersFooters headers = section.Headers; //Get all headers | |
foreach (HeaderFooter header in headers) | |
{ | |
Fields fields = header.Range.Fields; | |
foreach (Field field in fields) | |
{ | |
// Här har du fälten i headern. Kolla om det är den du vill uppdatera. Tex | |
if (field.Type == Microsoft.Office.Interop.Word.WdFieldType.wdFieldUserName) | |
{ | |
field.Select(); | |
field.Delete(); | |
document.Selection.TypeText("Johan Eriksson"); | |
} | |
// Osv med uppdateringar | |
field.Update(); | |
} | |
} | |
} | |
document.Save(); | |
document.Close(); | |
} | |
catch (NullReferenceException) | |
{ | |
System.Windows.Forms.MessageBox.Show("A valid file was not selected."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment