How to copy from Excel to Word without using the clipboard? - excel

I've written a C# application that copies cell ranges of an Excel workbook as images in a Microsoft Word document, using the Excel.Range.CopyPicture or Excel.Range.Copy methods.
But processing big Excel files can take time, and if the user uses the Clipboard in an other application during the process, my application can crash.
I'm wondering if there is a way to copy Excel cell ranges as images in a Word document without using the clipboard, to avoid this kind of issues.
Thanks for your help,
Julien

There is a solution, I have used ages ago:
Copy the data to a new worksheet and save that worksheet as a pdf, in turn, extract the picture from the pdf and than save that picture as a new file. in Word, use the addpicture method to load the file from disk...
Very clumsy, but it is reliable....

What about copying the image to the clipboard like you're doing, but then immediately saving that image as an image in your code? That way, you can still use your old procedures and store the image internally - no longer having any dependancy on the clipboard.
I did the exact same thing in a question I asked just earlier this week - Look at my posted solution:
Control Excel Within WebBrowser Control
Just use the image as needed now that you have it stored internally and set it to nothing as soon as you can to save on memory.
Hope this helps

Related

VBA: Saving Excel range as image and pasting to PowerPoint vs directly pasting it into PowerPoint?

I'm pretty much done with my project of automating copy-pasting a bunch of ranges from Excel to a Powerpoint presentation. Now a co-worker remarked that I could save the relevant ranges as jpeg, pdf or whatever else and only then export it to Powerpoint.
This got me thinking. Is there an advantage of doing it my co-worker's way? The only one I (an inexperienced user) can think of is that I would have the data I exported on my hard drive as separate files. Are there any other advantages or even disadvantages to this method?
The main advantage of saving it through an image/pdf is the staticness of the output file. Not only on the values, but also on the format, across all users, despite the differences of versions/preferences/fonts... [Side note: I do really prefer .pdf or even .emf as these formats are vectorial, so they do not get pixelated if you are zooming in]
I would suggest using a temporary file for that. Remember to kill the file after adding it to the PowerPoint.
Here is a pseudo-code to give you the general idea:
Get the Temp file name [and file path]
Export/Print your range/tables (in whatever format you want) using the temporary file name
Import that file as an image in PowerPoint (remember to set LinkToFile:=False)
Delete the temporary file
No matters if you do the 1 or 2 way ,
About eficiency i think the Pasting directly in powerpoint is more useful and powerful bcoz is less 1 step (saving in a Directory and them put it on ppt)
If u need this informations (jpgs /pdfs) or just want to have them saved so u should use your friend's tip , but if u dont need it , just keep your way of doing the copy and paste

Excel - allow saving as pdf but no saving of original document

I have a heavily formulated workbook which will be used by staff members who will paste in potentially confidential client information. The workbook then comes up with graphics and data to summarize the imputed information
In an ideal world, the people using the program will be able to Save specific sheets as a PDF and would be able to close the program without saving changes to the worksheet but Excel always demands to save the template first, THEN will allow PDFs. This is obviously a problem as if a person uses it, paste's in a client's information and saves the result as a PDF then whoever next opens the Excel workbook will have that previous client's information showing.
SO. I either need a way to tell Excel to not require compulsory saving to allow PDF conversion or another option which will result in the same thing.
I should also note that the workbook has to allow users to paste in information so I don't imagine a Read Only will help :/
I have also attempted using a Macro-Enabled Template which still has the same problem
I imagine there's a ridiculously simple solution to this.
Thanks in advance

Batch save pictures from multiple excel files in a folder

I have about 10000 excel files, that in a specific cell of all of them there is a picture. I need a script to read all files and save the picture with the same name of the excel files in a folder.
Could you please help with that?
Thanks.
This method is based on a number of assumptions:
All the files (10000) are located in a know folder,
All files are named according to a paradigm that can be reproduced programmatically (if not, you can get the list of files within the folder, store the list within an array, and loop through the array),
Pictures are always within the same worksheet or, if in more than one, the names of the worksheets can be reproduced programmatically,
The filenames to be used to save the pictures can match (at least as a seed) the one of the Excel the pictures are extracted,
You will manage to write some basic VBA.
Note that for the VBA you have at least two options:
Write it within an EXCEL that will only serve as the extraction engine, or
Write it as a stand-alone file and run it via DOS commands.
The VBA logic:
Create the outer loop that processes a single file,
Within the outer loop, generate the name of a file to be open,
Open the file using Workbooks.Open VBA function,
Select the worksheet and the cell containing the picture,
Use the Workbook.SaveAs to save the picture (you will need to specify the type of file to be used, e.g. .bmp).
As a simple and very efficient tool to get the code (at least) partially generated by Excel, you can RECORD a MACRO for each action and then stop recording. You will see the code generated (you will need to access the VBA mode). You can copy-paste the generated code into your development (you might need to do some simple adaptations though).
That's it.
Hope you manage. Good luck!!

Save logfile within Excel file using VBA

I am trying to develop a manner in VBA to track changes in a document without having to hide the contents in an extra sheet within a workbook.
I understand that if you change the extension of an Excel file to ".zip", you can access the Excel document as components sorted into directories. Is there a way to save and write to a text file within one of these directories so that I can access it every time the document is opened, without having to have the user drag a log file along with the Excel document?
Some facts:
When Excel opens the file, the file is blocked by Excel. There is no possibility to write to that file within VBA
You can store additional data into that file externally or after the Excel workbook has been closed
You would need to have code externally from the workbook to accomplish writing to that file after it has been closed. You may want to use VSTO or an oldschool Excel Addin.
you have to ensure that Excel will not destroy your changes when restructuring or repairing the file.
In the first run, your idea sounds very natural, to not use sheets from a programmer's point of view. You only have full control on Excel files when
you use external libraries (e.g. Spreadsheet Gear) or
you remote control Excel via automation.
you use openxml SDK for Excel
you use VBA
You could insert additonal information and take care that this information is not skipped by Excel.
When you want to do the tracking this way, I would suggest you to use an Excel Addin. There is actually no need for installation when using this kind of Addin. Attach to open workbook and close workbook events and ensure that all changes are written to the Excel Workbook after it has been closed. Certainly you would have to attach to all kind of other events to track all changes to the workbook. You may need to have in mind that there can be more than one workbook opened at a time.
Actually there are alternatives.
write your logging code in VBA or whatever fits
abstract away how your persist the code (e.g. use a data provider)
think about these two alternatives to store logging data:
You can save logging data in cells of excel. When using a "newer" version of excel, you have a limit of 1 million rows. You may want to implement a rolling mechanism that ensures that you never go over the border of 1 million records. (you may be dont want to track a million changes)
You can use the document properties to store you information as xml.
Last but not least, the most obvious: Why not using Excel's functionality of tracking changes? Understand track changes in Excel 2013

excel add in to save internal data?

I am new to writing excel add-in (in C#) and trying to figure out the right way to save some internal data structures so I can restore the state the next time file is opened. I can convert data in xml or base64 strings if it makes things easier. I don't want to maintain a separate file and would like to embed this information inside excel worksheet.
Many thanks for your help.
Use a cell in an invisible sheet (you can name it, for example, "internal data sheet") for storing the information. Excel sheets have a Visible property which can be set programmatically to `xlVeryHidden' which means it can only be made visible again by a program. Here you find some more information:
http://support.microsoft.com/kb/142530/en-us

Resources