如何将数据粘贴到Word文档中
问题描述:
我正在使用发送键来选择所有数据并从另一个应用程序复制它。我的目标是将这些数据粘贴到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
答
根据剪贴板数据的来源和格式,你可以影响到剪贴板中的内容是由具有以下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
谢谢。有用! – user3753620