I have a word doc that I have been writing some VBA for, and in it I use this a lot:
LoadPicture(ThisDocument.Path & "image_name")
However, for this to work I've needed users to have an "Images" folder in the same directory, which is very inconvenient.
Is there a better way to include resources in a VBA project?
Thanks!
I use the QuickParts in Word to store them. Create a new document template, paste the image onto the page and select it. Then go to QuickParts and Add it to the Library and save your template. Repeat for each item to add. Then use VBA code to apply the template to the document and your QuickPart will pull in from the template. Then use a second macro to pull your image from the QuickPart.
Why not check if the folder exists on document open and create the folder if it does not?
Private Sub Document_Open()
Dim imagesFolder As String
imagesFolder = ThisDocument.Path & "\images\"
If Len(Dir(imagesFolder, vbDirectory)) = 0 Then MkDir imagesFolder
End Sub
Related
There seem to be many descriptions online about attachments with duplicate names. When you have a lot of attachments, it's not practical to manually save each one to a different name. It's also prone to manual error.
There are many code snippets for automating the saving of attachments, but I hunted for through quite few and experimented with VBA code snippets before assembling a complete subroutine. Hoping that my answer posted below will save someone an afternoon.
Here is VBA code that does what is needed:
Public Sub svAttachByIndx(Folder As String)
Set fso = CreateObject("Scripting.FileSystemObject")
For Each Atchmt In Application.ActiveExplorer.Selection(1).Attachments
FilePath = Folder & "\" & Format(Atchmt.Index, "00.") & _
fso.GetExtensionName(Atchmt.FileName)
Debug.Print FilePath
Atchmt.SaveAsFile FilePath
Next
End Sub
The VBA editor is opened from Outlook using Alt+F11. To use the above subroutine, select the message containing the attachments of interest, open the Immediate Window in the VBA editor (Ctrl+G), and enter:
Call svAttachByIndx("C:\User\Your.User.Name\Path\to\desired\folder")
I am trying to open an existing .xls file and overwrite the contents in one spreadsheet (tab).There are many tabs on the file and many have pivottables and other visual presentations.
I have tried Spreadsheet and axlsx. Axlsx has great controls but overwrites the entire file including any other created tabs. Spreadsheet will open and edit a file but you have to copy the other tabs which will remove the excel formatting.
Is there a way to use Ruby to add data to only one tab in a spreadsheet without changing content in the other tabs?
Update:
Here is what I am testing now using the Spreadsheet gem. I can open a spreadsheet that has multiple tabs where one tab contains a pivottable, another contains a chart, and another the raw data. They have to be saved out as a new doc otherwise you get a File Format is not Valid error.
open_book = Spreadsheet.open('../data/exports/test_output_dashboard.xls')
puts "#{open_book.worksheet(0)}"
puts "#{open_book.worksheet(1)}"
puts "#{open_book.worksheet(2)}"
open_book.write('../data/exports/test_output_dashboard_2.xls')
If I just open and resave the new document is fine, a working copy of the original. However, if I edit the tab with the raw data as in this code then when I open the file it is shown as needs to be 'repaired' and none of the tabs show the correct information.
open_book = Spreadsheet.open('../data/exports/test_output_dashboard.xls')
puts "#{open_book.worksheet(0)}"
puts "#{open_book.worksheet(1)}"
puts "#{open_book.worksheet(2)}"
new_row_index = open_book.worksheet(1).last_row_index + 1
open_book.worksheet(1).insert_row(new_row_index, row_2)
open_book.write('../data/exports/test_output_dashboard_4.xls')
Any suggestions for adding data to one tab of an excel doc while keeping the other tabs intact would be greatly appreciated. The solution can be any gem or could be any language or automatable tool.
UPDATE:
Here is an example Excel dashboard that I am using for testing. I am writing rows into the data tab. https://dl.dropboxusercontent.com/u/23226147/test_output_dashboard.xlsx
UPDATE:
With RubyXL I can open and inspect the content of each tab but the saved doc cannot be opened by Excel.
workbook = RubyXL::Parser.parse("../data/exports/test_output_dashboard.xlsx")
puts "#{workbook.worksheets[0].inspect}"
puts "#{workbook.worksheets[1].inspect}"
puts "#{workbook.worksheets[2].inspect}"
workbook.write("../data/exports/test_output_dashboard_5.xlsx")
If you're just looking for a quick tool, RubyXL might do the trick for you:
https://github.com/weshatheleopard/rubyXL
It parses existing .xlsx .xlsm files and has a decent set of documentation.
You could try cloudxls.com API. You can merge data into existing xls and xlsx files using its API. If that is not an option, you most likely have to use some java libraries like Apache POI.
Solution for Windows users only.
I use to modify Excel spreadhseet with the gem win32ole and it works fine.
In case it is interesting for you here is a short sample to open a file and activate a given tab:
excel = WIN32OLE.new('Excel.Application')
excel.visible = true
filepath = 'e:\tmp\file.xlsx'
cur_book = excel.workbooks.Open(filepath)
sheet_name = 'sheet1'
cur_sheet = cur_book.Worksheets(sheet_name)
# put value 10 in Cell(2,2)
cur_sheet.Cells(2,2).Value = 10
Official documentation: http://ruby-doc.org/stdlib-1.9.3/libdoc/win32ole/rdoc/WIN32OLE.html
I am writing an add in for excel. It is supposed to create a new worksheet, then copy data from the pre-existing worksheets.
Now the whole add-in works on another excel document. But the one I need it to work in has disabled the ability to add new worksheets.
Can someone please tell me what code enables this?
Sub Auto_Open()
Dim WSheet As Worksheet
On Error Resume Next
Set WSheet = Sheets("DispersionList")
On Error Resume Next
Dim works As Worksheet
ActiveWorkbook.Unprotect
If WSheet Is Nothing Then
Set works = Worksheets.add(after:=Sheets(Worksheets.Count)).Name = "DispersionList"
Call makeFormat
Worksheets(1).Activate
End If
DispersionForm.Enabled = True
DispersionForm.Show
End Sub
Like pnuts mentioned above, if you search Google/SO, you will find plenty of posts which talk about hacking the password. However my answer is not about hacking the password but about alternative(s) that you can employ to play around with the workbook. If someone protected the Excel file then it is obvious that you are not meant to fiddle with it :) And as a personal choice, I do not assist with hacking. If you can get the password from the author then there's nothing like it.
There could be 2 kinds of protection that I can think of which will stop you from adding sheets to that workbook.
Via Code
Workbook Structure is protected.
If you want to play around with the workbook with not the intention of hacking it then you have couple of options.
If the protection is via code, then that file could be an xls or xlsm file which supports macros. Here what the author might have done is written a code where the moment you add a sheet, it is immediately deleted. In such a case, simply resave the file as an xlsx file. Close the file and re-open it so that you can then add sheets to it and play with it without hacking it.
If the workbook structure is protected then there is nothing much you can do except copy (if it allows) the cells from the existing workbook to a new workbook and then play with it
I have thousands of html files, and need to save each of them as txt, using firefox.
If I do this job manually, I would open each html file in firefox, click the File menu, click the 'Save Page As' menu item, then select the format as 'TEXT', and save to local disk.
But how to automate this job?
Any script/tool can help this?
Thanks.
If your goal is to get firefox to strip the html out of each page and save just the text, then there are a ton of options. I'm not aware of any firefox add-ons that will be intelligent enough to loop over every file in a directory in order to perform a macro, so here are some options:
Refer to this SO question regarding how to use python to strip the html from each file. It provides examples for both the built in HTMLParser module and for using BeautifulSoup
Use Selenium to automate your webbrowser: http://seleniumhq.org/
If you know javascript, you can use PhantomJS:
http://www.phantomjs.org/, which is a headless web browser that you
drive with javascript scripts.
I have thousands of html files...
Do you actually have these files on-hand, or are they online?
...and need to save each of them as txt...
Any text editor should be able to save the data within (i.e. why use FireFox), and I think a straight rename of .htm or .html to .txt. will work (at least on any Windows system). Or do you mean: save just the displayed text of the HTML file?
EDIT:
First, start off with this link, which has a good explanation of how to get started with shdocvw, which you will need to do this.
Once you have the reference set up, using the functions
Function GetNewIE() As SHDocVw.InternetExplorer
and
Function LoadWebPage(i_IE As SHDocVw.InternetExplorer, i_URL As String) As Boolean
from the link (just copy into your project as described in the link) to load your individual html files, using a loop to get through each file. (Excel would be good for this, because you can put your list of files into the cells, and cycle through each cell to retrieve.) I have never done something like this with so many files, so I cannot guarantee this will work, unfortunately...
Dim IE As SHDocVw.InternetExplorer
Dim lRow as Long 'Long in case you have a LOT of files
Dim iFNum As Integer
Dim sFilePath As String
Set IE = GetNewIE
For lRow = 1 To 5000 Step 1 ' Assuming you have 5,000 html files, so 5,000 rows with the paths to each
sFilePath = ActiveSheet.Range("A" & lRow).Value ' This should also include the filepath. i.e. "C:\dir\..."
If LoadWebPage(IE, sFilePath) Then
iFNum = FreeFile(lRow)
Open sFilePath & ".txt" For Output As iFNum
Write #iFNum, IE.Document.InnerText
Close #iFNum
End If
Next lRow
I need to extract the template from a powerpoint presentation and apply that template to another presentation.
After my search, I found two commands:
1) get template name of presentation
The above commands gives the template name, but not the path where it is stored. I am unable to find that.
2) apply template presentation file name "Macintosh HD:Users:Shared:Zesty"
The above command applies the "Zesty" design template to the presentation. But again I had no success as I don't know where this design template is stored. After googling I found the location can be: "/Users/sanjeev/Library/Application\ Support/Microsoft/Office/User\ Templates ". But I found no templates there.
So, is there any way to extract the template from a powerpoint presentation and apply that template to another presentation??
Thanks
PowerPoint doesn't store the path to templates in presentation files. There's no need to. It never refers to external templates except when the user/your code applies them. At that point, it stores a copy of the template IN the PPT file.
Extracting a template from the PPT file is simply a matter of opening the file and saving it as a template.
But you don't even need to do that unless you want to. Since every presentation contains its own template, you can apply the template from one presentation to another; no need for an actual template file.
In VBA, you'd do:
ActivePresentation.ApplyTemplate FileName:="[path to file whose template you want to apply"
I don't do Applescript, but it should be possible to translate that.
This might be 10 years late :-) but here is some AppleScript that does it:
-- Create a presentation to write to
set newPresentation to make new presentation
-- apply a template file to this new presentation
apply template newPresentation file name "/Users/martinpacker/Documents/template.potx"
(Obviously I came across this question while looking for an answer myself - and eventually figured it out. It might help someone in another decade or so.) :-)