Results 1 to 5 of 5

Thread: help in WORD

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Location
    Brussels
    Posts
    104

    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.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Here is a small example for you.
    VB Code:
    1. Option Explicit
    2. 'Add reference to MS Word xx.0 Object Library
    3. Private Sub Command1_Click()
    4.    
    5.     Dim oApp As Word.Application
    6.     Dim oDoc As Word.Document
    7.     Dim sString As String
    8.    
    9.     sString = "This is entered in from VB!"
    10.     Set oApp = New Word.Application
    11.     oApp.Visible = True
    12.     Set oDoc = oApp.Documents.Add
    13.     oDoc.Paragraphs(1).Range.Select
    14.     oApp.Selection.TypeText sString
    15.    
    16. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Location
    Brussels
    Posts
    104
    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.

  4. #4
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    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

    BOFH Now, BOFH Past, Information on duplicates

    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...

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Location
    Brussels
    Posts
    104
    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
  •  



Click Here to Expand Forum to Full Width