如何将数据粘贴到word doc

时间:2015-12-02 14:53:45

标签: vb.net ms-word

我使用send-keys选择所有数据并从另一个应用程序复制它。我的目标是将这些数据粘贴到word中并另存为PDF。我似乎有的问题是,使用Microsoft互操作,需要您以编程方式格式化数据。如果我从其他应用程序复制数据,并将其手动粘贴到真正的单词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
            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 个答案:

答案 0 :(得分: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