0
votes

How can I use a office 365 COM component on a computer without office 365 installed?

I have this system that imports data from SQL server to Excel, pretty straightforward actually. Maybe the only problem is I use loops to allocate which cells to put data on.

Why do I concluded that I need office 365 components?

First I used a computer with office 365 on it, I could import the data into the excel sheet.

Afterwards, I recompiled the same code on a machine without office 365, I can only get a few lines on my excel sheet. Interestingly though, the lines are consistent, only appearing on the 77th row but the data is imported the same way as the other rows.

I also tried a blank document to see if the formatting was at fault, but the import is still unsuccessful.

I tried to change the excel type too, from .xlsx into .xls but the result is the still unsuccessful.

I then recompiled the code on a computer without office 365, then installed office 365 in it, the code now works fine! After uninstalling the office 365 on the same computer, the code didn't worked anymore.

I tried to copy and paste the office 365 component which I think helps to upload succesfully (MSO.DLL), which is located at

C:\Program Files\Microsoft Office\root\vfs\ProgramFilesCommonX86\Microsoft Shared\OFFICE16. 

When using on a computer without office 365, the code doesn't work.

IF you want to know what kind of code I have during the import, I have this code:

                For e = 0 To dt.Columns.Count - 1 
                     objWorksheet.Cells(MyRow1, MyColumn) = dt.Rows(a).Item(e).ToString
                    MyColumn = MyColumn + 1
                Next

I am running out of ideas on how should I work through this, I am thankful for any suggestions that will be raised.

1
It is not exactly clear what you mean by import. It's fairly easy to construct a .CSV file that can be read by Excel or any other number of programs. If you want to manipulate the Excel object model like you are doing, you need to install Office or at least Excel. Microsoft does have specifications for their file formats so that you could write code to save as .XLS or .XLSX, but it's a lot of work. Writing as .CSV is not that difficult to figure out. - Joseph Willcoxson

1 Answers

0
votes

You can compile programs that use Office automation on a machine that doesn't have Office installed. Just find the .dll and/or .tlb files that you need to reference, copy them onto your compile machine and go.

You cannot run programs that use Office automation on a machine that doesn't have Office installed. Period.

Generally speaking, it's safer to generate Office documents directly rather than automating Office to construct documents. Automating Office is like pretending to be a user -- you have to have the software installed, run it, and then move it through the same steps that a user would do when using the software. Generating a document is just producing a set of data that, conceptually, isn't any different than a CSV or any dumb text file.

If you're planning to do this on a server or other non-interactive environment, you absolutely should generate the Office documents directly rather than automate. Microsoft has been warning us about the many (many) pitfalls of automation on servers for decades now. There is no safe way to do that. Period.

To generate an Office document, I always recommend the OpenXML SDK, which is a Microsoft toolset specifically for this. There is a NuGet package for version 2.9, which I would recommend using. There is also the OpenXML Productivity Tool, which allows you to open any Office document and see the literal code that it would require to build that document programmatically. The only way I know of to get this tool is through the download link for version 2.5 of the SDK (click the Download button, and you'll be given the option of downloading the SDK and/or the productivity tool).