Reputation: 181
I have an application that will print a .docx file, which has a Text Box shape with text. In it, there are certain words that need to be bold.
For example, a part of the text is "...representando a empresa M&A, o presente..." and only "M&A" needs to be bold.
However, I don't know for sure how I can do this. I've searched in SO, as well as other sites like MSDN and such, but none provide the solution. Could anyone help me?
EDIT: I'm using Interop to achieve this. The code I've done so far is as follows:
// opens word app
wordApp = new Microsoft.Office.Interop.Word.Application();
wordApp.Visible = false;
// print dialog for settings input
PrintDialog pd = new PrintDialog();
if (pd.ShowDialog() == DialogResult.OK)
{
wordApp.ActivePrinter = pd.PrinterSettings.PrinterName;
// opens the document
Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(filePath);
// iterates through each shape in the file
foreach (Microsoft.Office.Interop.Word.Shape shape in doc.Shapes)
{
string shapeName = shape.Name;
// checks whether the shape is a Text Box
if (shapeName.StartsWith("Text Box"))
{
string shapeText = shape.TextFrame.ContainingRange.Text;
// checks whether the shape is the one I want to modify
// side note: there are more shapes in the file, so I specify it
if (shapeText.StartsWith("Concedemos"))
{
// erases the text, and sets it to a string
shape.TextFrame.ContainingRange.Text = "";
shape.TextFrame.ContainingRange.Text = "textSample";
}
}
}
// prints the file
wordApp.ActiveDocument.PrintOut();
}
// quits the app
wordApp.Quit();
wordApp = null;
Thanks in advance!
Upvotes: 0
Views: 984
Reputation: 25693
The code extract below demonstrates how to loop through all Shapes in a Word document, check whether it's a text box (drawing element) that begins with the desired string, then search the text box for the term to be bolded and bold it.
It's possible to check the Shape.Type
to determine whether or not it's a text box. The Shape.Type
is an Office Enum (since the Shape
object is common to many Office applications). So if (shapeType == Office.MsoShapeType.msoTextBox)
To pick out text and format it, most often Word's Range.Find
functionality is the best approach. It enables to find and replace formatting, as well as string values. (Tip: When embarking on a project that requires this kind of thing it's often a good idea to test in the dialog box (Ctrl+H) in the Word UI to figure out the right combination of parameters.)
To understand what all the parameters of Find.Execute
are for, consult the Help or look at Intellisense (or a combination of the two).
For this example, it's important to note that, in order to format the target text no Replacement.Text
is specified.
foreach (Microsoft.Office.Interop.Word.Shape shape in doc.Shapes)
{
Office.MsoShapeType shapeType = shape.Type;
// checks whether the shape is a Text Box
if (shapeType == Office.MsoShapeType.msoTextBox)
{
// checks whether the shape is the one I want to modify
// side note: there are more shapes in the file, so I specify it
if (shapeText.StartsWith("Concedemos"))
{
string textToBold = "M&A";
Word.Range rngTextBox = shape.TextFrame.TextRange;
Word.Find rngFind = rngTextBox.Find;
rngFind.Text = textToBold;
rngFind.Replacement.Font.Bold = -1;
object oFindStop = Word.WdFindWrap.wdFindStop;
object oTrue = true;
object oReplaceAll = Word.WdReplace.wdReplaceAll;
object missing = System.Type.Missing;
rngFind.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref oFindStop, ref oTrue, ref missing, ref oReplaceAll,
ref missing, ref missing, ref missing, ref missing);
}
}
Upvotes: 1