Click here to Skip to main content
15,311,014 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have been trying to write a small program that generates a Word document using OpenXML docmentformat.

In my mainform I have a method creating the document:
' Creates a WordprocessingDocument.
Public Sub CreatePackage(filePath As String, text As String)
    Using package As WordprocessingDocument = WordprocessingDocument.Create(_filename, WordprocessingDocumentType.Document)
        ' Add a new main document part.
        package.AddMainDocumentPart()

        ' Create the Document DOM.
        package.MainDocumentPart.Document = New Document(New Body(New Paragraph(New Run(New Text(text)))))

        ' Save changes to the main document part.
        package.MainDocumentPart.Document.Save()
    End Using
End Sub


When I run the program I get a "UnauthorizedAccessException" Access to the path 'C:\Temp' is denied.

I have tried searching the net but with 51.000 hits on this basically impossible to review them all. My settup and what I have checked so far:
* Windows 7 Pro
* Visual Studio 2010 (running as admin)
* I have admin rights on PC
* tried to add a app.manifest with
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />


Any help would appreciated.
Posted

1 solution

I guess C:\Temp is an existing folder, and hopefully you set the access rights correctly - hence C:\Temp is not an acceptable name for a file.
I see that your function uses CreatePackage(filePath As String while the you call WordprocessingDocument.Create(_filename. Did you mix up the filenames? Try a non-existing filename in a folder where you have write access.
   
v2
Comments
Member mosp 14-Jun-13 7:55am
   
Hi Bernard, _filename is a private variable I filled up in my code elsewhere with "C:\Temp".

When I try to do thesame with "C:\users\MyName\AppData\Roaming\Microsoft\MyProjectName\"
it is working. But to give a user as much freedom as possible I would like them to use all maps on a drive. Like any other application.

Kind regards.
Bernhard Hiller 17-Jun-13 2:17am
   
So C:\Temp is not a folder? And you believe that you have write access to the root folder of your C: drive on a modern version of Windows? Oh no, Windows 7 is not Windows 95! Put the files int the folders where they belong to. For temporary files, that is System.IO.Path.GetTempPath(); and to make things clear: that's a folder, not a file. You have to create a filename, e.g. System.IO.Path.GetTempFileName()

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900