Hi,
I have created a new word document using
Documents.Add()
how can i write into this document from a string?
thanks in advance.
Printable View
Hi,
I have created a new word document using
Documents.Add()
how can i write into this document from a string?
thanks in advance.
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
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.
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
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