Adding a macro file to an XLSX/XLSM file using Packaging - vb.net

I'm using System.IO.Packaging to build simple Excel files. One of our customers would like to have an autorun macro that updates data and recalcs the sheet.
Pulling apart existing sheets I can see that all you need to do is add the vbaProject.bin file and change a few types in the _rels. So I made the macro in one file, extracted the vbaProject.bin, copied it into another file, and presto, there's the macro.
I know how to add package parts when they are in XML format, like the sheets or the workbook itself, but I've never added a binary file and I can't figure it out. Has anyone done this before?

Ok I got it. Following TnTinMn's suggestion:
Open a new workbook and type in your macro. Change the extension to
zip, open it, open the xl folder and copy out the vbaProject.bin
to somewhere easy to find.
In your .Net code, make a new Part and add it to the Package as
'xl/vbaProject.bin'. Copy over byte-for-byte from the
vbaProject.bin you extracted above. It will be compressed as you
add the bytes.
Then you have to add a relationship to the workbook that points to
your new file. You can find those relationships in
xl/_rels/workbook.xml.rels.
You also have to add a content type entry at the root of the
document, which goes into the [Content Types].xls. This happens automatically when you use the ContentType parameter of CreatePart
And finally, change the extension to .xlsm or .xltm
I'm extracting the following from many places in my code, so this is pseudo...
'the package...
Dim xlPackage As Package = Package.Open(WBStream, FileMode.Create)
'start with the workbook, we need the object before we physically insert it
Dim xlPartUri As URI = PackUriHelper.CreatePartUri(New Uri(GetAbsoluteTargetUri("/", "xl/workbook.xml"), UriKind.Relative)) 'the URI is relative to the outermost part of the package, /
Dim xlPart As PackagePart = xlPackage.CreatePart(xlPartUri, "application/vnd.ms-excel.sheet.macroEnabled.main+xml", CompressionOption.Normal)
'add an entry in the root _rels folder pointing to the workbook
xlPackage.CreateRelationship(xlPartUri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument", "xlWorkbook") 'it turns out the ID can be anything unique
'now that we have the WB part, we can make our macro relative to it
Dim xlMacroUri As URI = PackUriHelper.CreatePartUri(New Uri(GetAbsoluteTargetUri("/xl/workbook.xml", "vbaProject.bin"), UriKind.Relative))
Dim xlMacroPart as PackagePart = xlPackage.CreatePart(xlPartUri, "application/vnd.ms-office.vbaProject", CompressionOption.Normal)
'time we link the vba to the workbook
xlParentPart.CreateRelationship(xlPartUri, TargetMode.Internal, "http://schemas.microsoft.com/office/2006/relationships/vbaProject", "rIdMacro") 'the ID on the macro can be anything as well
'copy over the data from the macro file
Using MacroStream As New FileStream("C:\yourdirectory\vbaProject.bin", FileMode.Open, FileAccess.Read)
MacroStream.CopyTo(xlMacroPart.GetStream())
End Using
'
'now write data into the main workbook any way you like, likely using new Parts to add Sheets

Related

Automaticaly open xls file on program start?

To provide our users to edit excel files without ms excel, we have made a simple app with visual studio 2012 and devexpress Spreadsheet module.
It is very simple to open excel file and use it.
But now only one excel file is being used (with multiple sheets), and I would like the file being used to be opened always on startup.
If I add the path and filename to command line arguments, noting happens...
Using devexpress components is very different then vanilla code for me, I am a complete beginner here, so I have no idea how to continue - can someone, please point me in the right direction?
I have made a procedure, to open the file dialog and load the file - I don't know how to "pass" it to devexpress, so the file actually loads to workbook.
Private Sub OpenXls()
Dim ofd As OpenFileDialog = New OpenFileDialog
ofd.DefaultExt = "xls"
ofd.FileName = "FILE"
ofd.InitialDirectory = "C:\ref_files"
ofd.Title = "Select file"
End Sub
As you have pointed out - using the dialog is not the right way.
After some googling I have find out that this should be a better way:
Dim workbook As New Workbook
workbook.LoadDocument("C:\ref_files\file.xls", DocumentFormat.xls)
I do not get any error, but the file is also not shown...
Do I have to display the document manually after loading?
If you're using openfiledialogue to open file then you must use command to load the specific file at Form.Load event.
The backslash char in ofd.InitialDirectory = "C:\ref_files"escapes the letter R to carrige return.
Change this line to ofd.InitialDirectory = "C:\\ref_files" (add another backslash).

Access Data From Already Open But Not Saved Excel File In Separate Instance

I have two excel files in separate instances of excel. I want to take data from one instance of excel to another. This seemed simple as I know that path of the file that I want to pull data from. However, the file I want to pull data from is used by a separate program where it opens up the file I want to pull from (a template), populates it, but does not save it. So each time this external program is running it is using the file I want to pull data from but since it never saves it I am having a hard time pulling data from the template file. I have used the getObject() function which successfully pulls data from the file as I know the file path but the fields are of course empty as when the external program used the file, it only filled in the data but never saves it. How can I do what I am asking?
Building on Scotts suggestion
Since you know the full path and name of the other workbook, use GetObject to reference it
Use .SaveCopyAs to save it
Open the saved copy in the local instance
This code goes in the file running in your instance of Excel
Sub Demo()
Dim wbRemote As Workbook
Dim wbLocal as Workbook
' Get reference to the workbook running in the other instance of Excel
Set wbRemote = GetObject("C:\data\Temp\TemplateBook.xlsx")
' Save a copy
wbRemote.SaveCopyAs "C:\data\Temp\Temp.xlsx"
' remove reference
Set wbRemote = Nothing
' open copy in this instance
Set wbLocal = Application.Workbooks.Open("C:\data\Temp\Temp.xlsx")
' work with object wbLocal
' ...
End Sub

How to refer to a relative path in SolidWorks VBA

I am writing a macro in SolidWorks VBA that is supposed to open all the parts of an assembly and update their design tables. When the assembly and parts are copied to a new location, I want the macro to open the new (copied) parts in their new location. How do I refer to a relative path for the parts instead of an absolute path.
Or: How do I program VBA to change the file location of the parts to the current folder?
I've found some info on how this works in Excel VBA, yet I can't seem to translate this to SolidWorks VBA.
Dim folderpath As String
folderpath = Application.Activeworkbook.Path
I can't figure out with what I should replace "ActiveWorkbook" to get the location of the current assembly. Searching the object browser in SW VBA doesn't show anything usefull either.
You'll want to use swModel.GetPathName. Here's an example.

Macro (VBA) in Excel is failing on colleagues PC but works on mine when colleague is logged in

I'm developing VBA tools to automate a series of long winded administration tasks, the code runs fine in the following circumstances.
When I am logged into my PC
When my colleague is logged into my PC
When I am logged into my colleagues PC
However it fails to complete correctly, when my colleague runs it on her PC.
The specific area it is failing in is:
'creates 2 dims for location of the two files that need opening based on the critera set on the home page
Dim newdata As String
newdata = Range("f11").Value
Dim olddata As String
olddata = Range("f12").Value
Dim fileextension As String
fileextension = Range("f14").Value
Dim fulllocationolddata As String
fulllocationolddata = Range("f13") & olddata & fileextension
Dim fulllocationnewdata As String
fulllocationnewdata = Range("f13") & newdata & fileextension
'open file containing OLDDATA c&p previous days data to the conversion tool
'then shuts the old data workbook
Workbooks.Open Filename:=fulllocationolddata
Workbooks(olddata).Activate
Worksheets("sheet1").Select
Range("A1").CurrentRegion.Copy
Workbooks("Stockfile Conversion Tool.xlsm").Activate
Sheets("OLD STOCK").Activate
Range("A3").Select
Selection.PasteSpecial
Workbooks(olddata).Activate
Worksheets("sheet1").Select
Workbooks(olddata).Close SaveChanges:=False
The final line (Workbooks(olddata).Close SaveChanges:=False) does not shut the workbook, then later in the macro I open another workbook of the same name but as it's already open it just activates the window and the rest of the code falls apart.
If anyone has any ideas where I'm going wrong it would be appreciated.
Thanks in advance for your assistance
Plan303
Making my comments to an answer here:
Whether Workbooks("Name of the Workbook") is working or not depends on the settings in System control - Folder Options - View - [ x ] Hide extensions for known file types.
If this is set, then Excel's file extensions .xlsx, .xlsm, ... are not visible in Explorer or other file listings. Only the file names of the Excel files are visible. If so, then Workbooks("Name of the Workbook") will work.
If Hide extensions for known file types is not set, then Excel's file extensions .xlsx, .xlsm, ... are visible in Explorer or other file listings. If so, then Workbooks("Name of the Workbook") will not work. Then only Workbooks("Name of the Workbook.xlsx"), giving the name and the extension, will work.
But Workbooks("Name of the Workbook.xlsx") will also work if Hide extensions for known file types is set. So using the full name inclusive extension should be preferred.
So for the concrete question:
If olddata only contains the name of the workbook and not the file extension then Workbooks(olddata) will only work if Hide extensions for known file types is set in Folder Options. It will fail, if that option is not set and the file extensions are visible in Explorer. But Workbooks("Stockfile Conversion Tool.xlsm") will always work independent of whether Hide extensions for known file types is set or not. So Workbooks(olddata & fileextension) should also always work if olddata only contains the name of the workbook and fileextension contains .xlsx for example.

copy image to clipboard and let it be pasted as file (vb.net)

I have a picture box, and if I use snipet below:
Clipboard.SetImage(PictureBox.image)
Then I can only paste the image into things like Paint and MS word. I can't paste it as a file into a folder/desktop.
So how can I copy the image to the clipboard and if gets pasted to a folder then it becomes a file?
If you're using .net and your ultimate goal is to save the file, there's a LOT easier way,
Here the code in C#, porting it into VB.net won't be hard, I'm just too lazy to do that :)
Anyway, you do have to save it somewhere before you can paste it so...
It loads the file to the Picture box and again saves it to a file, (lame, I know)
and set the clipboard data as a copy operation
then when you paste (Ctrl+V) it, it gets pasted.
C#
__
Bitmap bmp;
string fileName=#"C:\image.bmp";
//here I assume you load it from a file, you might get the image from somewhere else, your code may differ
pictureBox1.Image=(Image) Bitmap.FromFile(fileName);
bmp=(Bitmap)pictureBox1.Image;
bmp.Save(#"c:\image2.bmp");
System.Collections.Specialized.StringCollection st = new
System.Collections.Specialized.StringCollection();
st.Add(#"c:\image2.bmp");
System.Windows.Forms.Clipboard.SetFileDropList(st);
</pre>
and viola tries pasting in a folder the file image2.bmp will be pasted.
Here's basically what #Vivek posted but ported to VB. Up-vote his if this works for you. What you have to understand is that explorer will only allow you to paste files, not objects (AFAIK anyway). The reason is because if you copy image data to the clipboard, what format should it paste in? PNG, BMP, JPG? What compression settings? So like #Vivek said, you need to think those over, create a file on your own somewhere on the system and use SetFileDropList which will add the temp file to the clipboard.
' Add it as an image
Clipboard.SetImage(PictureBox1.Image)
'Create a JPG on disk and add the location to the clipboard
Dim TempName As String = "TempName.jpg"
Dim TempPath As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, TempName)
Using FS As New System.IO.FileStream(TempPath, IO.FileMode.Create, IO.FileAccess.Write, IO.FileShare.Read)
PictureBox1.Image.Save(FS, System.Drawing.Imaging.ImageFormat.Jpeg)
End Using
Dim Paths As New System.Collections.Specialized.StringCollection()
Paths.Add(TempPath)
Clipboard.SetFileDropList(Paths)