VB operation WORD code:
Dim wrdObject As Word.Application
Dim wrdDoc As Word.Document
Set wrdObject = CreateObject("Word.Application")
Set wrdDoc = wrdObject.Documents.Open("d:/hao/one.doc")
wrdObject.Visible = True
wrdObject.Selection.TypeText "This is some text." 'Write text into word
wrdDoc.PrintOut
'wrdDoc.ClosePrintPreview 'Close print preview
wrdDoc.Close 'Close word document
wrdObject.Quit 'word application exit
Set wrdDoc = Nothing 'Release memory
Set wrdObject = Nothing 'Release memory
In your code, it is estimated that you did not close the word document before exiting directly. The above code will close it after printing.
I know, but I can’t read it directly because Microsoft has not announced the encryption method, so I have to call WORD to read it and then get it
First add Microsoft Word 11.0 Object Library to the project reference
Dim clipboard text
Dim Word word processing system interface As Word.Application 'Create a word.application object
Dim Word Document As Word.Document
Dim Word document text As Word.Selection 'Add a document in the word word processing system
Set Word word processing system interface = CreateObject("Word.Application") 'Select object
Word word processing system interface.Visible = False 'Do not display word.application, which is the word word processing system interface
Set Word document = Word word processing system interface.Documents.Open (file path)
Set Word document text = Word word processing system interface.Selection
Word document text.WholeStory
Word document text.Copy
Clipboard text = Clipboard.GetText(vbCFText) 'Put it into text1
Text1.Text = Clipboard text
'Clipboard.SetData Temp
Word document.Close 'Close document
Word word processing system interface.Application.Quit
Set Word word processing system interface = Nothing 'Clear variables
Set Word document text = Nothing
'Complete operation
I get the text through the clipboard because it preserves the format (my original creation)
It is also possible to obtain it directly, but all the formats will be lost, so my method will be better
Dim WordApp As Word.Application'define word application variables
Dim newDoc As Word.Document'define word document variables
Set WordApp = CreateObject("Word.Application")'Create a new word application
WordApp.Visible = True
With WordApp
Set newDoc = .Documents.Add(App.Path & "\Report Sheet\Food and Water Quality Report.Doc")'Create a new word document in the created word application
With .Selection
.GoTo wdGoToBookmark, , , "bh" bh is the tag name of the corresponding position in the word document
.TypeText PrnBID'Write content
.GoTo wdGoToBookmark, , , "GoodsName" 'GoodsName is the tag name of the corresponding position in the word document
.TypeText PrnGName'Write content
Set MyRS = Nothing
End With
' newDoc.SaveAs FileName:=FilePath
End With
' If theError = 429 Then WordApp.Quit
Set WordApp = Nothing
Set the label in the pre-made word document where you want to write the content, then find the label in the program, and then write the data
The above is the detailed content of Printing operation of word document in vb. For more information, please follow other related articles on the PHP Chinese website!