How to read a .csv file embeded in a Word file - vba

I currently am working on an MS Word form where I use VBA to import data from a .csv file. The form is a phone request for employes who work outside, and my .csv is a list of phones that is updated for time to time. My boss would like to have this .csv directly into the Word to have only one single file to handle.
I found how to include (embeded) it, I go to "insert" tab on the ribbon, "Object" button, then "Create from File" tab. I check "display as icon" but not "Link to file".
My .csv is only used to import data through a macro when opening the Word form. Data are then paste in a variable that is handled in VBA to fill combos. The .csv is periodicaly updated and re-embedded so the form is updated also. I don't plan to alter the .csv in VBA in any way.
The objective is eventualy to give the Word document to someone else who will embed a new .csv when needed, but won't look at the code.
The problem I am working on is to reach the .csv and import data.
Another solution, thanks to #Cindy Meister's and #SlowLearner's comments for pointing out the idea, would be to use Custom XML Parts. I will make some additional researches about that.
Moreover, with my currently very limited experience on programming and VBA, I'm not sure how to handle that (I'm not even sure it's possible). The lack of quality documentation on VBA doesn't help either.
After comments of #Cindy Meister and #SlowLearner, I reformulated part of the question to be more precise, although I feel it's getting very long and and messy. I probably should make my own research to narrow down the problem.
ps: that's my first question here, so any advice is welcome, and please be nice with me :)

Related

bunch of weird characters instead of text

I really count on your help.
Well, for hours I've been trying to have my excel files inserted in sql database as a table through the msvs and no matter what I have tried, the output data is always some sets of weird characters, boxes and etc. First I thought that it could be the PC language settings, I've tried changing them to my local one, changed the system locale to my own language and etc. But there was no result.
Then I just opened an excel file, typed there a single letter "d" and tried to open it in notepad++ to check whether the result will be the same or not. It was again a big pile of boxes and symbols instead of single letter "d". *Tried to change the encoding in notepad++, didnt work either.
Do you have any idea what can help me? It's really frustrating.
Thanks
Sabuhi.
Of course that happens, this is common behaviour when you are trying to open a compiled program in a text editor.
As #Gary'sStudent suggested, if you want to open the excel file
directly in a text-editor, it needs to be saved as .csv
Ctrl+Alt+S
Select the .csv file type:
Save (may need to adjust your table data to fit the .csv formatting
Either way, it sounds like you're a bit confused with what you're trying to achieve. If you want to export your Excel file as a database (which has it's own quirks and is not exactly the best approach to databases) then you should be able to view your database in your database editor - whatever you're using, after you have imported it and compiled it, instead of trying to open it in a text file!

Select three rows of text in PDF using VBA

I am trying to use VBA to select three rows of data in a PDF file and copy them to the clipboard. I have tried third party libraries but I still can't seem to find a simple solution. I can use the cursor to select the data and copy it, so I just want to automate this step with VBA.
I have looked high and low for an answer to this and I feel like it might be really simple and I'm just missing it. I assume I could just use the "highliteList" method in the acrobat library to select the rows, but I don't know how to specify where to begin the selection. There is a header on each page, so I just want to say something like:
For Each header In pdf.pages
NextLine.SelectRow
NextLine.SelectRow
Next header
Selection.CopyToClipboard
Is this possible? I know those methods probly don't exist, I was using it as an example. Does anyone have experience with doing this? Thanks in advance for any help
I found a solution for all those interested. I used Bytescout PDF extractor library to convert the file to .xls format. Then I just parsed out what I needed in Excel since Excel is easy to work with via vb.net.

Create documents without dependency on Office programs?

Is there any way for my app to create a document that will need saved and printed without utilizing some external software? Currently I have a spreadsheet that does a bunch of calculations then a button that runs some VBA to export 2 sheets to a PDF, then it saves and prints it. I want to port this spreadsheet into a .NET app.
I have experience with everything EXCEPT this: how do I re-create these 2 documents that are currently in Excel, without having to utilize Excel? My whole goal here is to get out of Excel...because I hate it, and I'd like to send this app to clients across the country. I really don't want to have to tell clients that they need Excel to use this. I might as well just leave it in the spreadsheet if that's the case.
I'm sorry if this is sort of "nooby", but I'm not sure what the best course of action here is. Should I try to mimic my Excel sheets on 2 hidden forms and save/print those? Should I write some HTML to produce these forms in a browser and save/print from there? Are there any other options here? I'll probably end up saving as XPS if I can find a way to get out of Excel. Would love some pointers if you have any ideas. Thanks everyone!
Edit: a little more info...I don't need help with the calculations, or exporting PDF's. I don't need any help regarding Excel or VBA. The workbook has 1 input sheet where users enter data. The results of the calculations appear on 2 other sheets in this workbook. These 2 sheets are currently exported to 1 PDF using VBA, which is then saved and printed using VBA. These spreadsheets are not "spreadsheets" like you may be thinking. They are actually "forms", for lack of a better term, that the user will never edit after running the macro to export them as PDF's. They contain text, pictures, shapes, etc. Excel is merely the medium currently being used to create these documents. My goal is to build this project in .NET and get us out of Excel, but I'm not quite sure how to reproduce these 2 forms within my app without utilizing Office. Think of them as templates. After the user enters data in my app, it will do some calculations, and the results need to appear on 2 forms that need printed and saved on the user's machine. How do I recreate these 2 forms in .NET?
Of ya, vb.net, winforms (although I could use WPF as I haven't started yet), 4.0 framework :)
Here is what you can do:
Add an empty Excel file into your resources and use it as template
When your program is to save data, you can take that file from resources and save it to hard disk.
Connect to Excel file using "Microsoft.Ace.oleDb."
Save and read data in Excel just as it was a db table - there are plenty examples on the net. Google for it.
For this project you don't need Excel application on the machine.
Now, if your concern is Excel, you don't have to worry. Your clients can use OpenOffice, for example. Or, you can save data in CSV format. CSV is not Excel. You can create your own text format and your clients will be able to read it with the Notepad. You can do HTML/XML combination and have your html page load whatever xml you supply.
Seriously, create a spreadsheet and tell your clients that they can open it with their favorite spreadsheet editor.
So instead of using a third party program or anything fancy , you could just read in the excel or xsl file and spit it out? Just write some code to format the data properly for users... There is a similar question that may help you with a tutorial - Here But this is for java, Are you using c# or vb ? .Net 4.5 ? razor ?
You can create the PDF's from code using tools like iTextSharp. Or use fillable PDF templates and use iTextsharp to fill in the form. You will need a program to print the PDF, I use the Foxit Reader. I'm sure there are some full featured PDF tools out there that include printing.
You can also doing printing from VB.Net but I would guess mixed media documents would be difficult.

Automated Excel document creation using VBA

The managing director at our company wants me to produce an automated monthly document that saves to a certain place on our system so that he doesn't have to manually input all of the data. I have set up so that the document can save to the correct place in the correct format but my knowledge of VBA is not great.
Tackling this from a 1 question at a time point of view I suppose my question would be is it possible to create 1 very long macro that will accomplish many different tasks over several workbooks. For example we have a report that comes from our ERP (Baan) and shoves all of the data into one cell. Is it possible to create a macro that will accomplish formatting text to columns, then copy data from a cell based on a row reference and then take said data and paste it in to a different workbook? Would it then be able to save the workbook all from just running one macro and if so how long will all of that take once the macro is executed?
Yes I believe this should be do-able, keeping in mind that the file names + location remain the same (otherwise you'll have to edit each month). Create different Subs/Functions and call them in one main macro.
The easiest way is probably to do it step by step. Record macro's and see whether that already helps you out and if not use google & stackoverflow for help! :)
it is entirely possible - but in my Opinion VBA is not well suited to the task. The editor is atrocius at best and it is easy to produce highly specific "spaghetti code".
File operatione are possible, but are not nice. Error handling is 80s style with lots of goto.
So if you want to build something maintainable, build an external Application using Interop or epplus (.net package for reading /writing to excel documents) or an .net addin for office.

Manipulate Excel workbooks programmatically

I have an Excel workbook that I want to use as a template. It has several worksheets setup, one that produces the pretty graphs and summarizes the numbers. Sheet 1 needs to be populated with data that is generated by another program. The data comes in a tab delimited file.
Currently the user imports the tab delimited file into a new Workbook, selects all and copies. Then goes to the template and pastes the data into sheet1.
This is a large amount of data, 269 columns and over 135,000 rows. It’s a cumbersome process and the users are not experienced Excel users. All they really want is the pretty graphs.
I would like to add a step after the program that generates the data to programmatically automate the process the user currently must do manually.
Can anyone suggest the best method/programming language that could accomplish this?
POI is the answer. Look at the Apache website. You can use java to read the data and place it in cells. The examples are very easy.
You can can solve this, for example, by a simple VBA macro. Just use the macro recorder to record the steps the user does manually now, this will give you something to start with (you probably will have to add a function to let the user choose the import file).
You said you have some data generated by another program. What kind of program? A program that you have developed by yourself and where you can add the excel-import functionality? Or a third party program with a GUI that cannot be automated easily?
And if you really want to create an external program for this task - choose whatever programming lanuguage you like as long as it can use COM objects. In .NET, you have the option of using VSTO, but I would only suggest that for this task if you have already some experience with that (but than you would not ask this kind of question, I think :-))
Look here:
Create Excel (.XLS and .XLSX) file from C#
There's NPOI (.NET Framework version of POI) so that you can code in C# if you want.
If you use two workbooks - one for data and one for graphs - and don't update links automatically you can use a macro to get the data (maybe an ODBC connection if the file is in a format it can read - long shot) and then link the charts to the data workbook.
Use a macro to update the links and generate the charts and then send them out and hope no one updates the links.