var intFound = 0;
if (searchRange.Execute(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))
while (rng.Find.Found)
{
intFound++;
rng.Font.Color = Word.WdColor.wdColorBlue;
}
In the above code, if I add any other actions like add an index field or a messagebox.show makes the loop goes endless. Can any one help me understand where am I going wrong?
Related
I have a code that opens up a word document, reads an excel file as data source for mail merge fields, and then saves the document.
The problem: I want the text in the merge field to be shown (and saved) as "previewed". (e.g. the field shows its value lets say "Dog" instead of its field title "Animal"). In the Word application i would simply click the button "Preview Result".
By recording a macro I have found that this can be solved in Visual Basic by the line ActiveDocument.MailMerge.ViewMailMergeFieldCodes = wdToggle
although I have not managed to use this in the C#-code.
I am new in both coding and this forum, so please let me know if I can provide more information.
using System;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using Word = Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
using Microsoft.Office.Core;
namespace word
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void CreateWordDocument(object wordFilename, object excelSource)
{
Word.Application wordApp = new Word.Application();
object missing = Missing.Value;
Word.Document myWordDoc = null;
object readOnly = false;
object isVisible = false;
wordApp.Visible = true;
myWordDoc = wordApp.Documents.Open(ref wordFilename, ref missing, ref readOnly,
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);
myWordDoc.Activate();
myWordDoc.MailMerge.MainDocumentType =
Word.WdMailMergeMainDocType.wdNotAMergeDocument;
myWordDoc.MailMerge.OpenDataSource(Name: excelSource, Format: ref missing,
SQLStatement: "SELECT*FROM`Data$`");
//Any code for previewing
//VB-code: ActiveDocument.MailMerge.ViewMailMergeFieldCodes = wdToggle
myWordDoc.SaveAs2(ref wordFilename, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref mailmerge, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing);
myWordDoc.Close();
wordApp.Quit();
}
}
The property to use is:
myWordDoc.ActiveWindow.View.MailMergeDataView = true;
Read more here:
https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word.view.mailmergedataview?view=word-pia
I am trying to get bullet list from docx using C#, however, instead of bullet number or symbol, the output gets a weird symbol or rectangle with a question mark inside "", instead of a bullet. Following is the code snippet I am using to get bullet value, but it is not working:
Application word = new Application ();
Document doc = new Document ();
object fileName = #"D:\testing\Sample_2.docx";
// Define an object to pass to the API for missing parameters
object missing = System.Type.Missing;
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);
for (int i = 0; i < doc.Paragraphs.Count; i++) {
string bullet = doc.Paragraphs[i + 1].Range.ListFormat.ListString;
}
((_Document) doc).Close ();
((_Application) word).Quit ();
How can I do a find and replace using C# in a word doc? For example, let's replace all instances of the word "Run" with "Ran"?
Maybe something like a String.Replace("run", "ran"); but that executed okay, but no changes were made.
A Find and Replace method :
public void FindAndReplaceWordText(ref Microsoft.Office.Interop.Word.Document wordDoc, string textToReplace, string newText)
{
object missing = System.Type.Missing;
foreach (Microsoft.Office.Interop.Word.Range tmpRange in wordDoc.StoryRanges)
{
//set the text to find and replace
tmpRange.Find.Text = textToReplace;
tmpRange.Find.Replacement.Text = newText;
tmpRange.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
//Declare an object to pass as a parameter that sets the Replace parameter to the "wdReplaceOne" enum.
object replaceOne = Microsoft.Office.Interop.Word.WdReplace.wdReplaceOne;
//Execute the Find and Replace
tmpRange.Find.Execute(ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref replaceOne,
ref missing, ref missing, ref missing, ref missing);
}
Invoking the method:
FindAndReplaceWordText(ref wordDoc, "Ran", "Run");
Hope this helps!
I have a Word document which I want to attach an existing template to.
In order the template styles will be set as the document styles I have to update the styles.
All templates definitions are successfully updated except the footer of the template.
//Opening the document to Word application
Microsoft.Office.Interop.Word.Document wordDoc = null;
Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
object oFileName= MyDocumentPath;
wordDoc= wordApp.Documents.Open(ref oFileName,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);
//Use an existing template
object oTemplate = (object)coverPagePath;
wordApp.ActiveDocument.set_AttachedTemplate(ref oTemplate);
wordApp.ActiveDocument.UpdateStyles();
Any ideas?
I'm using word automation to generate a docx file, then print it out. Here is my basic code. I found that I have to wait some time after adoc.PrintOut, otherwise, the print may fail. It seems it is because I close the file after that. Before the file is closed, word does not have time to print it out yet. So, basically, adoc.PrintOut is non-blocked (this may not be the correct term). Right now, I wait for 10 seconds, it works fine. However, does it have a more elegant solution for this? I mean, how can wait until PrintOut finished, then close the doc?
thanks
object yes = true;
object no = false;
object missing = System.Reflection.Missing.Value;
Word.Application wordApp = new Word.Application();
object fileName = originalFileName;
Word.Document adoc = null;
try
{
adoc = wordApp.Documents.Open(ref fileName,
ref missing, ref no, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref yes, ref missing, ref missing, ref missing, ref missing);
adoc.Activate();
/*
some other processing
*/
object sFile = fileName;
adoc.PrintOut(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, ref missing, ref missing, ref missing);
// http://msdn.microsoft.com/en-us/library/b9f0ke7y
//adoc.PrintPreview();
for (int i = 0; i < 10; i++)
{
Thread.Sleep(TimeSpan.FromSeconds(1));
Application.DoEvents();
}
//adoc.Save();
object savechanges = Word.WdSaveOptions.wdDoNotSaveChanges; //.wdSaveChanges;
object originalFormat = Word.WdOriginalFormat.wdWordDocument;
object routeDocument = missing; // Type.Missing; // true;
((Word._Document)adoc).Close(ref savechanges, ref originalFormat, ref routeDocument);
((Word._Application)wordApp).Quit(ref savechanges, ref missing, ref missing);
adoc = null;
wordApp = null;
}
catch
{
}
finally
{
/*
some dispose work
*/
// do it twice to release all memory
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
You can use the first parameter of PrintOut.
object background = false;
adoc.PrintOut(background, 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, ref missing,
ref missing);
As the documentation says:
http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.printout(v=vs.80).aspx
"Background
true to have the customization code continue while Microsoft Office Word prints the document."