Word Interop Delete results in Wrong Parameter - c#

I have the pleasure to write some code that moves around stuff in an Office XP environment. I've referenced the OfficeXP Interop Assemblies and written code to Search/Replace stuff. That works fine. Now I need to delete Text around a Bookmark and i keep getting Exceptions thrown at me.
Here is some of the code:
object units = WdUnits.wdLine;
object lines = 2;
object extend = WdMovementType.wdExtend;
object bookmarkName = "Bank1";
var bm = doc.Bookmarks;
var bm1 = doc.Bookmarks.get_Item(bookmarkName);
var ra = bm1.Range;
ra.Delete(ref units, ref lines);
The last line is where i get a "Wrong Parameter" Exception. Looking at the Definition in the MSDN I kind of think I'm right. But obviously I'm not. Hope you guys can help me out here.
Update: ok, i see. Using the Delete method on the Range object i can only use wdWord as a Parameter. I'd like to change my question now: what i do want to do is delete two lines starting from the bookmark. How would i do this?

Range objects in Word are not "line oriented", they don't allow line operations, only paragraph operations. However, selections allow line operations. The current selection is not a property of the word document, but of the word application object. Here is some VBA code which does essentially what you try, I think you can easily port this to C#:
Dim rng As Range
Dim doc As Document
Set doc = ActiveDocument
Set rng = doc.Bookmarks("BM").Range
Dim s As Long, e As Long
rng.Select
s = Application.Selection.Start
e = Application.Selection.Next(wdLine, 1).End
Application.Selection.SetRange s, e
Application.Selection.Delete

Ok, i found a way to do what i had to do. Here is the Code:
if (doc.Bookmarks.Exists("Bank1"))
{
object bookmarkName = "Bank1";
object units = WdUnits.wdLine;
object lines = 2;
object extend = WdMovementType.wdExtend;
doc.Bookmarks.get_Item(bookmarkName).Select();
app.Selection.MoveDown(units, lines, extend);
app.Selection.Delete();
}

Related

Passing a vector from C# to Python

I use Python.Net for C# interaction with Python libraries. I solve the problem of text classification. I use FastText to index and get the vector, as well as Sklearn to train the classifier (Knn).During the implementation, I encountered a lot of problems, but all were solved, with the exception of one.
After receiving the vectors of the texts on which I train Knn, I save them to a separate text file and then, if necessary, use it.
string loadKnowVec = File.ReadAllText("vectorKnowClass.txt", Encoding.Default);
string[] splitKnowVec = loadKnowVec.Split('\r');
splitKnowVec = splitKnowVec.Where(x => x != "").ToArray();
for()
{
keyValues_vector.Add(float.Parse(splitKnowVec[i], NumberFormatInfo.InvariantInfo), 1);
}
dynamic X_vec = np.array(keyValues_vector.Keys.ToArray()).reshape(-1, 1);
dynamic y_tag = np.array(keyValues_vector.Values.ToArray());
dynamic neigh = KNN(n_neighbors: 3);
dynamic KnnFit = neigh.fit(X_vec, y_tag);
string predict = neigh.predict("0.00889");
MessageBox.Show("Скорее всего это: "+predict);
During the training of the classifier, I encountered such a problem that from c# to python, it is not values with the float type, but the value of System.Single[].
Python.Runtime.PythonException: "TypeError : float() argument must be a string or a number,
not 'Single[]'
The stored value, at this point, of dynamic X_vec is "System.Single[]".(I think that's exactly the problem)
2.At first I tried to manually set the values of X_vec, but the error and its values were the same.
The first idea was to change the array type using the numpy library, but it didn't help, it also gave out "".
dynamic Xx = np.array(X_vec, dtype: "float");
dynamic yY = np.array(y_tag, dtype: "int");
Next, it was tried to create an empty array in advance and load specific values into it before changing the data type, but this also did not work.
Perhaps I do not understand the principle of the formation and interaction of the MSVS19 IDE and the python interpreter.
I solved this issue for a couple of days and each time I thought it was worth reading the documentation on python.net .
As a result, I found a solution and it turned out to be quite banal, it is necessary to represent X_vec not as a float[] , but as a List<float>
List<float> vectors = keyValues_vector.Keys.ToList();
List<int> classTag = keyValues_vector.Values.ToList();
dynamic a = np.array(vectors);
dynamic X_vec = a.reshape(-1, 1);
dynamic y_tag = np.array(classTag);

C# Interop Non-invocable member 'Microsoft.Office.Interop.Excel.Range.End' cannot be used like a method

I'm using C# Interop to get some values from a Worksheet and I get the following error:
Non-invocable member 'Microsoft.Office.Interop.Excel.Range.End' cannot be used like a method.
This is my code:
var wb = (Excel.Workbook)Globals.ThisAddIn.Application.ActiveWorkbook;
var wsEvars = wb.Sheets["Evars"];
var wsProps = wb.Sheets["Props"];
var wsEvents = wb.Sheets["Events"];
var wsListVars = wb.Sheets["List Vars"];
var sheetList = new Excel.Worksheet[] { wsEvars, wsProps, wsEvents, wsListVars };
for (var i = 0; i < sheetList.Length; i++)
{
// I get the error on the line below
var rowLast = sheetList[i].Range["I" + sheetList[i].Rows.Count].End(Excel.XlDirection.xlUp).Row;
}
The thing is that is works if I try as follows:
for (var i = 0; i < sheetList.Length; i++)
{
var rowLast = wsEvars .Range["I" + wsEvars .Rows.Count].End(Excel.XlDirection.xlUp).Row;
}
Am I missing something?
Looks like you found a bug in the C# compiler. The bug is actually present in the workaround, it ought to not compile for the same reasons the first snippet did not. Albeit that it is difficult to definitely claim that this is a bug, the C# language spec does not describe what is acceptable in this case.
The Range.End property is an indexed property. Such properties are not formally supported in C#, the language permits only the class indexer (aka this[]) to be the one-and-only indexed property of a class. But that restriction was lifted in C# version 4, specifically to make interop with COM servers easier. Like Excel, indexed properties are very common in COM object models.
Like the normal indexer, you have to use square brackets. Fix:
var rowLast = sheetList[i].Range["I" + sheetList[i].Rows.Count]
.End[Excel.XlDirection.xlUp].Row;
And the workaround you had to use in older C# versions is still available:
var rowLast = sheetList[i].Range["I" + sheetList[i].Rows.Count]
.get_End(Excel.XlDirection.xlUp).Row;
Hard to guess why it finds () parentheses acceptable in the second snippet. It looks like a bug, swims like a bug and quacks like a bug, so it is probably a bug. Let them know about it by clicking the New Issue button. I doubt they'll fix it but there might be more wrong than meets the eye.

Equivalent to string * 10 from VB6 in C#

I have import an unmanageable .dll to my project. It's no any document left
and the original working code is in VB6. so I try to make C# code equivalent to VB6 as same as possible.
PROBLEM
I don't know how to convert following code to C#...
Dim ATQ As String * 10
Dim Uid As String * 10
Dim MultiTag As String * 10
NOTE
Q: some users ask me that do you really need string fixed length?
A: I already try string in c# but there are no result update to these variable. So, I think input signature for the dllImport function might be wrong. So, I want to make it as same as VB6 did because I didn't know exactly what should be the right signature.
TRIAL & ERROR
I tried all of this but it's not working (still no result update to these variable)
Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString ATQ = new Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString(10)
Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString Uid = new Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString(10)
Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString MultiTag = new Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString(10)
You can use Microsoft.VisualBasic.Compatibility:
using Microsoft.VisualBasic.Compatibility;
var ATQ = new VB6.FixedLengthString(10);
var Uid = new VB6.FixedLengthString(10);
var MultiTag = new VB6.FixedLengthString(10);
But it's marked as obsolete and specifically not supported for 64-bit processes, so write your own that replicates the functionality, which is to truncate on setting long values and padding right with spaces for short values. It also sets an "uninitialised" value, like above, to nulls.
Sample code from LinqPad (which I can't get to allow using Microsoft.VisualBasic.Compatibility I think because it is marked obsolete, but I have no proof of that):
var U = new Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString(5);
var S = new Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString(5,"Test");
var L = new Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString(5,"Testing");
Func<string,string> p0=(s)=>"\""+s.Replace("\0","\\0")+"\"";
p0(U.Value).Dump();
p0(S.Value).Dump();
p0(L.Value).Dump();
U.Value="Test";
p0(U.Value).Dump();
U.Value="Testing";
p0(U.Value).Dump();
which has this output:
"\0\0\0\0\0"
"Test "
"Testi"
"Test "
"Testi"
string ATQ;
string Uid;
string MultiTag;
One difference is that, in VB6, I believe the String * 10 syntax may create fixed-size strings. If that's the case, then the padding behavior may be different.

Get Find results in Word (Save a set of successive Word Find results for later use)

I'm trying to write a Word Add-In that searches the document for a specific text, highlights its occurrences, and enables the user to skip from one occurrence to the next.
I've succeeded with the first two tasks (searching and highlighting), but I couldn't find a way to store the locations of the results, so the user can skip between them.
The Find method returns bool, and I couldn't find any info regarding the count of the occurrences, and their location.
Here is the code I have so far:
var range=Globals.ThisAddIn.Application.ActiveDocument.Range();
var find = range.Find;
find.HitHighlight("My Text");
bool found = find.Found;
So how can I get the result of Find?
Thanks!
Looking at the Word API documentation for Find Members I do not see any method that returns a the list of results, so I do not believe you can do what you want.
Maybe I am misunderstanding your point of creating this add-in, but you can do the functionality you describe inside of Word already, via the Advanced Find. So what is the point of your add-in?
Almost 10 years later, but I recently ran into a need to do the same thing as the OP.....
When you want to execute a series of Word Find commands and want to later let the user set the cursor to the location of the previous Find(s), all you really need to store at the time you do each Find is the start and end range values associated with each Find result. Then, at a later point in your program, you can create a Range object, load the previously stored start range value and end range value, and navigate to that location in the document using the reconstituted Range object.
Note: This presumes the user did not edit the document after the start and end range values associated with each Find result were stored. If that's a possibility, then you'll need to provide a way for the user to redo the Find(s) before letting the user attempt to navigate to the locations.
Here's the general idea. This is Word VSTO add-in code for a scenario where I want the find to not use wildcards and ignore whitespace in the found text. Untested, not compiled, but extracted from a working VSTO add-in.
Final note: I didn't try this, but the Word Range object provides a Duplicate property. By duplicating a Range object, you can, for example, change the starting or ending character position of the duplicate range without changing the original range, e.g., Word.Range copyOfRangeObject = wordRange.Duplicate; It may be you use this to build an array or list of found Range(s)
// Example from a Word VSTO add-in program
Microsoft.Office.Interop.Word.Document document_ActiveDoc = Globals.ThisAddIn.Application.ActiveDocument;
Microsoft.Office.Interop.Word.Range wordRange = null;
// list and class to store start and range end values
IList<RangeMetadata> rangeMetadataList = new List<RangeMetadata>();
public class RangeMetadata
{
int rangeStart;
public RangeStart { get => rangeStart; set => rangeStart = value; }
int rangeEnd;
public RangeEnd { get => rangeEnd; set => rangeEnd = value; }
public class RangeMetadata(int rangeStart, int rangeEnd)
{
RangeStart = rangeStart;
RangeEnd = rangeEnd;
}
}
// Code for Word Find
string yourFindText = "foo";
wordRange = document_ActiveDoc.Content;
wordRange.Find.ClearFormatting();
wordRange.Find.IgnoreSpace = true;
// Prime the pump Find
wordRange.Find.Execute(FindText: yourFindText, MatchWildcards: false, Forward: true);
while (wordRange.Find.Found)
{
// Note: you can obtain the Find range page number. Might be useful to the user
int pageNumber = wordRange.get_Information(WdInformation.wdActiveEndPageNumber);
RangeMetadata rangeMetadata = new RangeMetadata(wordRange.Start, wordRange.End);
rangeMetadataList.Add(rangeMetadata);
// Next Find
wordRange.Find.Execute(FindText: yourFindText, MatchWildcards: false, Forward: true);
}
// code to set the cursor on previous find(s) using a range
foreach (RangeMetadata rmd in rangeMetadataList)
{
Word.Range rng = Globals.ThisAddIn.Application.ActiveDocument.Range(rmd.RangeStart, rmd.RangeEnd);
Globals.ThisAddIn.Application.ActiveWindow.ScrollIntoView(rng, true);
// to select the found text
rng.Select();
// optional to de-select and leave the cursor at the beginning of the found text
//object direction = Word.WdCollapseDirection.wdCollapseStart;
//Globals.ThisAddIn.Application.Selection.Collapse(ref direction);
}

Using a large static array in C# (Silverlight on Windows Phone 7)

I have a question that's so simple I cannot believe I can't answer it myself. But, there you go.
I have a large-ish static list (of cities, latitudes and longitudes) that I want to use in my Windows Phone 7 Silverlight application. There are around 10,000 of them. I'd like to embed this data statically in my application and access it in an array (I need to cycle through the whole list in code pretty regularly).
What is going to be my most effective means of storing this? I'm a bit of an old school sort, so I reckoned the fastest way to do it would be:
public struct City
{
public string name;
public double lat;
public double lon;
};
and then...
private City[] cc = new City[10000];
public CityDists()
{
cc[2].name = "Lae, Papua New Guinea"; cc[2].lat = 123; cc[2].lon = 123;
cc[3].name = "Rabaul, Papua New Guinea"; cc[3].lat = 123; cc[3].lon = 123;
cc[4].name = "Angmagssalik, Greenland"; cc[4].lat = 123; cc[4].lon = 123;
cc[5].name = "Angissoq, Greenland"; cc[5].lat = 123; cc[5].lon = 123;
...
However, this bums out with an "out of memory" error before the code actually gets to run (I'm assuming the code itself ended up being too much to load into memory).
Everything I read online tells me to use an XML resource or file and then to deserialise that into instances of a class. But can that really be as fast as using a struct? Won't the XML take ages to parse?
I think I'm capable of writing the code here - I'm just not sure what the best approach is to start with. I'm interested in speed of load and (more importantly) run time access more than anything.
Any help very much appreciated - first question here so I hope I haven't done anything boneheaded.
Chris
10,000 structs shouldn't run out of memory, but just to make sure, I would first try turning your struct into a class such that it uses the heap instead of the stack. There is a strong possibility that doing that will fix your out of memory errors.
An XML file stored in isolated storage might be a good way to go if your data is going to be updated even every once in a while. You could pull the cities from a web service and serialize those classes to the Application Store in isolated storage whenever they get updated.
Also, I notice in the code samples that the cc array is not declared static. If you have a few instances of CityDists, then that could also be bogging down memory as the array is getting re-created every time a new CityDists class is created. Try declaring your array as static and initializing it in the static constructor:
private static City[] cc = new City[10000];
static CityDists()
{
cc[2].name = "Lae, Papua New Guinea"; cc[2].lat = 123; cc[2].lon = 123;
cc[3].name = "Rabaul, Papua New Guinea"; cc[3].lat = 123; cc[3].lon = 123;
cc[4].name = "Angmagssalik, Greenland"; cc[4].lat = 123; cc[4].lon = 123;
cc[5].name = "Angissoq, Greenland"; cc[5].lat = 123; cc[5].lon = 123;
...
If loading an xml doc from the xap works for you..
Here's a project I posted demonstrating loading of an xml doc from the XAP via XDocument/LINQ and databinding to a listbox for reference.
binding a Linq datasource to a listbox
If you want to avoid the XML parsing and memory overhead, you could use a plain text file for storing your data and use the .Net string tokenizer functions to parse the entries e.g. use String.Split()
You could also load the file partially to keep memory consumption low. For example, you load only k out of n lines of the file. In case you need to access a record that is outside the currently loaded k segments, load the appropriate k segments. You could either do it the old school way or even use the fancy Serialization stuff from .Net
Using a file such as XML or a simple delimited file would be a better approach as others have pointed out. However can I also suggest another change to improve the use of memory.
Something like this (although the actual loading should be done using an external file):-
public struct City
{
public string name;
public string country;
public double lat;
public double lon;
}
private static City[] cc = new City[10000];
static CityDists()
{
string[] countries = new string[500];
// Replace following with loading from a "countries" file.
countries[0] = "Papua New Guinea";
countries[1] = "Greenland";
// Replace following with loading from a "cities" file.
cc[2].name = "Lae"; cc[2].country = contries[0]; cc[2].lat = 123; cc[2].lon = 123;
cc[3].name = "Rabaul"; cc[3].country = countries[0]; cc[3].lat = 123; cc[3].lon = 123;
cc[4].name = "Angmagssalik"; cc[4].country = countries[1]; cc[4].lat = 123; cc[4].lon = 123;
cc[5].name = "Angissoq"; cc[5].country= countries[1]; cc[5].lat = 123; cc[5].lon = 123;
}
This increases the size of the structure slightly but reduces the memory used by duplicate country names signficantly.
I hear your frustration. Run your code without the debugger, it should work fine. I'm loading 2 arrays in under 3 seconds, each with over 100,000 elements. Debugger reports "Out of Memory", which is simply not the case.
Oh and you are correct about the efficiency. Loading the same information from an XML file was taking over 30 seconds on the phone.
I don't know who was responding to your question but they really should stick to marketing.

Categories