|
-
Nov 8th, 2004, 10:43 AM
#1
Thread Starter
Lively Member
help in WORD
Hi,
I have created a new word document using
Documents.Add()
how can i write into this document from a string?
thanks in advance.
-
Nov 8th, 2004, 02:23 PM
#2
Here is a small example for you.
VB Code:
Option Explicit
'Add reference to MS Word xx.0 Object Library
Private Sub Command1_Click()
Dim oApp As Word.Application
Dim oDoc As Word.Document
Dim sString As String
sString = "This is entered in from VB!"
Set oApp = New Word.Application
oApp.Visible = True
Set oDoc = oApp.Documents.Add
oDoc.Paragraphs(1).Range.Select
oApp.Selection.TypeText sString
End Sub
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 9th, 2004, 09:52 AM
#3
Thread Starter
Lively Member
Hey thanks for that, it worked. but i have a peculiar problem, hope u can solve it
I am using Word 10.0 object, to do some functionality of converting txt files to doc files and some doc files to txt files. All my code is working perfectly, if i run it once, but if i run it second time i am getting an error saying
"Run time error '462' :
The remote server machine does not exist or unavailable"
if i close the application, and if i complie and run it again, it works perfectly for the same input. please help me.
-
Nov 9th, 2004, 10:20 AM
#4
Post your code.
Its probably that you haven't removed the references to the doc and its still open and cannot be reopened (for some reason...)
Vince
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
-
Nov 9th, 2004, 10:29 AM
#5
Thread Starter
Lively Member
this is my code
Code:
Dim Path As String
Dim oFs As New FileSystemObject
Dim sAns() As String
Dim oFolder As Folder
Dim oFile As File
Dim tFile As String
Dim lElement As Long
Dim oLine As String
Dim linesPat As String, removePat As String, stopPat As String, diagPat As String
Dim lineBoolean As Boolean, removeBoolean As Boolean, stopBoolean As Boolean, diagBoolean As Boolean
Dim fname As String
Dim wordapp As New Word.Application
Dim inputFile As String
Dim outputFile As String
Dim deleteWords() As String
Private Sub Form_Unload(Cancel As Integer)
If IsObjectValid(wordapp) Then
wordapp.Quit
Set wordapp = Nothing
End If
End Sub
Private Sub start_Click()
deleteWords = Split(Text1.Text, ",")
If OptionOneFile.Value = True Then
oneFile (Text2.Text)
ElseIf OptionFolder.Value Then
allFolder
End If
If IsObjectValid(wordapp) Then
wordapp.Quit
Set wordapp = Nothing
End If
MsgBox ("Process Complete")
End Sub
Private Sub allFolder()
Dim searchPattern As String
Dim tFile As String, tLine As String
Dim i As Integer
Dim fileName As String
Dim docFile As Boolean
Path = Dir1.Path
ReDim sAns(0) As String
If oFs.FolderExists(Path) Then
Set oFolder = oFs.GetFolder(Path)
For Each oFile In oFolder.Files
docFile = False
frmSplash.Label2 = "Processing " & oFile & "..."
frmSplash.Show ownerform:=Form1
oneFile (oFile.Path)
Next
frmSplash.Label2 = ""
frmSplash.Hide
End If
End Sub
Private Sub oneFile(inputFile)
Dim fileName As String
Dim txtFile As Boolean, docFile As Boolean
Dim sourceFile As String
sourceFile = inputFile
If sourceFile Like "*.doc" Or sourceFile Like ".rtf" Then
docFile = True
sourceFile = processDOC(sourceFile)
ElseIf sourceFile Like "*.txt" Then
txtFile = True
sourceFile = processTXT(sourceFile)
End If
If (OptionUnchange.Value = True And inputFile Like "*.doc") Or OptionDOCFormat.Value = True Then
fileName = Left(sourceFile, Len(sourceFile) - 4) & ".doc"
wordapp.Documents.Open sourceFile, ReadOnly:=True
ActiveDocument.SaveAs fileName, wdFormatDocument
Documents(fileName).Close wdSaveChanges, wdWordDocument, True
Kill sourceFile
End If
End Sub
Private Function convertTotxt(sourceFile)
Dim inputFile As String
Dim outputFile As String
Dim fso As New FileSystemObject
If sourceFile Like "*.doc" Or soruceFile Like "*.rtf" Then
inputFile = sourceFile
outputFile = Left(inputFile, Len(inputFile) - 4) & ".txt"
If fso.FileExists(outputFile) Then fso.DeleteFile (outputFile)
wordapp.Documents.Open inputFile, ReadOnly:=True
ActiveDocument.SaveAs fileName:=outputFile, fileFormat:=wdFormatText
Documents(outputFile).Close wdSaveChanges, wdFormatText, True
convertTotxt = outputFile
Else
convertTotxt = sourceFile
End If
End Function
Private Function processDOC(sourceFile)
Dim outputFile As String
outputFile = convertTotxt(sourceFile)
Kill sourceFile
processDOC = processTXT(outputFile)
End Function
Private Function processTXT(sourceFile)
Dim tFile As String, tLine As String
Dim i As Integer
Dim deleteWord As String
Dim searchPattern As String
tFile = sourceFile & ".temp"
Open sourceFile For Input As #1
Open tFile For Output As #2
While Not EOF(1)
Line Input #1, tLine
For i = LBound(deleteWords()) To UBound(deleteWords())
searchPattern = deleteWords(i)
deleteWord = TestRegExp(searchPattern, tLine)
If deleteWord <> "Failed" Then
If OptionDeleteSent.Value Then
tLine = ""
Exit For
Else
tLine = Replace(tLine, deleteWord, "")
End If
End If
Next
Print #2, tLine
Wend
Close #1
Close #2
Kill sourceFile
Name tFile As sourceFile
processTXT = sourceFile
End Function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|