Visual Basic > Visual Basic Forums > Visual Basic Language > Replacing and Writing to a .doc
Ask a questionAsk a question
 

AnswerReplacing and Writing to a .doc

  • Friday, April 28, 2006 12:13 AMTryin2Bgood Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I have this dee.doc that has a [name] in it i want to replace that with Demetrius and write that line to a new word doc called deee.doc that i am creating...It creates the document but it does not write to the file the info .....what am i missing...any help thanks 

     

    Dim sr As StreamReader = New StreamReader("C:\dee.doc")

    Dim doc As FileStream = New FileStream("c:\deee.doc", FileMode.Create)

    Dim sw As StreamWriter = New StreamWriter(doc)

    Dim str As String

    Dim bl As Boolean = False

    Dim str1 As String

    bl = False

    Do While Not bl

    str = sr.ReadLine()

    If InStr(str, "[name]") > 0 Then

    bl = True

    Else

    sw.WriteLine(str)

    End If

    Loop

    If Len(str) = 0 Then

    MsgBox("Word Document is corrupt", MsgBoxStyle.Information)

    Else

    Do While str = sr.ReadLine

    If InStr(str, "[name]") > 0 Then

    str = Replace(str, "[name]", "Demetrius")

    sw.Write(str)

    MsgBox(str)

    End If

    Loop

    End If

Answers

  • Friday, April 28, 2006 12:36 AMspotty Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Why do it using the streamwriter, I would think you are certainly more prone to problems this way.

     

    Try the following as the basis for your code - this will open a test document do a search/replace and a save as - obviously you can change the hardcoded values to variables.

     

     

    Dim word As New Microsoft.Office.Interop.Word.Application

    Dim doc As Microsoft.Office.Interop.Word.Document

    Try

    doc = word.Documents.Open("c:\test.doc")

    doc.Activate()

    Dim myStoryRange As Microsoft.Office.Interop.Word.Range

    For Each myStoryRange In doc.StoryRanges

    With myStoryRange.Find

    .Text = "findme"

    .Replacement.Text = "findyou"

    .Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue

    .Execute(Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)

    End With

    Next myStoryRange

    doc.SaveAs("c:\test1.doc")

    Catch ex As Exception

    MessageBox.Show("Error accessing Word document.")

    End Try

     

     

All Replies

  • Friday, April 28, 2006 12:36 AMspotty Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Why do it using the streamwriter, I would think you are certainly more prone to problems this way.

     

    Try the following as the basis for your code - this will open a test document do a search/replace and a save as - obviously you can change the hardcoded values to variables.

     

     

    Dim word As New Microsoft.Office.Interop.Word.Application

    Dim doc As Microsoft.Office.Interop.Word.Document

    Try

    doc = word.Documents.Open("c:\test.doc")

    doc.Activate()

    Dim myStoryRange As Microsoft.Office.Interop.Word.Range

    For Each myStoryRange In doc.StoryRanges

    With myStoryRange.Find

    .Text = "findme"

    .Replacement.Text = "findyou"

    .Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue

    .Execute(Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)

    End With

    Next myStoryRange

    doc.SaveAs("c:\test1.doc")

    Catch ex As Exception

    MessageBox.Show("Error accessing Word document.")

    End Try

     

     

  • Friday, April 28, 2006 12:56 PMTryin2Bgood Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hey spotty thanks for getting me on the right track.....dont you need to make New object from the word.application and word.document or is 1 enough?? I was getting an exception about object not set to instance of an object and i use the New() keyword on both word and application and it works fine......whats the proper way???
  • Friday, April 28, 2006 4:59 PMspotty Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Nope the code I posted works just fine.

    The Application would need to be an new object but the document is a reference to the document and didn't need to be instanced with new.

    As long as you've got the code working thats good.  

    Which versions of VB and Word are you using ? Just out of curiousity

     

  • Tuesday, May 02, 2006 1:08 PMTryin2Bgood Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I am using VB2003 and I am using word 2003...What i would usually do is save my word document as a xml file and create a template and just re-write a new file and just parse out the elements in the template that i needed to replace with live data...Is there any books that goes over COM+ and how to use them???
  • Thursday, April 26, 2007 5:22 PMbyroman Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello,
    I would like to use this script to customize an application in .doc format.
    Heres what Ive got:

    Dim word As New Microsoft.Office.Interop.Word.Application
    Dim doc As Microsoft.Office.Interop.Word.Document
    Try
    doc = word.Documents.Open("C:\Documents and Settings\Sean\Desktop\application.doc")
    doc.Activate()
    Dim myStoryRange As Microsoft.Office.Interop.Word.Range
    For Each myStoryRange In doc.StoryRanges
    With myStoryRange.Find
    .Text = "(name)"
    .Replacement.Text = TextBox1.text
    .Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue
    .Execute(Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
    End With
    Next myStoryRange
    doc.SaveAs("c:\C:\Documents and Settings\Sean\Desktop\application1.doc")
    Catch ex As Exception
    MessageBox.Show("Error accessing Word document.")

    Basically the same as what you have got above. When I run the program, heres the errors i get:

    Type 'Microsoft.Office.Interop.Word.Application' is not defined.
    Type 'Microsoft.Office.Interop.Word.Document' is not defined.
    Type 'Microsoft.Office.Interop.Word.Range' is not defined.

    Im running Visual Basic Express edition 2005. Why wont this code work?
    Thanks a lot.
    Sean