2015-12-02 152 views
2

我正在使用發送鍵來選擇所有數據並從另一個應用程序複製它。我的目標是將這些數據粘貼到word中並保存爲PDF格式。我似乎遇到的問題是,使用Microsoft interop,需要您編程格式化數據。如果我從其他應用程序複製數據,並將其手動粘貼到真實的文檔文檔中,則格式會保留自身。如何將數據粘貼到Word文檔中

有沒有一種方法可以輕鬆地將我的剪貼板數據與這段代碼一起使用?

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 
      oPara1 = oDoc.Content.Paragraphs.Add 
      oPara1.Range.Text = Clipboard.SetText 

      '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("K:\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 

回答

1

根據剪貼板數據的來源和格式,你可以影響到剪貼板中的內容是由具有以下Application選項擺弄周圍粘貼到Word的方式(不要忘記你的時候恢復原來的設置完成):

' when pasting between different Office documents  
oWord.Options.PasteFormatBetweenDocuments = Word.WdPasteOptions.wdKeepSourceFormatting 

' when contents is copied from a document that uses styles 
oWord.Options.PasteFormatBetweenStyledDocuments = Word.WdPasteOptions.wdKeepSourceFormatting 

' when pasting from an external source such as a web page 
oWord.Options.PasteFormatFromExternalSource = Word.WdPasteOptions.wdKeepSourceFormatting 
+0

謝謝。有用! – user3753620

相關問題