Saving Document in Aspose Words

Could you help me please. I create a document which I invoke in word - How do I also automatically (with no action from the user), save it;
My code is

Public Sub ExecuteWithRegionsDataTable()
Dim doc As Document = New Document(Server.MapPath("/docs/documents/polcomp1.doc"))
Dim Id As Integer = 10444
Dim PolicyNo As String = CStr(Request.Form("ID1"))
Dim fname As String = ("www.selkirk.co.za/DocMan/Images/" & PolicyNo & (Year(Now()) & (Month(Now())) & (Day(Now())) & (Hour(Now())) & (Minute(Now())) & "shed"))
Dim orderTable As DataTable = GetTestOrder(Id)
orderTable.TableName = "Schedule"
doc.MailMerge.ExecuteWithRegions(orderTable)
Dim BankDetails As DataView = New DataView(GetbankDetails(Id))
doc.MailMerge.ExecuteWithRegions(BankDetails)
Dim orderDetailsView As DataView = New DataView(GetTestOrderDetails(Id))
doc.MailMerge.ExecuteWithRegions(orderDetailsView)
doc.Save(fname, SaveFormat.Doc, SaveType.OpenInBrowser, Response) ???????
End Sub
Private Function GetTestOrder(ByVal Id As Integer) As DataTable
Dim table As DataTable = ExecuteDataTable("SELECT * FROM Results WHERE [ID] = @ID", Id)
table.TableName = "Schedule"
Return table
End Function
Private Function GetTestOrderDetails(ByVal Id As Integer) As DataTable
Dim table As DataTable = ExecuteDataTable("SELECT * FROM Resultsvehicle WHERE [ID] = @ID", Id)
table.TableName = "vehicleDetailsView"
Return table
End Function
Private Function GetbankDetails(ByVal Id As Integer) As DataTable
Dim table As DataTable = ExecuteDataTable("SELECT * FROM bankfile WHERE [Policy_No] = @ID", Id)
table.TableName = "Bankfile"
Return table
End Function
Private Function ExecuteDataTable(ByVal commandText As String, ByVal id As Integer) As DataTable
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("~/fpdb/passliabcapture.mdb")
Dim conn As OleDbConnection = New OleDbConnection(connString)
conn.Open()
Dim cmd As OleDbCommand = New OleDbCommand(commandText, conn)
cmd.Parameters.Add(New OleDbParameter("@ID", OleDbType.VarChar, 200))
cmd.Parameters("@ID").Value = CStr(Request.Form("ID1"))
Dim da As OleDbDataAdapter = New OleDbDataAdapter(cmd)
Dim table As DataTable = New DataTable()
da.Fill(table)
conn.Close()
Return table
End Function

Hi
Thanks for your request. This is impossible to save a file on client side without any actions from the user. This is not restriction of Aspose.Words this is security restriction.
Best regards.

Thanks for the response. Sorry I want to save it on server side without user response.

Hi
Thanks for your request. just use Save method. For example:

doc.Save("C:\Temp\out.doc")

But note that you should have write permissions to the folder where you’d like to save the file. So I think you should create subfolder in the root folder of your web site and use the following code to save the file.

doc.Save(Server.MapPath("documents\out.doc"))

Hope this helps.
Best regards.

Hi Alexey,
I have a similar situation, but I wish to save my documents automatically to another server (not the website server). Is it possible to do this using the Save method? I tried this:

docMaster.Save(strSavePath & strDocumentName & ".doc", SaveFormat.Doc)

where strSavePath contains the full path name of the server where I wish to save the document, but it gave me the message:
Filename cannot contain path.
Is there any way to do this?
Thanks very much!
Chris.

Hello Chris,

Thanks for your request. Yes you can try using the following code:

doc.Save(@"\\192.168.0.4\\Share\\out.doc");

Best regards,

Hi Andrey,
Thanks for your response. I tried copying and pasting into my VB.NET code hoping to modify it to my server and document, but it flagged the “@” as a syntax error. When I hover my mouse over it, the tag says “Expression expected”.
Am I missing something?
Thanks!
C.

Hello

Thanks for your inquiry. You should just escape this “/”. Please try using the following code:

Dim doc = New Document("C:\\Temp\\in.doc")
doc.Save("/\\192.168.0.1/\\Temp/\\out.doc")

Best regards,

Hi again Andrey and thanks for your response. Your suggested code works well, but is there any way to also specify that the saved document opens in a new browser window? I note that if I save the document locally, I can tell it to open in Word or open in a new browser window like this:

doc.Save( "abc.doc", SaveFormat.Doc, SaveType.OpenInBrowser, Response)

But, if I try to save it to another server using the escaped string as you’ve suggested AND I also specify the SaveFormat and SaveType, like this:

doc.Save("/\\192.168.0.1/\\Temp/\\out.doc",SaveFormat.Doc, SaveType.OpenInBrowser, Response)

then I get an error message saying “Filename should not contain path”.
Is there any way to do this?
Thanks!
C.

Hello

Thanks for your request. When you save your document to the client browser the code works like the following:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Hello world!!!");
MemoryStream stream = new MemoryStream();
doc.Save(stream, SaveFormat.Doc);
Response.Clear();
// Specify the document type.
Response.ContentType = "application/plain";
// Other options:
// Response.ContentType = "text/plain"
// Response.ContentType = "text/html"
// Specify how the document is sent to the browser.
// Response.AddHeader("content-disposition","inline; filename=MyDocument.pdf");
// Another option could be:
Response.AddHeader("content-disposition", "attachment; filename=out.doc");
// Get data bytes from the stream and send it to the response.
byte[] bytes = stream.GetBuffer();
Response.BinaryWrite(bytes);
Response.End();

So in your case if you need to open document in client’s browser you should first read the document into an array of bytes. And use the similar code to send the document to client.
Best regards,

That’s excellent, Andrey!! Thanks so much.