Replacing and Writing to a .doc
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 Stringbl =
False Do While Not blstr = sr.ReadLine()
If InStr(str, "[name]") > 0 Thenbl =
True Elsesw.WriteLine(str)
End If Loop If Len(str) = 0 ThenMsgBox("Word Document is corrupt", MsgBoxStyle.Information)
Else Do While str = sr.ReadLine If InStr(str, "[name]") > 0 Thenstr = Replace(str, "[name]", "Demetrius")
sw.Write(str)
MsgBox(str)
End If Loop End If
Answers
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 Trydoc = 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 myStoryRangedoc.SaveAs(
"c:\test1.doc") Catch ex As ExceptionMessageBox.Show(
"Error accessing Word document.") End Try
All Replies
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 Trydoc = 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 myStoryRangedoc.SaveAs(
"c:\test1.doc") Catch ex As ExceptionMessageBox.Show(
"Error accessing Word document.") End Try- 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???
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
- 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???
- 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


