Results 1 to 3 of 3

Thread: [2008] Replacing. Is this the best way ?

  1. #1

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    [2008] Replacing. Is this the best way ?

    Hello to all...

    I wana ask you if you think that's the best and the fastest way to replace <<words>> into a .doc file?
    I don't know other way and i think mine is too slow for doing this kind of job.

    Code:
            Dim word As New Microsoft.Office.Interop.Word.Application
            Dim doc As Microsoft.Office.Interop.Word.Document
            Try
                doc = word.Documents.Open(System.Windows.Forms.Application.StartupPath & "\cdmf.mbm")
                doc.Activate()
                Dim myStoryRange As Microsoft.Office.Interop.Word.Range
    
                For Each myStoryRange In doc.StoryRanges
                    With myStoryRange.Find
                        .Text = "<<filiala>>"
                        .Replacement.Text = Me.txt_filiala.Text
                        .Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue
                        .Execute(Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
                    End With
    
                    With myStoryRange.Find
                        .Text = "<<data>>"
                        .Replacement.Text = Me.txt_data_cm.Text
                        .Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue
                        .Execute(Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
                    End With
    
                    With myStoryRange.Find
                        .Text = "<<seriacm>>"
                        .Replacement.Text = Me.txt_seria_cm.Text
                        .Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue
                        .Execute(Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
                    End With
    
                    With myStoryRange.Find
                        .Text = "<<nrcm>>"
                        .Replacement.Text = Me.txt_nr_cm.Text
                        .Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue
                        .Execute(Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
                    End With
    
                    With myStoryRange.Find
                        .Text = "<<numeprenume>>"
                        .Replacement.Text = Me.txt_nume_prenume.Text
                        .Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue
                        .Execute(Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
                    End With
    
                Next myStoryRange
    
               doc.SaveAs(System.Windows.Forms.Application.StartupPath & "\cdm\" & "\" & Me.txt_cnp.Text & "\" & Me.txt_nr_cm.Text & ".mbm")
                doc.Close()
                doc = Nothing
    
            Catch ex As Exception
                MessageBox.Show("Error opening.")
            End Try
    I'm waiting your opinions and... Thanks in advice...
    Last edited by Alexandru_mbm; Jan 14th, 2009 at 04:20 PM.
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2008] Replacing. Is this the best way ?

    I don't see anything explicitly wrong with your code. The only optimization you might be able to add is that I don't think you need to loop through the Stories in a document to do a replace like this. It has been a while since I have done a replace, but I think you can do something that works across the whole document.

    Are you seeing performance problems with the code that would indicate a problem with performance?

  3. #3

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Re: [2008] Replacing. Is this the best way ?

    For replacing 20 words takes up to 15-20 seconds.

    All the code works under BackGroundWorker and if a user is giving another command for replace then will interrupt the current replacing job.

    Whats the best way to do it ?
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

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