1

I am having trouble with the Open XML SDK opening and saving word documents.

I am using the following code (VB.Net):

        Try
        'Set Path
        Dim openPath As String = "../Documents/" & worddoc
        Dim savePath As String = "\\web-dev-1\HR_Documents\" & worddoc
        Using doc As WordprocessingDocument = WordprocessingDocument.Open("/Documents/" & worddoc, True)
            'Employee Name Insert
            'Find first table in document
            Dim tbl1 As Table = doc.MainDocumentPart.Document.Body.Elements(Of Table).First()
            'First Row in tbl
            Dim row As TableRow = tbl1.Elements(Of TableRow)().ElementAt(0)
            'Find first cell in row
            Dim cell As TableCell = row.Elements(Of TableCell)().ElementAt(0)
            'Insert selected Employee Name
            Dim p As Paragraph = cell.Elements(Of Paragraph)().First()
            Dim r As Run = p.Elements(Of Run)().First()
            Dim txt As Text = r.Elements(Of Text)().First()
            txt.Text = ddlEmployeeList.SelectedItem.Text
            'Save File
            'Supervisor Name Insert
            'Find second table in document
            Dim tbl2 As Table = doc.MainDocumentPart.Document.Body.Elements(Of Table).First()
            'First Row in tbl
            Dim row2 As TableRow = tbl2.Elements(Of TableRow)().ElementAt(0)
            'Find first cell in row
            Dim cell2 As TableCell = row2.Elements(Of TableCell)().ElementAt(0)
            'Insert selected Employee Name
            Dim p2 As Paragraph = cell2.Elements(Of Paragraph)().First()
            Dim r2 As Run = p2.Elements(Of Run)().First()
            Dim txt2 As Text = r2.Elements(Of Text)().First()
            txt2.Text = ddlSupervisorList.SelectedItem.Text
        End Using
        Return 1
    Catch ex As Exception
        Return Nothing
    End Try

The trouble starts on the first using statement. It throws the following error:

Could not find a part of the path 'C:\Documents\Hourly_Employee_Performance_Review .docx

I have placed the word documents in a folder of the ASP.NET site called Documents. I also have created a public share on the dev server to see if maybe that would help.

The problem is that it doesn't use the supplied path variable. I have gone through the documentation for OPEN XMl SDK but all it talks about is the Using Statement and its need and use for it.

Can anyone tell me, show me, or point to a site that has examples of how to set both the open path and save path?

mbcharney
  • 355
  • 1
  • 4
  • 15

1 Answers1

0

You need a path to the file which is based on the filesytem, not a URL. You can do that with

Dim openPath As String = Path.Combine(Server.MapPath("~/Documents"), worddoc)

And then to open the file:

Using doc As WordprocessingDocument = WordprocessingDocument.Open(openPath, True)

It appears that you will need to do the same for the location to save to, but you didn't say if "\\web-dev-1" is a different server; if it were that would need other considerations.

(Not tested, some typos may exist. You will need an Imports System.IO.)

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • no the ave path is on the same server. I tried to the following: Dim filePath As String = Path.Combine(Server.MapPath("\\web-dev-1\HR_Documents\"), worddoc) But it still throws me an error saying it could not part of the path but it shows me the local path to the project instead of the filePath from above. – mbcharney Apr 24 '18 at 15:42
  • "\\web-dev-1\HR_Documents\" is the actual file location for the documents. – mbcharney Apr 24 '18 at 15:54
  • I don't think that is going to work. It is still looking at this directory: H:\Projects\HR\Code\HR_Docs\HR_Docs\web-dev-1\HR_Documents\Hourly_Employee_Performance_Review .docx ----------------- H:\Projects\HR\Code\HR_Docs\HR_Docs is the location of the VS Project... – mbcharney Apr 24 '18 at 17:04
  • @mbcharney Ummm... am I correct in thinking that 1) this code is running in a web site (ASP.NET), 2) it does now *open* the document, and 3) you want to save the document on the same machine but in a directory which is not part of the web site? – Andrew Morton Apr 24 '18 at 17:18
  • 1) Correct 2)Correct 3)NO - i want to prompt the user to save it where they want to. – mbcharney Apr 24 '18 at 17:54
  • Aha! Please take a look at my answer to [Linking to PDF stored on network via Webforms ASP.net?](https://stackoverflow.com/a/25653028/1115360) (but ignoring the part about their file being on a remote server) and see if the general idea helps with the sending the file to the user part. – Andrew Morton Apr 24 '18 at 18:04