user238271
user238271

Reputation: 643

How to save opened word document in vb.net

I am trying to make a word add-in that saves opened documents. I put a ribbon and button on it. Below is the [button click handler] code I use for saving a word document in a specific location:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click
        Dim ThisApplication As Word.Application
        ThisApplication.ActiveDocument.SaveAs("C:\email")
        ThisApplication.Quit()
        ThisApplication= Nothing
        End Sub

But when I click on this button, I create email.doc but that document does not contain any content of the opened document; it just creates a new doc file.

What I am doing wrong? The event-handler on this button needs to behave the same as the event-handler on the standard Word save button, so how I can do this?

Upvotes: 3

Views: 7103

Answers (1)

Steve McLain
Steve McLain

Reputation: 94

I can only imagine that maybe you need to create an object to represent the document itself first. Try the following:

Dim ThisApplication As Word.Application
Dim oDoc As Word.Document = ThisApplication.ActiveDocument
oDoc.SaveAs("C:\email")
oDoc.Close()        
ThisApplication.Quit()
oDoc = Nothing
ThisApplication = Nothing

Upvotes: 2

Related Questions