Remove all animations pptx from Powerpoint Presentation using OpenXML SDK - c#

I'm currently working on editing my existing PowerPoint file (.pptx) using OpenXML SDK. Can I know how to remove all animations from my PowerPoint presentation?
The reason for this is I want a clean non-animated ppt for my use case. I've checked everywhere and there is minimal to no documentation regarding animation using OpenXML.
Thanks.

Yeah, here's how I do it with the Timing class, it's pretty straight-forward.
In VB.Net:
Imports Presentation = DocumentFormat.OpenXml.Presentation
Imports DocumentFormat.OpenXml.Packaging
Module Module1
Sub Main()
Dim fileName As String = "C:\folder\New PPTX Presentation.pptx"
Using deck As PresentationDocument = PresentationDocument.Open(fileName, True)
For Each slidePart As SlidePart In deck.PresentationPart.SlideParts
Dim animationTimings As IEnumerable(Of Presentation.Timing) = slidePart.Slide.Descendants(Of Presentation.Timing)()
For Each animationTiming As Presentation.Timing In animationTimings
animationTiming.Remove()
Next
Next
End Using
End Sub
End Module
In C#:
using Presentation = DocumentFormat.OpenXml.Presentation;
using DocumentFormat.OpenXml.Packaging;
static class Module1
{
public static void Main()
{
string fileName = #"C:\folder\New PPTX Presentation.pptx";
using (PresentationDocument deck = PresentationDocument.Open(fileName, true))
{
foreach (SlidePart slidePart in deck.PresentationPart.SlideParts)
{
IEnumerable<Presentation.Timing> animationTimings = slidePart.Slide.Descendants<Presentation.Timing>();
foreach (Presentation.Timing animationTiming in animationTimings)
animationTiming.Remove();
}
}
}
}

Related

Can I use the WordInterop functionality with Office365?

I have to refactorate very old Code (older than 2005). The Software should open Word Documents and fill variables in the header with data. The Doc will open, but the variables are empty.
I use following codesnipped to show the amount of var´s in a Word Document to do some tests.
It always say´s there are 0... Can I use the WordInterop with Office 365 ?
using Microsoft.Office.Interop.Word;
namespace CheckForVariablesInWord
{
class Program
{
static void Main(string[] args)
{
Microsoft.Office.Interop.Word.Application ap = new Microsoft.Office.Interop.Word.Application();
Document document = ap.Documents.Open(#"C:\temp\TestDocument.docx");
ap.Visible = true;
System.Console.WriteLine(document.Variables.Count);
System.Console.ReadLine();
}
}
}
this is the TestWordDocument:
Here is the Variable named „myTest”
{ DOCVARIABLE myTest * MERGEFORMAT}
thx if anyone can help me out before i get mindsick ;.)

Autocad & System.Addin, FileNotFoundException for Autocads basic dlls

i just started to develop applications for AutoCAD 2016. I want to load my dLLs into a separate AppDomain, so that i don't have to restart ACAD all the time.
After a lot of research and trying i ended up with a pipeline solution
using System.Addin and System.Addin.Contract.
I use only interfaces and standardclasses for the Views Contract and Adapters like in this example here.
This is my addin containing one methode to write Hello into Acad's Editor and a second methode for drawing a line.
using System.AddIn;
using CADAddinView;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
namespace CADAddIn
{
[AddIn("cadAddIn", Version = "1.0.0.0")]
public class CADAddIn : ICADAddinView
{
public void drawLine()
{
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
using (DocumentLock acLckDoc = acDoc.LockDocument())
{
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
DBObject blkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead);
BlockTable acBlkTbl = blkTbl as BlockTable;
BlockTableRecord acBlkTblRec = (BlockTableRecord)acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
Polyline acPoly = new Polyline();
acPoly.SetDatabaseDefaults();
acPoly.AddVertexAt(0, new Point2d(0, 0), 0, 0, 0);
acPoly.AddVertexAt(0, new Point2d(100, 100), 0, 0, 0);
acBlkTblRec.AppendEntity(acPoly);
acTrans.AddNewlyCreatedDBObject(acPoly, true);
acTrans.Commit();
}
}
}
public void sayHello()
{
Editor ed = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("Hello");
}
}
}
this is my HostApplication:
using System.AddIn.Hosting;
using System.Windows.Forms;
using CADHostView;
using System;
using System.Collections.ObjectModel;
using Autodesk.AutoCAD.Runtime;
namespace CADHost
{
public class CADHost
{
[CommandMethod("sayHello")]
public static void sayHello()
{
string addInPath = Environment.CurrentDirectory + "\\Pipeline";
string[] warnings = AddInStore.Update(addInPath);
foreach (string warning in warnings)
{
MessageBox.Show(warning);
}
Collection<AddInToken> tokens = AddInStore.FindAddIns(typeof(ICADHostView), addInPath);
if (tokens.Count == 0)
{
MessageBox.Show("No AddIn found.");
}
else
{
AddInToken cadToken = tokens[0];
ICADHostView cadApp = cadToken.Activate<ICADHostView>(AddInSecurityLevel.Host);
cadApp.sayHello();
}
}
[CommandMethod("drawLine")]
public static void drawLine()
{
string addInPath = Environment.CurrentDirectory + "\\Pipeline";
string[] warnings = AddInStore.Update(addInPath);
foreach (string warning in warnings)
{
MessageBox.Show(warning);
}
Collection<AddInToken> tokens = AddInStore.FindAddIns(typeof(ICADHostView), addInPath);
if (tokens.Count == 0)
{
MessageBox.Show("No AddIn found.");
}
else
{
AddInToken cadToken = tokens[0];
ICADHostView cadApp = cadToken.Activate<ICADHostView>(AddInSecurityLevel.Host);
cadApp.drawLine();
}
}
}
}
Both of the two applications reference to three standard-Dlls from Acad:
accoremgd.dll, acdbmgd.dll, acmgd.dll.
In both projects these dlls have the option local copy false.
If i start then i get an Exception, where the programm cannot find the file "accoremgd.dll" and Acad crashes.
So i tried to set the Option local copy true only for the Addin.
Now it works for the "sayHello"-Methode.
but i get an invalide cast exception when acBlkTbl is initialised.
Would be great if someone has the last steps for me to make this work.
Also great would be a working example must not be made with the Addinsystem
i only want to make this work for not restarting acad all the time^^
Thank you for your help
matthias
I don't believe a separate AppDomain will work, when you call AutoCAD object types it will go to the main AppDomain and get messed up...
As just want to edit your code and don't restart, you'll be better with Edit & Continue feature (available since VC2013 on AutoCAD 2015, I believe).
This is not supported. AutoCAD is a very old and complex program and most of the AutoCAD API objects cannot be used in remote fashion.
Please read:
http://through-the-interface.typepad.com/through_the_interface/2008/09/tired-of-not-be.html
http://forums.autodesk.com/t5/net/netload-is-there-a-net-unload-command/td-p/2404002
https://www.theswamp.org/index.php?topic=38675.0
In the #3, you can see that the AutoCAD development team confirmed that there are some global variables which will prevent working this way.
I gave up my tries to solve this problem. My current "best" solution is to load dlls at the start of AutoCAD. At least i don't have to netload every dll.
If someone has a better solution feel free to tell me^^ Thanks to all that answered. matthias

Programmatically Open Word Document Located in the Computer in C#

I'm using WinForms. I have a form that has a button.
Goal: On button click: Open up a word document. Where the file path is hard coded into the program. I don't want the users to have to locate the word document.
Problem: I receive this error message. When I wrote my code, I get a red error line under 'Application'.
private void button1_Click(object sender, EventArgs e)
{
this.Application.Documents.Open(#"C:\Test\NewDocument.docx", ReadOnly:true)
}
Instead of adding interop in your reference, you may also consider to use this:
System.Diagnostics.Process.Start(#"C:\Test\NewDocument.docx");
first add the dll of Microsoft.Office.Interop.Word to your references then add this:
using Microsoft.Office.Interop.Word;
and use the following code:
Application ap = new Application();
Document document = ap.Documents.Open(#"C:\Test\NewDocument.docx");
This Application is not this.Application it's Microsoft.Office.Interop.Word.Application.
So you can use this code:
using System;
using Microsoft.Office.Interop.Word;
class Program
{
static void Main()
{
// Open a doc file.
Application application = new Application();
Document document = application.Documents.Open("C:\\word.doc");
//Do whatever you want
// Close word.
application.Quit();
}
}
There is a good answer above which is:
System.Diagnostics.Process.Start(#"C:\Test\NewDocument.docx");
This should be modified for .Net Core 2 and above to be:
var p = new Process();
p.StartInfo = new ProcessStartInfo(filename)
{
UseShellExecute = true
};
p.Start();

Using a .Net DLL in Microsoft Access VBA

Ok so I have an assembly, written in C#, using Visual Studio 2010.
This Assembly contains one class, which contains one method which returns the word Result, the code is below:
using System.Runtime.InteropServices;
namespace TestDLL
{
public class Class1
{
[ComVisible(true)]
public string TestMethod()
{
return "Result";
}
}
}
The output section in the Build tab on the properties window looks like so:
When I click on Build, I get a DLL file and a TLB file. I can add this TLB file to Microsoft Access simply by browsing to it.
Now, in Access I have a button and a label. I want to make the Caption property of my label equal to the result of testMethod. I'm thinking I need to do something similar to below but I'm not sure, any help would be much appreciated:
Private Sub btnMain_Click()
Dim tm As TestDLL
Dim foo As String
foo = tm.testMethod
lblBarr.Caption = foo
End Sub
Thankyou
Maybe next will work:
Private Sub btnMain_Click()
Dim tm As TestDLL.Class1
Dim foo As String
Set tm = New TestDLL.Class1
foo = tm.testMethod
lblBarr.Caption = foo
End Sub

How do I restore a file from the recycle bin using C#?

Moving files to the recycle bin and emptying the recycle bin are well documented, but how can a file be programmatically restored from the recycle bin?
There seems not to be a solution in pure C#. You most likely have to resort to P/Invoke.
This article presents a solution in C++ using the SHFileOperation API.
The only other reference to this beyond the previously mentioned link to codeproject that I can see mentions this:
Call SHGetFolderLocation passing CSIDL_BITBUCKET.
Then you can manipulate that folder as usual.
You'll have to create an interop for the SHGetFolderLocation function.
CSIDL_BITBUCKET being the CSIDL ("constant special item ID list") value for the virtual Recycle Bin folder. The quote is taken from here, and will involve interop with the Windows shell. MSDN also mentions that this function has been deprecated in favour of another in Vista.
Hope below code will work to restore the files. Please make sure, STA Calls only supported for shell calls
using System;
using System.Collections;
using System.Windows.Forms;
using System.IO;
using Shell32; //Reference Microsoft Shell Controls And Automation on the COM tab.
using System.Runtime.InteropServices;
using Microsoft.VisualBasic.FileIO;
using System.Threading;
private static void Restore(object param)
{
object[] args = (object[])param;
string filename = (string)args[0];
string filepath = (string)args[1];
Shl = new Shell();
Folder Recycler = Shl.NameSpace(10);
var c = Recycler.Items().Count;
var _recycler = Recycler.Items();
for (int i = 0; i < _recycler.Count; i++)
{
FolderItem FI = _recycler.Item(i);
string FileName = Recycler.GetDetailsOf(FI, 0);
if (Path.GetExtension(FileName) == "") FileName += Path.GetExtension(FI.Path);
//Necessary for systems with hidden file extensions.
string FilePath = Recycler.GetDetailsOf(FI, 1);
if (filepath == Path.Combine(FilePath, FileName))
{
DoVerb(FI, "ESTORE");
break;
}
}
}
private static bool DoVerb(FolderItem Item, string Verb)
{
foreach (FolderItemVerb FIVerb in Item.Verbs())
{
if (FIVerb.Name.ToUpper().Contains(Verb.ToUpper()))
{
FIVerb.DoIt();
return true;
}
}
return false;
}

Categories

Resources