[VB. NET] extract SWF from PPT

Source: Internet
Author: User
Tags ole
Failed to learn ~ First Paster ~~~~~~~~~~~~~~~~~

'The online has seen some methods to extract SWF in the PPT over the past few days, so I realized it through programming. After Embedding several other office files, you can simply save them to another webpage .....
'The idea is as follows:
1. Use the COM Object of the PPT to traverse the slides and copy the embedded SWF control to the clipboard.
2. Access the clipboard object and extract the embedded object content to get a stream.
3. process the excess part of the stream and save it as a file.
'There are still some problems:
'1. If only controls are available, but SWF is not embedded, I don't know what the code will get.

After negotiation, a satisfactory result is obtained... PUBLIC PARTCodeAnd software. But please followProgramProtocol ~~~~~~

Note that the "embedded" mentioned here refers to the built-in SWF file in the office file, that is, the embed move attribute of the flash ax control is set to true when designing the document.


Save the file. To see an animation in Excel, exit the design mode (click the triangle, pencil, or ruler icon to make it unselected ).

The encoding is followed by an overall call method:

private sub button2_click (byval sender as system. object, byval e as system. eventargs) handles button2.click
dim file as new openfiledialog
file. filter = "*. XLS | *. xls "
file. showdialog ()
If file. filename <> "" Then
addhandler MSExcel. progress, addressof progress
MSExcel. getswffile (file. filename)
removehandler MSExcel. progress, addressof progress
end if
end sub

Private sub progress (byval MSG as string)
Lstmsg. Items. Add (MSG)
End sub

Click the event button to call an open file dialog box to open the XLS document for extracting the SWF file. Then, to display the progress message, add the event processing function handle to the Progress function, after use, the handle is removed. The progress function is very simple. It only adds the message to a ListBox named lstmsg.
Next, implement the getswffile function. This function is set to a class (MSExcel) with only shared members. This class will have a shared Member:
Public shared event progress (byval MSG as string)
For future expansion, process and document references are put here:
Private shared app as Excel. Application
Private shared book as Excel. Workbook
To use this code, you need to import a namespace at the beginning of the class file. Here you use the method of specifying an alias:
Imports Excel = Microsoft. Office. InterOP. Excel
(Add Microsoft. office. interOP. excel. XX and the corresponding version of the Office. net reference, here I use office2003, so the versions added are all 11, and the reference of office will contain some namespace, for our program, only the namespaces containing some constants are used, so you can replace these constants with corresponding numbers without referencing the namespaces, the benefits of doing so will be described later)
Then, write a function to copy embedded objects in flash, and a function to save the clipboard content as a SWF file (this function is separated because it can be reused: this function is also used when processing other office file formats ). First, describe the first function. The function to implement is to copy the flash Ole content in the xls file. The simple method is to use Excel:
1. Open the document:
APP = new excel. Application ()
App. visible = Microsoft. office. core. msotristate. msotrue (in VBA, VB6, VB. in the IDE of net, the msotrue is actually-1. For VB.. Net Boolean relationship between IDE and CLR. If you have any questions, refer to VB. NET core programming or. NET Framework. In our program, "final" should replace these constants with-1, which will be explained later)
Book = app. workbooks. Open (file)
2. traverse and copy flash Ole content
There may be some minor issues to be discussed here. In fact, our operation is to copy the flash control, and other operations are automatically completed by the Office.
A. We all know that in Excel, there are N (3 by default) worksheets in the workbook, And the Flash we want is in a worksheet, therefore, we first traverse the sheets set of the book workbook, and then traverse the shapes set of each sheet to traverse the shape.
For I as integer = 1 to book. Sheets. Count
For J as integer = 1 to book. Sheets (I). shapes. Count
'Here for recognition
Next
Next
B. After obtaining the shape reference, we need to identify whether it contains the flash Ole content. How can this content be identified? We know that OLE objects all have an oleformat attribute, which contains Ole information. The progid attribute is a useful string, including the OLE object type, we use this attribute to identify:
If instr (1, Book. Sheets (I). shapes (j). oleformat. progid, "shockwaveflash. shockwaveflash", vbtextcompare) = 1 then
'Copy the code here.
End if
C. When we get a shape object reference containing flash Ole, we can copy it. In different office tools, the copying method may be different, however, in Excel, our object is shape, which supports the copy method, so we directly copy:
'The embedded flash file is copied to the clipboard.
Book. Sheets (I). shapes (j). Copy ()
D. Assign the processing permission to the clipboard handler:
Clipboard2file (book. Name, I, j)
This function has three parameters: the first is the file name to be processed, and the second and third are the data used during the time, in fact, these three parameters are passed in to name the final generated file. If necessary, you can separate the file storage function-maybe this is better, but I didn't do it, the reason is ...... I'm lazy

3. Clipboard processing:
This is the clipboard2file function above. I put it in a class (clipboard2fileclass), which has only one shared Member: our processing function.
Here, we use the clipboard object to read the Clipboard data:
Dim idata as idataobject = clipboard. getdataobject
This is a well-known concept. We can use it to read the content we want, but what is the content format? Of course it's an embedded object.
Dim MS as Io. memorystream = idata. getdata ("embedded object ")
So we get a stream containing the content of the SWF file-it is part of all the data we copy to the memory, which is also part of the SWF file we care about. Here, there is a hope that will be achieved soon? Maybe there's nothing to be happy with. How many lines of code have we written?
The next step is to process this stream. It is imperative to extract SWF files. If we are familiar with the structure of SWF files, how nice it is ...... (......) (* (-- & * % ...... ¥ % ...... $ )(()--(
Indeed, some comrades have made a lot of efforts to give us a local SWF file structure, and pay tribute to it and thank you to the original author!
Http://bbs.zhujiangroad.com/html/2008/6/250234.html
The above address is the correct one ~~~~~ The information we want to use is the first 8 Bytes:
1
2 FWS, CWS
3
Version 4
5
6 File Size
7
8
OK. start searching for the file header! I can't find this memory stream, convert it into an array, and then traverse it.
Dim BS as byte () = Ms. toarray () 'convert original streams into arrays to search for SWF file headers
Dim Point as integer location of the file header
For index as integer = 0 to Bs. Length-3
If BS (INDEX) = cbyte (& h46) orelse BS (INDEX) = cbyte (& h43) then 'Can be SwF or SWC, whether the file is compressed when the flash file is generated
If BS (index + 1) = cbyte (& h57) andalso BS (index + 2) = cbyte (& h53) then
Point = index
Exit
End if
End if
Next
In this way, the start position of the SWF file is saved in our point ~~ After obtaining the file length, you can write the array into a new stream and save it.
Dim filesize as integer = system. bitconverter. toint32 (BS, point + 4)
Dim by as new IO. memorystream (BS, point, filesize)
In this case, the by stream is our SWF file. Save it!
Dim filename as string = My. application. info. directorypath & "\ swffile \" & name & "-" & I & "-" & J & ". SWF"
If not my. computer. filesystem. directoryexists (my. application. info. directorypath & "\ swffile \") then my. computer. filesystem. createdirectory (my. application. info. directorypath & "\ swffile \")
If my. Computer. filesystem. fileexists (filename) then my. Computer. filesystem. deletefile (filename)
'Create a stream to save the memory flow to the disk
Dim dumpfile as Io. filestream = new IO. filestream (filename, Io. filemode. openorcreate, Io. fileaccess. readwrite)
'Save the memory stream
By. writeto (dumpfile)
The above process makes judgments and operations on the existence of folders and files to ensure that the program is "robust"-it cannot be caused by a file operation crash... It's a shame:
'Exit the used stream.
Dumpfile. Close ()
Ms. Close ()
By. Close ()
'Clear clipboard
My. Computer. clipboard. Clear ()
Now, the extraction of SwF in Excel has come to an end. You can test your code ...... It feels good, right? Even comments are included. Less than a hundred lines solve this problem...
The following is a test file and a written main program .. But there are some restrictions when using it. If you read the above carefully, I believe you already know how to do it ~~~~

/Files/zcsor/extract swftool test file .xls

/Files/zcsor/copy the swf.7z embedded in office

Finally, let's discuss the problems with this code ~~~ Adaptability. To support more office versions, you can perform the following operations ~~~ (Of course I didn't do it. I'm lazy)
0. Copy a solution directory and open the replica.
1. Point the cursor to every office constant. If you are afraid of it, you will be notified of the actual value and replace these constants with the value.
2. Declare the app and book as objcet and use the Createobject method to create an app object.
3. Scan for unused references on the reference page and delete them
OK, your more general version is born ~~~~ There are not so many prompts when you can return to the IDE to modify the code ~~~~~~ Hehahaha... We don't know how to do better. How can we do better?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.