Cannot iterate through Shapes collection - ms-word

C# and word interop,
I have a word document with some textboxs (msoTextBox shapes), the problem that I can't iterate through the shapes collection with the code below :
foreach (Shape shape in WordDocument.Shapes)
{}
although when setting a breakpoint in the loop line I can see that WordDocument.Shapes.Count returns 4.
I note that textboxs are inserted using open xml sdk.

I've found there's a problem when textboxes are used. Take a look at this solution.

From Code Project :
// Get the word count from all shapes
foreach (Word.Shape shape in wordDocument.Shapes)
{
if (shape.TextFrame.HasText < 0)
{
count+=GetCountFromRange(shape.TextFrame.TextRange,wordDocument,word);
}
}
From what you said, you look like you do the right thing.
Can you give us the Error StackTrace ?
PS : I know my question should have been in the comments, but it wouldn't have been readable :)

So,
Replace :
foreach (Shape shape in WordDocument.Shapes)
{
}
By:
foreach (Range rangeStory in WordDocument.StoryRanges)
{
foreach (Shape shape in rangeStory.ShapeRange)
{
}
}
It's work perfectly.

Related

How to select symbols onWorkspaceSymbol

I am developing an extension for visual studio code using language server protocol, and I am including the support for "Go to symbol in workspace". My problem is that I don't know how to select the matches...
Actually I use this function I wrote:
function IsInside(word1, word2)
{
var ret = "";
var i1 = 0;
var lenMatch =0, maxLenMatch = 0, minLenMatch = word1.length;
for(var i2=0;i2<word2.length;i2++)
{
if(word1[i1]==word2[i2])
{
lenMatch++;
if(lenMatch>maxLenMatch) maxLenMatch = lenMatch;
ret+=word1[i1];
i1++;
if(i1==word1.length)
{
if(lenMatch<minLenMatch) minLenMatch = lenMatch;
// Trying to filter like VSCode does.
return maxLenMatch>=word1.length/2 && minLenMatch>=2? ret : undefined;
}
} else
{
ret+="Z";
if(lenMatch>0 && lenMatch<minLenMatch)
minLenMatch = lenMatch;
lenMatch=0;
}
}
return undefined;
}
That return the sortText if the word1 is inside the word2, undefined otherwise. My problem are cases like this:
My algorithm see that 'aller' is inside CallServer, but the interface does not mark it like expected.
There is a library or something that I must use for this? the code of VSCode is big and complex and I don't know where start looking for this information...
VSCode's API docs for provideWorkspaceSymbols() provide the following guidance (which I don't think your example violates):
The query-parameter should be interpreted in a relaxed way as the editor will apply its own highlighting and scoring on the results. A good rule of thumb is to match case-insensitive and to simply check that the characters of query appear in their order in a candidate symbol. Don't use prefix, substring, or similar strict matching.
These docs were added in response to this discussion, where somebody had very much the same issue as you.
Having a brief look at VSCode sources, internally it seems to use filters.matchFuzzy2() for the highlighting (see here and here). I don't think it's exposed in the API, so you would probably have to copy it if you wanted the behavior to match exactly.

How to find the first instance of a pdf file using HTMLElement?

void DownloadFile(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElementCollection links = webBrowser1.Document.Links;
foreach (HtmlElement link in links) //
{
if (link.InnerText.Equals("*.pdf"))
{
link.InvokeMember("Click");
break;
}
}
}
How do I find the first instance of a pdf file using HTML element. I was trying to do *.pdf but it does not work.
Looks like you are using C#, you've tagged this as htmlelements which is a Java library, so you might have the wrong place.
However, if InnerText gets the link href (or if the link text contains the .pdf) then you probably want:
EndsWith(".pdf")
instead of
Equals("*.pdf").

Play! + Scala: Split string by commnas then Foreach loop

I have a long string similar to this:
"tag1, tag2, tag3, tag4"
Now in my play template I would like to create a foreach loop like this:
#posts.foreach { post =>
#for(tag <- #post.tags.split(",")) {
<span>#tag</span>
}
}
With this, I'm getting this error: ')' expected but '}' found.
I switched ) for a } & it just throws back more errors.
How would I do this in Play! using Scala?
Thx in advance
With the help of #Xyzk, here's the answer: stackoverflow.com/questions/13860227/split-string-assignment
Posting this because the answer marked correct isn't necessarily true, as pointed out in my comment. There are only two things wrong with the original code. One, the foreach returns Unit, so it has no output. The code should actually run, but nothing would get printed to the page. Two, you don't need the magic # symbol within #for(...).
This will work:
#for(post <- posts)
#for(tag <- post.tags.split(",")) {
<span>#tag</span>
}
}
There is in fact nothing wrong with using other functions in play templates.
This should be the problem
#for(tag <- post.tags.split(",")) {
<span>#tag</span>
}

Flex/Air file dragging: how to restrict filetypes?

I'm using a <S:nativeDragDrop> and getting files dragged over a component like so:
var arr:Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
I'm not sure how to restrict what type of files can be dragged. Is there a native control for this? The help documents mention the possibility of defining completely different ClipboardFormats, but I have no idea how to do that; I could run regex on the filenames as well, but that seems overcomplicated.
Wondering if there's a way like with FileReference.browse to specify specific file extensions
As far as I know, there is not a built-in way to filter dropped files. However, in your NATIVE_DRAG_ENTER handler, you could loop through the list of files and choose not accept the drag based on their file types. Or, you could merely ignore the unsupported types when you are processing the NATIVE_DRAG_DROP.
var validTypes:Object = {png : true, jpg : true, gif : true};
function nativeDragEnter(event:NativeDragEvent):void {
var files:Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array
for each(var file:File in files) {
if(!validTypes[file.extension.toLowerCase()]) // Don't accept drag if any of the dropped files aren't supported.
return;
}
DragManager.acceptDrag(InteractiveObject(event.target));
}
function nativeDragDrop(event:NativeDragEvent):void {
var files:Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array
for each(var file:File in files) {
if(validTypes[file.extension]) //accept only certain files
processFile(file);
}
}
As a side note, I assumed you are working on an AIR app here, but if you aren't, you'll have to use the FileReference class instead of File.

KRL and Yahoo Local Search

I'm trying to use Yahoo Local Search in a Kynetx Application.
ruleset avogadro {
meta {
name "yahoo-local-ruleset"
description "use results from Yahoo local search"
author "randall bohn"
key yahoo_local "get-your-own-key"
}
dispatch { domain "example.com"}
global {
datasource local:XML <- "http://local.yahooapis.com/LocalSearchService/V3/localsearch";
}
rule add_list {
select when pageview ".*" setting ()
pre {
ds = datasource:local("?appid=#{keys:yahoo_local()}&query=pizza&zip=#{zip}&results=5");
rs = ds.pick("$..Result");
}
append("body","<ul id='my_list'></ul>");
always {
set ent:pizza rs;
}
}
rule add_results {
select when pageview ".*" setting ()
foreach ent:pizza setting pizza
pre {
title = pizza.pick("$..Title");
}
append("#my_list", "<li>#{title}</li>");
}
}
The list I wind up with is
. [object Object]
and 'title' has
{'$t' => 'Pizza Shop 1'}
I can't figure out how to get just the title. It looks like the 'text content' from the original XML file turns into {'$t' => 'text content'} and the '$t' give problems to pick().
When XML datasources and datasets get converted into JSON, the text value within an XML node gets assigned to $t. You can pick the text of the title by changing your pick statement in the pre block to
title = pizza.pick("$..Title.$t");
Try that and see if that solves your problem.
Side notes on things not related to your question to consider:
1) Thank you for sharing the entire ruleset, what problem you were seeing and what you expected. Made answering your question much easier.
2) The ruleset identifier should not be changed from what AppBuilder or the command-line gem generate for you. Your identifier that is currently
ruleset avogadro {
should look something more like
ruleset a60x304 {
3) You don't need the
setting ()
in the select statement unless you have a capture group in your regular expression
Turns out that pick("$..Title.$t") does work. It looks funny but it works. Less funny than a clown hat I guess.
name = pizza.pick("$..Title.$t");
city = pizza.pick("$..City.$t");
phone = pizza.pick("$..Phone.$t");
list_item = "<li>#{name}/#{city} #{phone}</li>"
Wish I had some pizza right now!