2012-05-18 61 views
3

在VB.net中有沒有將Word文檔另存爲不同格式(即Me.Application.ActiveDocument.SaveAs)的方法,而無需切換到它?例如,如果當前文檔是未保存的,我想將該文檔的副本保存爲HTML,但仍保持未保存的文檔處於活動狀態。以不同格式保存Word文檔,但沒有有效的活動文檔

+0

是否要保存不是ActiveDocument的文檔?或者你想在活動文檔上使用SaveAs功能? –

+0

我想在後臺保存ActiveDocument的副本,但不能切換到保存的副本。 saveAs的默認行爲打開保存的文檔。 – ltfishie

回答

2

將當前的doc變量複製到另一個變量並保存。

Try 
     Dim oWord As Word.Application 
     Dim oDoc As Word.Document 



     'Start Word and open the document template. 
     oWord = CreateObject("Word.Application") 
     oWord.Visible = True 
     oDoc = oWord.Documents.Add 
     oDoc.PageSetup.TopMargin = oWord.CentimetersToPoints(5.08) 
     oDoc.PageSetup.LeftMargin = oWord.CentimetersToPoints(4.57) 
      oDoc.PageSetup.RightMargin = oWord.CentimetersToPoints(1.27) 
      oDoc.PageSetup.BottomMargin = oWord.CentimetersToPoints(3.81) 
      oDoc.PageSetup.PageHeight = oWord.CentimetersToPoints(29.7) 
      oDoc.PageSetup.PageWidth = oWord.CentimetersToPoints(21) 

      'TIll Above your entire odoc is formatted 
      'From below I will save it to my own code 

      Dim newdoc As Word.Document 
      newdoc = oDoc 
      newdoc.SaveAs2("d:\file.pdf", Word.WdSaveFormat.wdFormatPDF) 

      'All done. Close this form. 
      'BSPGlobals.DataBase.Contact.ExitApp() 
      MessageBox.Show("Print to Doc Done.") 
     Catch ex As Exception 
      MessageBox.Show("Error at Printing the bill." & vbCrLf & ex.Message) 
     End Try 
+0

你能告訴你你是什麼意思? – ltfishie

+0

添加了答案 – surpavan

+0

謝謝,我會試試看。 – ltfishie