Home > Net >  In vb.net in 2013 or 20008, how to implement open a word document, and write data to it?
In vb.net in 2013 or 20008, how to implement open a word document, and write data to it?

Time:09-23

Want to write a program in the vb.net, the specific content into an existing word file, provides some methods on the net, but did not resolve my problem,
For example: Dim wApp As Word. Application
The Set wApp=New Word. Application
WApp. Visible=True

There are
1 public Class WordOpLib
2
3
4 Private oWordApplic As Word. ApplicationClass
. 5 Private oDocument As Word Document
6 Private orange As Word. Range
7 Private oShape As Word. Shape
8 Private oSelection As Word. Selection


This kind of method, wrote will prompt wrong, I really don't know where is the problem?
Exclude o

CodePudding user response:

The set is a vb6

To add com references

Dim wApp As new Word. Application
WApp. Visible=True

CodePudding user response:

Use third party controls, also has a lot of is not completely, the various versions compatible with Microsoft is enough to you, you also control the control program with the customer please _ please,,,,,,,,, write the word general file format is not a good word to replace a few characters,,,

CodePudding user response:

Was studied, some time ago to give you a piece of code, I was directly to the content into the word inside the textbox control,
 Private Sub Button47_Click (ByVal sender As System. Object, ByVal e the As System. EventArgs) Handles Button47. Click 
'test results save to word file
If TextBox24. Text=Nothing Then 'judge whether has complete line of low pressure load table test
MsgBox (" first performing low-voltage load table test step!" , MessageBoxIcon. The Exclamation)
The Exit Sub
End the If
Dim WordApp As Word. The Application object 'statement Word
Dim WordFile As Word. The Document 'declaration Document object
Dim FileSave As New SaveFileDialog 'save file dialog
Dim SavePath As String 'file path
WordApp=New Word. Doc Application 'instantiated object
WordApp. Visible=False 'hidden doc file
FileSave. Filter="Word File (*. Docx) | *. Docx | Word File (*.doc) | *. Doc" 'save as docx File or doc File
FileSave. ShowDialog () 'displays save file dialog
SavePath=FileSave. FileName 'file path
If SavePath=Nothing Then 'If you do not save the file, the exit process
The Exit Sub
End the If
If Not File. The Exists (SavePath) Then 'File does Not exist, create a File
WordApp. Documents. The Add. SaveAs (SavePath)
The Else
'file exists, delete to the recycle bin, recreate the file
My.Com puter. FileSystem. DeleteFile (SavePath, FileIO. UIOption. OnlyErrorDialogs, FileIO. RecycleOption. SendToRecycleBin, FileIO. UICancelOption. DoNothing)
WordApp. Documents. The Add. SaveAs (SavePath)
End the If
WordFile=WordApp. Documents. The Open (SavePath, Nothing) 'opens the file
WordApp. Visible=False 'files hidden
WordApp. Selection. TypeText (" statistics of the low pressure load test results ") 'written title
WordApp. Selection. TypeParagraph () 'insert a blank line
WordApp. Selection. TypeText (TextBox24. Text)
'written test resultWordFile. The Save ()
WordApp. Quit () 'end WordApp object
System. The Runtime. InteropServices. Marshal. ReleaseComObject (WordApp)
WordApp release WordApp object
=Nothing 'MsgBox (" file has been saved to: "& amp; SavePath & amp; "!" , MessageBoxIcon. Information)
End Sub

CodePudding user response:

With a third party controls aspose is to use the most you can baidu.

CodePudding user response:

I usually write a word file, need to change with the placeholder identification, saved as XML format, extension then to doc, thus opening or open word, look not to come out,
Code is to open the XML text files, can replace placeholders, XML format might be disorderly, in both English and Chinese will be separately defined style, could be separated the placeholder, need a little text processing processing,
  • Related