Is there any way to get list of all style names contained in word file? I am using Spire.Doc an I know how get all used styles, but I need all available style names.
Check that:
object missing = System.Reflection.Missing.Value;
object fileName = "C:\\test.docx";
Microsoft.Office.Interop.Word.Application applicationWord = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document documentWord = new Microsoft.Office.Interop.Word.Document();
documentWord = applicationWord.Documents.Open(ref fileName, 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);
foreach (Microsoft.Office.Interop.Word.Style currentStyle in documentWord.Styles)
{
string name = currentStyle.NameLocal;
}
Related
I'm using .net .4.6.1 and trying to do a mail merge. When i run the following code, which has a valid path to a word (.doc) document, it returns null.
C#
object fileName = pathToDocument;
doc = word.Documents.Open(ref fileName,
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);
Any advice appreciated.
Thanks,
Chris
The signature of the Open method is really confusing but it can be treated with more of a common sense approach, try something like this adjusting the file path of your document ...
static void Main(string[] args)
{
var fileName = "c:\\temp\\Test.docx";
var wdApplication = new Microsoft.Office.Interop.Word.Application();
var wdDocument = wdApplication.Documents.Open(fileName);
Console.WriteLine(wdDocument.Words.Count);
wdDocument.Close();
wdApplication.Quit();
}
Word deals with local files only. You need to copy the file locally and then use the Word object model to open it.
The Documents.Open method doesn't require the ref keyword for passing parameters:
doc = word.Documents.Open(pathToDocument,
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);
I'm using office 2013, and I used the code below to open a word document:
object fileName = FD.FileName;
object readOnly = false;
object isVisible = true;
WordApp.Visible = true;
aDoc = WordApp.Documents.Open(ref fileName, ref missing,
ref readOnly, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref isVisible, ref missing, ref missing,
ref missing, ref missing);
aDoc.Activate();
How can I enable editing for some read only word files opened in my c# application?
Actually it's nothing to do with Office-interop, ReadOnly is a file attribute of that file. You could remove this by setting its FileAttribute to FileAttributes.Normal before you open the file.
You can try the code below:
string fileName = FD.FileName;
File.SetAttributes(fileName, FileAttributes.Normal);
aDoc = WordApp.Documents.Open(fileName, Visible: isVisible);
aDoc.Activate();
Remember, if you want to set it back to ReadOnly after you close the file, add the line below after you call aDoc.Close():
File.SetAttributes(fileName, FileAttributes.ReadOnly);
I'm trying to print a MS Word document from manual feed (in order to print to envelopes)
I found out that the Envelope.PrintOut method has a FeedSource property that suppose to help define this,
http://msdn.microsoft.com/en-us/library/office/ff197594.aspx
but i am having a hard time to use that...
Here is a function i am using to print a document that works, if fromFeed is false (a regular print) but not if its true...
of course i tried different types of WdPaperTray values...
public void PrintDocument(bool fromFeed)
{
this.doc = this.ap.Documents.Open(this.pathToNewFile, ReadOnly: false, Visible: false);
//if (fromFeed)
// this.doc.Envelope.FeedSource = WdPaperTray.wdPrinterManualFeed;
object copies = "1";
object pages = "";
object range = WdPrintOutRange.wdPrintAllDocument;
object items = WdPrintOutItem.wdPrintDocumentContent;
object pageType = WdPrintOutPages.wdPrintAllPages;
object oTrue = true;
object oFalse = false;
object missing = System.Type.Missing;
this.doc.Activate();
if (fromFeed)
this.doc.Envelope.PrintOut(missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, WdPaperTray.wdPrinterManualFeed, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
else
this.doc.PrintOut(ref oTrue, ref oFalse, ref range, ref missing, ref missing, ref missing,
ref items, ref copies, ref pages, ref pageType, ref oFalse, ref oTrue,
ref missing, ref oFalse, ref missing, ref missing, ref missing, ref missing);
this.ap.Documents.Close(SaveChanges: false, OriginalFormat: false, RouteDocument: false);
}
than i found this :
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.envelope.feedsource(v=office.11).aspx
and it says :
"If you use this property before an envelope has been added to the document, an error occurs"
So maybe i just need to add an envelope to the document ? but what is an envelope in a MS Word document ?
Btw I'm developing it as WPF.
and at this point the code throws this exception:object values out of range exception
Any help will be most welcomed.
I am wanting to replace all the substitue code for paragraph markers (^13) with the normal paragraph marker code ^p in a Microsft Word document, using C# and Interop.
I am have been using the Microsoft.Office.Interop.Word.Selection.Find.Execute() method.
For example..
.Application.ActiveWindow.Selection.Find.Execute(
ref findText,
ref matchCase,
ref matchWholeWord,
ref matchWildcards,
ref matchSoundsLike,
ref matchAllWordForms,
ref findForward,
ref findWrap,
ref findFormat,
ref replaceText,
ref replaceAll,
ref missing,
ref missing,
ref missing,
ref missing);
findText = "^13"
matchCase = true
matchWholeWord = true
matchWildcards = true
matchSoundsLike = false
matchAllWordForms = false
findForward = true
findWrap = WdFindWrap.wdFindContinue
findFormat = false
replaceText = "^p"
replaceAll = WdReplace.wdReplaceAll
Using the code above, the ^13 markers are not being replaced by ^p markers.
Does anyone know how I can rectify this?
check my codes below:
// Create the Word application and declare a document
Word.Application word = new Word.Application();
Word.Document doc = new Word.Document();
// Define an object to pass to the API for missing parameters
object missing = System.Type.Missing;
try
{
// Everything that goes to the interop must be an object
object fileName = #"D:\test.docx";
// Open the Word document.
// Pass the "missing" object defined above to all optional
// parameters. All parameters must be of type object,
// and passed by reference.
doc = word.Documents.Open(ref fileName,
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);
// Activate the document
doc.Activate();
// Loop through the StoryRanges (sections of the Word doc)
foreach (Word.Range tmpRange in doc.StoryRanges)
{
// Set the text to find and replace
tmpRange.Find.Text = "xml";
tmpRange.Find.Replacement.Text = "XML";
// Set the Find.Wrap property to continue (so it doesn't
// prompt the user or stop when it hits the end of
// the section)
tmpRange.Find.Wrap = Word.WdFindWrap.wdFindContinue;
// Declare an object to pass as a parameter that sets
// the Replace parameter to the "wdReplaceAll" enum
object replaceAll = Word.WdReplace.wdReplaceAll;
// Execute the Find and Replace -- notice that the
// 11th parameter is the "replaceAll" enum object
tmpRange.Find.Execute(ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref replaceAll,
ref missing, ref missing, ref missing, ref missing);
}
// Save the changes
doc.Save();
// Close the doc and exit the app
doc.Close(ref missing, ref missing, ref missing);
word.Application.Quit(ref missing, ref missing, ref missing);
}
catch (Exception ex)
{
doc.Close(ref missing, ref missing, ref missing);
word.Application.Quit(ref missing, ref missing, ref missing);
System.Diagnostics.Process.Start("D:\\test.docx");
}
One more thing: Note here: using Word = Microsoft.Office.Interop.Word;
If i'm not mistake you can't call find and replace for paragraph but you can change style of them paragraph
I'd like to change the template from a lot of Word-documents using a simple C#-program.
This documents are all based on a standard template for title styles, font, etc... We would like to change this template (more specifically: the title colors and other small things) and modify the current documents to use this new template.
In Word, this is easily achieved by clicking "Document Template" in the Designer tab in the ribbon. I used this guide to do this. This works beautifully and does exactly what it should do: change the title colors etc according to the new template.
So the question is simple: how do I do the exact same thing (attach other template and change styles) from within a .NET-application?
I guess I should use the Microsoft.Office.Interop.Word namespace, but I'm stuck there...
I managed to solve it by myself, wasn't that difficult apparantly. This is the code I used:
object missing = System.Reflection.Missing.Value;
Word.Application wordApp = new Word.ApplicationClass();
Word.Document aDoc = null;
object readOnly = false;
object isVisible = false;
wordApp.Visible = false;
object filename = "d:\\Testdocs\\testfile.doc";
object saveAs = "d:\\Testdocs\\output.doc";
object oTemplate = "d:\\Testdocs\\Template.dotx";
aDoc = wordApp.Documents.Add(ref oTemplate, ref missing,
ref missing, ref missing);
aDoc = wordApp.Documents.Open(ref filename, ref missing,
ref readOnly, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref isVisible, ref missing, ref missing,
ref missing, ref missing);
aDoc.Activate();
aDoc.set_AttachedTemplate(oTemplate);
aDoc.UpdateStyles();
aDoc.SaveAs(ref saveAs, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing);
aDoc.Close(ref missing, ref missing, ref missing);