Open password protected presentation - c#

How can I open the password-protected PowerPoint presentation using C# or VBA,in excel workbook.open the method has a password parameter. But in PowerPoint, it does not have.
In PowerPoint Presentation.Open method does not have a password parameter
In Excel, I can use
Application.Workbooks.Open(Filename,Password) But there is no equivalent in PowerPoint
I need to pass the password during PowerPoint document open

This will Work: Tested in VBA
Dim PVW As ProtectedViewWindow, Pres As Presentation
Set PVW = ProtectedViewWindows.Open("Full Path ", "Password")
Set Pres = PVW.Edit("modify")
Another Method: Untested
Sub SetPassword()
With Presentations.Open(FileName:="C:\My Documents\Earnings.ppt")
.Password = complexstrPWD 'global variable
.Save
.Close
End With
End Sub
Taken From: Link

Related

C# How to update Powerpoint chart without opening Excel [duplicate]

Slide.Shapes.AddChart() automatically opens Excel. Even if I quickly do Chart.ChartData.Workbook.Application.Visible = false, it still shows a little while. This makes automating chart creation error-prone as the user has to try not to touch the Excel applications that keeps popping up.
Opening a presentation with WithWindow = false will still open Excel when creating new charts.
This behavior is "by design" and Microsoft is not interested in changing. This is the way the UI functions.
What you could do would be to create the chart in Excel (using either the interop or OpenXML), then import (insert) that file into PowerPoint.
Check this link from MSDN
Here's a possible work around.
Sub ChartExample()
Dim s As Shape
Set s = Application.Presentations(1).Slides(1).Shapes.AddOLEObject(ClassName:="Excel.Chart")
End Sub
You would then manipulate the chart you added via the s.OLEFormat.Object. I only experimented slightly, but it does not open an external Excel application and I did not see any extreme flickering unless I activated the object. A trade off is that at least in Powerpoint 2010, you need to convert it to use all of the features. If this doesn't work you could always try web components.
Edit:
I don't understand why this method causes a problem, but to try to assist further here is a little more code that shows actually manipulating the object. This was written with objects instead of workbooks etc, so that no references need to be made. It only demands the user have Excel on their machine.
Option Explicit
Const xlcolumns = 2
Sub ChartExample()
Dim s As Shape
Dim wb As Object, chart As Object, data As Object
Set s = Application.Presentations(1).Slides(1).Shapes.AddOLEObject(ClassName:="Excel.Chart")
Set wb = s.OLEFormat.Object
Set chart = wb.Sheets(1)
Set data = wb.Sheets(2)
'Set the range for the chart data
chart.setsourcedata Source:=data.Range("A1:C7"), PlotBy:= _
xlcolumns
'Update data values for the chart
data.Range("B1").Value = "Column Label 1"
data.Range("C1").Value = "Column Label 2"
data.Range("A2:C7").clearcontents
data.Range("A2").Value = "Row Label"
data.Range("B2").Value = 7
data.Range("C2").Value = 11
End Sub
I would suggest another methdology to over come the same.
In the powerpoint VBA add refrences to "Microsoft Excel 12.0 Object Library"
Ensure the user that for this operation none of the excel must be open via yuser form popup before the operation.
In the VBA create an excel and set its parameters in the following code
Add the powerpoint chart, the user wouldnt be able to see the opening of the underlying excel sheet upon adding chart excet the excel tabs which can be controled via code.
Sample Code:
Option Explicit
Sub AddExcelChartSample()
Dim xlApp As Excel.Application, xlWkbk As Excel.Workbook
Dim pres As PowerPoint.Presentation, sld As PowerPoint.Slide, iCount As Integer, chtShape As PowerPoint.Shape
'Open up the excel instance and set parameters
Set xlApp = New Excel.Application
With xlApp
.WindowState = xlNormal
.Top = -1000
.Left = -1000
.Height = 0
.Width = 0
End With
Set sld = PowerPoint.ActiveWindow.View.Slide
For iCount = 1 To 10
Set chtShape = sld.Shapes.AddChart(xlLine)
Set xlWkbk = chtShape.Chart.ChartData.Workbook
With xlWkbk
.Sheets(1).Range("A2").Value = "Test 1"
.Sheets(1).Range("A3").Value = "Test 2"
.Sheets(1).Range("A4").Value = "Test 3"
End With
chtShape.Chart.Refresh
xlWkbk.Close False
Next iCount
xlApp.Quit
End Sub

Extracting Excel workbook name before it actually opens

I am trying to get the name of the workbook before it actually opens up.
((Excel.AppEvents_Event)this.Application).WorkbookOpen += new Excel.AppEvents_WorkbookOpenEventHandler(App_WorkBookOpen);
private void App_WorkBookOpen(Excel.Workbook Wb)
{
System.Windows.Forms.MessageBox.Show("Shakti " + " " + Wb.Name);
}
With the handler as shown above, Excel application shows the workbook name when it is opened completely.My intention is to do some formal check before it is actually opened up and data is shown to the user.
Is there any way or mechanism to extract the file name before the contents are loaded on to Excel and shown to the user? Any sort of help is highly appreciated.Thanks.
AFAIK you can't do that. But like I mentioned in my comment you could hide the workbook the moment it is visible. So the user will see the workbook open for a split second and then go invisible. In that split second you can read the name of the workbook and then hide the workbook.
Based on your calculations/conclusion you can then close/unhide the workbook as required.
You can hide the workbook using
Wb.Windows[1].Visible = false;
No you can't.
You anyway could create a Macro on a WorkBook Module with Open class tag as here:
Private Sub Workbook_Open()
Dim ws As Workbooks
For Each ws In ActiveWorkbook.Worksheets
MsgBox ws.Name
Next
ActiveWorkbook.Worksheets.Close
End Sub
Then call this sub via c# on opening the file, this sub runs before loading the workbook then it closes it. It has not that sense, because you'll never access the wb again...
Maybe with some tweaking here and there you could accomplish your task, but it depends to you.
Hope it helps...
Isn't Wb.name the same as the filename? In which case, since you must know the filename/location in order to open it, you can check it beforehand?

update password protected linked word document in C#

Let me first describe the problem. I have this application that opens a word document and updates all the fields and then saves the document in another folder. The word document is just used as a template for a report. It is filled with linked content from an excel worksheet. The whole application works, but the problem is that the excel document is password protected. when I update the document fields the application is stopped and word asks for a password.
When you input the password, the program works as advertised, you have to insert it more than once thou, dont really know why. But the program is suppose to work autonomously without user input. Is there a way to give word the password so that it doesnt have to be entered, either via code or in the word document properties.
Below is my current code that does this, its in C#
Microsoft.Office.Interop.Word.Application ap = new Microsoft.Office.Interop.Word.Application();
Document doc = ap.Documents.Open(template, ReadOnly: false, Visible: false);
doc.Activate();
red_debug.AppendText("opening " + template + "\n");
doc.Fields.UpdateSource();
doc.Fields.Update();
red_debug.AppendText("Saving as " + final + "\n");
doc.SaveAs(final, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument);
doc.SaveAs(path + "\\" + name, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF);
red_debug.AppendText("Closing word" + "\n");
doc.Close();
ap.Quit();
Get the Word document to open up the Excel file (with the password) for you everytime it has been opened. Same time, we can also successfully close the Excel file from Word.
Put this into the Word macro area.
Private Sub Document_Open()
Dim xlApp As Object
Dim xlWB As Object
Dim myRange
Application.DisplayAlerts = wdAlertsNone
Set xlApp = CreateObject("Excel.Application")
Set xlWB = xlApp.Workbooks.Open("C:\ExcelFile.xls", , , , "password", "password")
Set myRange = Selection.Range
Selection.WholeStory
Selection.Fields.Update
myRange.Select
xlApp.Quit
Set xlWB = Nothing
Set xlApp = Nothing
Application.DisplayAlerts = wdAlertsAll
End Sub
This will open up the excel document and update ALL fields on your word document and shouldnt ask you for password
I think you can try to call doc.Unprotect(ref Object password) by passing your password as argument.
Check out this article on CodeProject and this on MSDN.

How do I access the "Save Thumbnail" feature of Excel through Interop?

I'm working on an Excel add-in using C# and .NET 4.0. In Excel there's a feature in the Save-As dialog for saving a preview thumbnail along with the document. How can I access this feature in code? Also, how do I access the preview image (I think it's a bitmap) once it has been saved?
Currently my Excel add-in makes a copy of the document as follows:
Globals.ThisAddIn.Application.ActiveWorkbook.SaveCopyAs("tempwbcopy");
It then copies the document to a server and erases the temp file. Basically I'd like to also make the thumbail image, post it to the server, and erase the temp file.
I don't know how to access the Save Thumbnail feature programatically, but if you have a Excel file with a thumbnail and want to extract the image you could use the following code (using the OpenXml 2.0 API):
Private Sub ExtractThumbnailAsPng(ByVal pathToExcelFile As String, ByVal outputPath As String)
Dim thumbnailPart As DocumentFormat.OpenXml.Packaging.ThumbnailPart
Using excelFile As SpreadsheetDocument = SpreadsheetDocument.Open(pathToExcelFile, True)
thumbnailPart = excelFile.ThumbnailPart
If thumbnailPart IsNot Nothing Then
Using thumbnailStream As Stream = thumbnailPart.GetStream(FileMode.Open, FileAccess.ReadWrite)
Dim thumbBitmap As New Bitmap(thumbnailStream)
thumbBitmap.Save(outputPath, System.Drawing.Imaging.ImageFormat.Png)
End Using
End If
End Using
End Sub
Since this isn't Excel automation you could do this server side as well.

How do I open a file in C# and change its properties?

I need to open a Microsoft Word 2003 file and change its file properties. Such as changing the Subject in the Summary Tab.
Microsoft provides a very useful little assembly called DSOFile. With a reference to it in your project, you can modify Office document properties. It won't necessarily let you open the actual Office file's properties dialog, but you could certainly simulate it.
According to Microsoft:
The Dsofile.dll files lets you edit
Office document properties when you do
not have Office installed
More details and a download link can be found at http://support.microsoft.com/kb/224351
Here's a snippet some (very old) VB code I used ages ago. Sorry I haven't converted to C# and be aware that it's part of a class so there are references to instance variables. Still, it should be pretty easy to understand and covert to your own needs:
Private Sub ProcessOfficeDocument(ByVal fileName As String)
Dim docDSO As New DSOFile.OleDocumentPropertiesClass
Dim docTitle, docModified, docAuthor, docKeywords As String
Try
docDSO.Open(fileName, True)
Dim docSummary As DSOFile.SummaryProperties = docDSO.SummaryProperties
docTitle = docSummary.Title
docAuthor = docSummary.Author
docKeywords = docSummary.Keywords
docModified = CStr(docSummary.DateLastSaved)
If (Not String.IsNullOrEmpty(docTitle)) Then
_Title = docTitle
End If
If (Not String.IsNullOrEmpty(docAuthor)) Then
_Author = docAuthor
End If
If (Not String.IsNullOrEmpty(docModified)) Then
_DateModified = DateTime.Parse(docModified)
End If
Catch ex As Exception
'Do whatever you need to do here...'
Finally
If (Not docDSO Is Nothing) Then
docDSO.Close()
End If
End Try
End Sub
I can think of 2 ways to do this:
Use the Microsoft Office APIs. You
will have to reference them in your
project, and you will need the
Primary Interop Assemblies.
Convert the file to the Word 2003
XML format and change that value in
the XML document. Here is the MSDN
documentation on the document
properties:
http://msdn.microsoft.com/en-us/library/aa223625(office.11).aspx
I would go with the second option if you can, because that way you don't have to depend on Word being installed on the system.

Categories