Finding & Replacing text in a MS Word textbox - c#

I am trying to replace the temporary text in a word document with new text from a list. It works if the text is not in a shape, but once it tries to find the text in a textbox it throws an error. Here is what I have so far:
public void FindReplace(List<repvals> replaceVals, string docLocation, int listLen)
{
//Opens a new Word application
var app = new Microsoft.Office.Interop.Word.Application();
//Opens the .docx
var doc = app.Documents.Open(docLocation, true, false);
//Selects the document
var range = doc.Range();
for (int i = 0; i < listLen; i++)
{
//Finds the parameter, then replaces
range.Find.Execute(FindText: Convert.ToString(replaceVals[i].tempVal), Replace: WdReplace.wdReplaceAll, ReplaceWith: Convert.ToString(replaceVals[i].Boxes));
var shapes = doc.Shapes;
//Finds text within textboxes, then changes them
foreach (Microsoft.Office.Interop.Word.Shape shape in shapes)
{
var initialText = shape.TextFrame.TextRange.Text;
var resultingText = initialText.Replace(Convert.ToString(replaceVals[i].tempVal), Convert.ToString(replaceVals[i].Boxes));
shape.TextFrame.TextRange.Text = resultingText;
}
}
//prints document
doc.Save();
doc.Close();
//fully closes Word
Marshal.ReleaseComObject(app);
}
The problem occurs when it hits
var initialText = shape.TextFrame.TextRange.Text;
And throws an error saying: "This object does not support attached text."
The text in the shapes are nothing special. (e.g. tDATE, tNAME, etc.)
Any ideas?

I found the answer. Turns out my code was fine, however the document I was using (which I didn't write), had another shape on the second to last page to form a place to sign your name. I replaced that with an underscore, ran the code, and everything changed perfectly.
For those who also experience this problem, try checking how many shapes your foreach loop has counted:
http://i.imgur.com/1yNrL4p.png
Thank you Andrew and varocarbas for the help

*"In 2003 the AltText default for a standard textbox WAS the contained text BUT since you can change the Alt Text to NOT match it was never a good idea to read it this way. In 2010 the default for Alt Text is blank
If the textbox is named "Text Box 2" (substitute the correct name if not)
MsgBox ActiveDocument.Shapes("Text Box 2").TextFrame.TextRange should work."*
--
John SR Wilson
http://answers.microsoft.com/en-us/office/forum/office_2010-customize/shapesalternativetext-is-blank-for-the-docx/7671c746-2c2b-41d9-b7de-389a766587a7?page=2&msgId=31041d67-e62b-4ce0-b283-57fd6a4ff6b2

Related

How to change text field in a PPT programmatically in C#?

I have a PPT template in which I need to programmatically replace some text fields with data that comes from my database then convert the ppt to pdf and send it as attachment in an email. I am not sure of how to change the content of text fields in the PowerPoint Presentation. I think OpenXML needs to be used.
Please help me to feed dynamic data into my ppt template.
I have worked previously with the DocumentFormat.OpenXml from Microsoft but with word files. I played a bit with it to replace some Texts in a Power Point file.
Here is my simple test code snippet:
static void Main(string[] args)
{
// just gets me the current location of the assembly to get a full path
string fileName = GetFilePath("Resource\\Template.pptx");
// open the presentation in edit mode -> the bool parameter stands for 'isEditable'
using (PresentationDocument document = PresentationDocument.Open(fileName, true))
{
// going through the slides of the presentation
foreach (SlidePart slidePart in document.PresentationPart.SlideParts)
{
// searching for a text with the placeholder i want to replace
DocumentFormat.OpenXml.Drawing.Text text =
slidePart.RootElement.Descendants<DocumentFormat.OpenXml.Drawing.Text>().FirstOrDefault(x => x.Text == "[[TITLE]]");
// change the text
if (text != null)
text.Text = "My new cool title";
// searching for the second text with the placeholder i want to replace
text =
slidePart.RootElement.Descendants<DocumentFormat.OpenXml.Drawing.Text>().FirstOrDefault(x => x.Text == "[[SUBTITLE]]");
// change the text
if (text != null)
text.Text = "My new cool sub-title";
}
document.Save();
}
}
In my case i had a simple presentation with one slide and in the Text fields the entries "[[TITLE]]" and "[[SUBTITLE]]" which i replaced with this text.
For my test file this worked well but maybe you will need to adopt / change something for your specific file.
E.g. in Word i had sometimes texts which was splitted in multiple Text parts within a Run element and had to write a logic to "collect" this data and replace them with one Text element with the new text i wanted to be there or maybe you have to search for other Descendant types.

Formatting Sections of a TextRange or TextRange2 in PowerPoint.Interop C#

I would like to add rich text to a TextBox on a PowerPoint Slide.
I am using .Net 4.7.2 with Microsoft.Office.Interop.PowerPoint
Overview
// Initialization of the Application
public PresentationGenerator () {
pptApplication = new Application ();
}
// Creating a new document based on a teplate.
Presentation pptPresentation = generator.pptApplication.Presentations.Open (templatePath, MsoTriState.msoFalse, MsoTriState.msoTrue, showWindow);
// Setting a slide and editing the text works perfectly fine with this
Slide currentSlide;
currentSlide = pptPresentation.Slides[1];
currentSlide.Shapes.Title.TextFrame.TextRange.Text = "Wonderful Title";
currentSlide.Shapes[3].TextFrame.TextRange.Text = "Great TextBox";
This is an mwe of my setup to set text on slides.
I would however like to add text to one of my shapes using a loop and setting the layout depending on a property.
Imagine the following Array
customParagraphs = [
{
text:"example heading",
type:"title"
},{
text:"example normal text",
type:"text"
}]
I can loop over this list and add the text to the end of the TextRange2 using .insertAfter(text) and try setting the font-size for the text portion that i added.
TextRange2 textrange = currentSlide.Shapes[3].TextFrame2.TextRange
foreach(var paragraph in customParagraphs){
TextRange2 paragraphRange = textrange.Paragraphs.insertAfter(paragraph.text)
if(paragraph.type == "title"){
paragraphRange.Font.Size = 24.0F;
}
}
This will successfully add the text and change the font-size, if type is title. However it will change the font-size for the whole text-range!
The reference returned by .insertAfter() seems to refer to the instance of TextRange2 and not my newly added paragraph.
My Questions
Is there a way to change the font-size and other attributes of a line, paragraph or word inside a TextRange or TextRange2 element?
Is there a better way to add text to a TextRange or TextRange2 element than .insertAfter that preferably returns a reference to only the text i added?

c# How to get standard font style object in Word

I'm inserting variable text from a *.html file into a Word document and have to adapt the font(name and size) of the inserted text to the rest of the document.
I have a working solution but I don't like the way I did it, so I'm searching another way to get the standard font name and size from Word application.
Another problem is that NameLocal can be in different languages. So I also need another way to find the Headers. I already tried Style.Type but it has always value "1"
My code so far:
foreach (Word.Style style in Globals.ThisAddIn.Application.ActiveDocument.Styles)
{
if (style.NameLocal.Equals("Normal")) // find correct style object
{
float size = style.Font.Size;
string font = style.Font.Name;
foreach (Word.Paragraph paragraph in Globals.ThisAddIn.Application.ActiveDocument.Paragraphs)
{
if (paragraph.Range.get_Style().NameLocal.Contains("Heading")) // find all headers
{
paragraph.Range.Font.Size = size;
paragraph.Range.Font.Name = font;
}
}
break;
}
}
The reason why I'm not simply changing the style is so the headers are still marked as headers.
I'm pretty clueless atm
For built-in styles, the Word object model provides the enumeration WdBuiltinStyle. Using this instead of a string value (the local name of a style) makes specifying a style language-independent. In addition, the built-in styles will always be present in a document so there's no need to loop the Styles collection of a document to get a particular style.
So, for example:
Word.Document doc = Globals.ThisAddin.Application.ActiveDocument;
Word.Style style = doc.Styles[Word.WdBuildinStyle.wdStyleNormal];
float size = style.Size;
string font = style.Font.Name;
foreach (Word.Paragraph paragraph in doc)
{
if (paragraph.Range.get_Style() = Word.WdBuildinStyle.wdStyleHeading1)
{
paragraph.Range.Font.Size = size;
paragraph.Range.Font.Name = font;
}
}

When I try to use UI Automation for PowerPoint 2013, I can only get the first character/word when I use RangeFromPoint

The code works for Word and Outlook but fails with PowerPoint in that only the first character or first word of the textbox ever gets selected. Is this a bug? Is there any workaround? Try this on a simple PowerPoint slide in PowerPoint 2013.
private static async Task<string> getText(double x, double y)
{
string result = null;
try
{
var location = new System.Windows.Point(x, y);
AutomationElement element = AutomationElement.FromPoint(location);
object patternObj;
if (element.TryGetCurrentPattern(TextPattern.Pattern, out patternObj))
{
var textPattern = (TextPattern)patternObj;
var range = textPattern.RangeFromPoint(location);
range.ExpandToEnclosingUnit(TextUnit.Word);
range.Select();
var text = range.GetText(-1).TrimEnd('\r');
return text.Trim();
}
else
{
return "no text found";
}
}
catch (Exception ex)
{
return ex.Message;
}
}
You cannot see it from the screenshot, but the mouse is on "first" not "stuck", but regardless of where the mouse is placed, it always is stuck. Maybe this is fixed in PowerPoint 2016?
When I look at the bounding box for the range it is always the whole element, rather than the selected word. That could be part of the problem of why RangeToPoint is not working.
Original posted in MSDN but no response...
Update. If I use
text = printRange(range, text);
while (range.Move(TextUnit.Word, 1) > 0)
{
text += Environment.NewLine;
text = printRange(range, text);
}
I get
This behavior is probably due to a limitation in PowerPoint 2013, and I expect you can't work around it using UIA. When you call RangeFromPoint(), the UIA provider hit beneath the mouse, (ie the one that's implementing IUIAutomationTextPattern::RangeFromPoint(),) is meant to return a degenerative (ie empty) range where the mouse cursor is. Then the UIA client can expand the returned range to get the surrounding character, word, line or paragraph.
However, as you point out, PowerPoint 2013 isn't doing that. I've just written the test code below, (using a managed wrapper for the native Windows UIA API generated by tlbimp.exe,) and found that PowerPoint apparently returns a TextRange for the entire text box beneath the cursor. When I ran the code, I found that I did get the expected word beneath the cursor in WordPad, Word 2013 and PowerPoint OnLine, but not PowerPoint 2013. I got the same results when I ran the Text Explorer tool that's part of the Inspect SDK tool. The image below shows Text Explorer reporting that the text returned from PowerPoint 2013 is the entire text in the a text box, when the mouse is hovering over one of those words.
(I should add that for the test code below to work at all, I think the current display scaling setting needs to be at 100%. I've not added code to account for some other scaling being active.)
I don't know if this is fixed in PowerPoint 2016, I'll try to look into that and let you know.
Thanks,
Guy
private void buttonGetTheText_Click(object sender, EventArgs e)
{
labelText.Text = "No text found.";
IUIAutomation uiAutomation = new CUIAutomation8();
Point ptCursor = Cursor.Position;
tagPOINT pt;
pt.x = ptCursor.X;
pt.y = ptCursor.Y;
// Cache the Text pattern that's available through the element beneath
// the mouse cursor, (if the Text pattern's supported by the element,) in
// order to avoid another cross-process call to get the pattern later.
int patternIdText = 10014; // UIA_TextPatternId
IUIAutomationCacheRequest cacheRequestTextPattern =
uiAutomation.CreateCacheRequest();
cacheRequestTextPattern.AddPattern(patternIdText);
// Now get the element beneath the mouse.
IUIAutomationElement element =
uiAutomation.ElementFromPointBuildCache(pt, cacheRequestTextPattern);
// Does the element support the Text pattern?
IUIAutomationTextPattern textPattern =
element.GetCachedPattern(patternIdText);
if (textPattern != null)
{
// Now get the degenerative TextRange where the mouse is.
IUIAutomationTextRange range = textPattern.RangeFromPoint(pt);
if (range != null)
{
// Expand the range to include the word surrounding
// the point where the mouse is.
range.ExpandToEnclosingUnit(TextUnit.TextUnit_Word);
// Show the word in the test app.
labelText.Text = "Text is: \"" + range.GetText(256) + "\"";
}
}
}
I can suggest only Python code getting caption text of the slide (for example). Sorry, I have no time to re-write it on C#. You can play with the PowerPoint.Application COM object and MSDN example of Power Point automation.
from __future__ import print_function
import win32com.client as com
pp = com.Dispatch('PowerPoint.Application')
print(pp.Presentations[0].Slides[8].Shapes[0].TextFrame.TextRange.Text)

FlowDocument alternative in wp8 or Hyperlinks and Run on same line

I have a text with hyperlinks in it.
To not irritate the reader I want to have the normal text and the links on the same line. Right now every Inline element starts a new line.
It displays:
Please visit
http://google.com
to continue.
I want:
Please visit http://google.com to continue.
I've also noticed, that the hyperlink Hit area cover the hole inline element and not just the text.
My problem is identical than described and solved here:
Add clickable hyperlinks to a RichTextBox without new paragraph
The problem is, that it seems than something like a flowdocument for wp8 doesn't exist.
I need to create the inline elements programatically.
EDIT 1:
Here my code how I add the inline elements:
int index = 0;
rt = new RichTextBox() { };
while (true)
{
Paragraph para = new Paragraph();
if (item.text.Substring(index).IndexOf("<") == 0)
{
//TRUE when link
//I extract the URL and the linktext, and also update the index
Hyperlink hyper = new Hyperlink();
hyper.Click += new RoutedEventHandler((sender,e) => Hyperlink_Click(sender,e,URL));
hyper.Inlines.Add(linktext);
para.Inlines.Add(hyper);
}
else if (item.text.Substring(index).Contains("<"))
{
//TRUE when text, item.text contains a link
// I extract the text and update index
Run run = new Run() { Text = text };
para.Inlines.Add(run);
}
else
{
//TRUE when only text is left
Run run = new Run() { Text = item.text.Substring(index) };
para.Inlines.Add(run);
rt.Blocks.Add(para);
break;
}
// REMOVE: rt.Blocks.Add(para);
}
rt.SetValue(Grid.RowProperty, MainViewer.RowDefinitions.Count - 1);
MainViewer.Children.Add(rt);
EDIT 2
I still couldn't solve this Problem, does no one know a solution? I saw what I want in an App before, so it must be possible.
EDIT 3
I've created for every inline element a new paragraph. I've fixed my code above, it is working now
Paragraph p = new Paragraph();
p.Inlines.Add("Plase visit ");
var link = new Hyperlink();
link.Inlines.Add("google.com ");
p.Inlines.Add(link);
p.Inlines.Add("to continue");
rtb.Blocks.Add(p);
this works fine for me.
PS
If you want to show some html in you app, you can use HTMLTextBox or HTMLViewer from http://msptoolkit.codeplex.com/

Categories

Resources