Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Mon Mar 26, 2018 5:02 pm

I wish to open word from vb.net without specifying a file. How can I do this?

jstiegler
 
Posts: 18
Joined: Fri Mar 23, 2018 5:35 pm

Tue Mar 27, 2018 3:03 am

Hello,

Thanks for you post. You could use LoadFromStream to open a Word file which doesn't need to specify the file path. If I misunderstand your requirement, please tell us and provide more details.
Code: Select all
            Document document = new Document();
            document.LoadFromStream(myStream,FileFormat.Docx);


Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Tue Mar 27, 2018 2:44 pm

Thank you...this is exactly what I was looking for.

Regarding mySteam...anything I need to do here other than:

Dim myStream As New IO.MemoryStream

Jeff

jstiegler
 
Posts: 18
Joined: Fri Mar 23, 2018 5:35 pm

Tue Mar 27, 2018 3:01 pm

Below is the code I used.

Dim document As New Document()

Dim objMemoryStream As New IO.MemoryStream

document.LoadFromStream(objMemoryStream, FileFormat.Docx)

Produces exception unhandled.

Exception.png


What am I doing wrong?

jstiegler
 
Posts: 18
Joined: Fri Mar 23, 2018 5:35 pm

Wed Mar 28, 2018 2:23 am

Hello,

Thanks for your reply.
I noticed that you just created a MemoryStream without data and Spire.Doc can't load an empty stream. It's unrealistic. If there is a file stream which contains the data of a Word file, you could use LoadFromStream to open it.
Code: Select all
        Dim doc As New Document()
        Dim ms As New IO.MemoryStream(File.ReadAllBytes("C:\test.docx"))
        doc.LoadFromStream(ms, FileFormat.Docx)


Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Wed Mar 28, 2018 3:13 pm

Thanks for your speedy response.

My original question was how to open Word with no document loaded. Your example requires a doc to be loaded from a file which I do not want to do.

This is why my code contained an empty memorystream thinking that this would allow me to open Word with no document.

Is there a way to open Word with no document loaded?

Thanks.

jstiegler
 
Posts: 18
Joined: Fri Mar 23, 2018 5:35 pm

Thu Mar 29, 2018 1:45 am

Hello,

Thanks for your reply.
I am sorry there isn't an approach to open a Word file without itself. Maybe you want to create a Word file, right? In Spire.Doc, you could create a Word file and then edit it. Please see below.
Code: Select all
            //Create New Word
            Document doc = new Document();
            //Add Section
            Section section = doc.AddSection();
            //Add Paragraph
            Paragraph Para = section.AddParagraph();
            //Append Text
            Para.AppendText("Hello World!");
            doc.SaveToFile("output.docx", FileFormat.Docx2010);


Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Thu Mar 29, 2018 4:06 am

Thank for your response.

I will try making a blank document in a file, then use you code to load this document into Word.

jstiegler
 
Posts: 18
Joined: Fri Mar 23, 2018 5:35 pm

Thu Mar 29, 2018 7:03 am

Hello,

Thanks for you prompt reply. Welcome to write to us if you encounter any other question.

Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Return to Spire.Doc