r/badcode • • Jun 28 '21

c# MS Word Interop is Trash

Post image
866 Upvotes

33 comments sorted by

141

u/UltraPoci Jun 28 '21

Can anyone explain plis

142

u/PolishedCheese Jun 29 '21

Due to the ms word Interop library in c# being a bag of flaming dogshit, the author of the code wrote the disclaimer. Why the disclaimer was necessary, I assume is that the value of those 2 objects should be the same after running the 4 lines of code as after. -1 then +1 is a change of 0

89

u/TheOldTubaroo Jun 29 '21

My guess is that it's something to do with either wide (multi-byte) characters or combining characters. Something in the previous code might end up with a selection that starts or ends in the middle of a "character" (like between the 'e' and the '`' of an 'è', fire example).

By moving both the start and end of the selection forwards and backwards by a "character", they ensure that the resulting selection is valid, instead of in the middle of what the user sees as a "character".

17

u/[deleted] Jun 29 '21

the range start and range end are defined explicitly by calling these operations in this order.

34

u/[deleted] Jun 29 '21

looks like he's moving forward and back twice

18

u/[deleted] Jun 28 '21

Seconded

6

u/[deleted] Jun 29 '21

Retracted

7

u/JimFive Jun 29 '21

It looks like after deleting the range Word gets confused and the programmer found that moving the start and end of the range and then moving them back fixed the problem

32

u/PolishedCheese Jun 29 '21

I have seen those horrors. That API was created in the before times, and has 0 consideration for maintainability.

54

u/Willinton06 Jun 28 '21

Interop needs to be rewritten in Core so we can actually do addins on core, they also need to add C# macros to seal de deal

26

u/vigbiorn Jun 29 '21

The documentation could also be massively upgraded.

I feel OP with the dreaded hack working in Excel Interop. Before I knew what I was looking for, it was impossible to work anything out and I needed to bash together a thing using marshaling (apparently a lot of people don't really know how to use it and are passing on those broken hacks). Once I stumbled into how it's meant to work, I could manage it but just sitting down from 0? It's useless, if not worse.

4

u/Zaphod118 Jun 29 '21

Do you have any tips or resources? I’ve got a work application that builds excel files, and I’ve been using all the marshaling “tricks” you find online. If there’s a better way I’d love to learn it! Because the way it is now is awful

3

u/vigbiorn Jun 29 '21

YMMV for everything below. I'm still no where near an expert. The code below works for my use-case, but it can have issues.

If I remember correctly, the biggest issue for me was using Cells[]. That is where the marshaling needed to come from, I think, because you're calling for the individual data to be read in from Excel. Using UsedRange and (for me) converting it to an object[,] was easiest. Though, if you're dealing with massive excel files, you may want to find a way to limit it to chunks.

I'd like to share resources, but I didn't save the site I used to get my code working over a year ago now, and looking again all I'm finding are options needing marshaling.

If it helps, here is are snippets I use (not used to Reddit's code formatting, so it might be janky):

Excel.Application excel = new Excel.Application();

Excel.Workbooks workbooks = excel.Workbooks;

Excel.Workbook exlFile = workbooks.Open(ExcelFilePath, ReadOnly: true);

Excel.Sheets sheets = exlFile.Worksheets;

//x is the index to your curr sheet of interest

Excel.Worksheet sheet = (Excel.Worksheet)sheets[x];

//I think this reads in the entire sheet, so might cause problems

//for large sheets

Excel.Range usedRange = sheet.UsedRange;

object[,] log = (usedRange.Value as object[,]);

// . . .var aValue = log[x,y];

// . . .

exlFile.Close();

workbooks.Close();

excel.Quit();

I've been running the code for awhile and it is much faster. I think Cells is meant for individual one-time cell access, and provided just as a "if you need it", hence why the marshaling is needed to clean up afterwards. Switching from individual Cell[] calls to the UsedRange.Value saved me minutes to read kind of small excel files and I've not had the issue of missing a bit of memory not properly marshaled and having a floating Excel instance in limbo taking up memory.

6

u/burgundius Jun 29 '21

You can actually make add-ins on core and .NET 5, by implementing the IDTExtensibility2 interface from AddInDesignerObjects COM dll, registering the class for COM and writing a few registry entries.

You won't get the benefit of easy debugging, you'll have to attach it yourself.

5

u/Willinton06 Jun 29 '21

I want the whole interoperability to be reworked on core with native support, I’m asking for way too much

43

u/Cmgeodude Jun 29 '21

The programmer gets a gold star for the comment.

No judgment here.

16

u/tulliandar Jun 29 '21

The comment is the worst thing about it. They could’ve tried to be helpful instead of ornery

40

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jun 28 '21 edited Jun 29 '21

Image Transcription: Photograph


[The image is a photograph of code on a computer screen, as follows:]

capRange.Delete();

//Until you've felt my pain I don't want to hear a damn thing about the next 4 lines
capRange.MoveStart(WdUnits.wdCharacter, 1);
capRange.MoveStart(WdUnits.wdCharacter, -1);
capRange.MoveEnd(WdUnits.wdCharacter, 1);
capRange.MoveEnd(WdUnits.wdCharacter, -1);

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

18

u/un_blob Jun 28 '21

good human

8

u/bacon666666 Jun 28 '21

God I hate it so

25

u/devdevgoat Jun 28 '21

Is this why when I select a word in office products it fucking includes the space before/after it?!?!

17

u/aj-ric Jun 29 '21

Probably not but I hate that as much as you do.

12

u/[deleted] Jun 29 '21

I think you can disable this via Options > Advanced > When Selecting, automatically select entire word.

5

u/AlFasGD Jun 29 '21

I'd hope there was a utility function for this

8

u/yaxamie Jun 29 '21

The comment is gold

3

u/danfish_77 Jun 29 '21

A lot of it is more that the Word object model is unintuitive and perhaps surprisingly complicated. Like, I feel like it should be pretty easy to tell if a table is going to break across a page, but nope! Gotta build the whole document to find out

3

u/gigawattwarlock Jun 29 '21

I have felt this pain.

Word wouldn’t make the dynamic range selection without forcefully setting the positioning, but wouldn’t honor 0.

Excel had a similar annoyance but I can’t remember what it was exactly.

1

u/ferrango Well it's broken now, and nobody is here to fix it Jun 29 '21

Try using the Libre Office interop library. I still have nightmares.

1

u/RavynousHunter Jun 29 '21

Yeah...much as I love C# and .NET in general, working with Office interop is one of the biggest gateways to spaghetti code-induced madness. I was doing something for my internship, I don't remember what, and I took one look at screwing with Excel spreadsheets in code and nope'd right outta there.

1

u/[deleted] Jun 29 '21

N

1

u/[deleted] Jun 29 '21

E em.n niuujbkb.