Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Fri Sep 29, 2017 4:35 am

Dear support team,
Kindly advise on VB code in 2 cases:
1. Open exiting file > Replace certain text > Save file with a different name in a certain location/path > Close new file.
2. Open exiting file > Remove certain text > Save file (same file) after changes > Close file.

Thank you!
Lior

kdr13
 
Posts: 72
Joined: Fri Apr 15, 2016 4:35 pm

Fri Sep 29, 2017 6:22 am

Hello Lior,

Thanks for your inquiry.
Below are the code snippets for the two cases.
Case1. Replace text, save to a new file, then close the file.
Code: Select all
            Dim document As New Document()
            document.LoadFromFile("sample.docx")
            document.Replace("Text to replace", "new text", True, True)
            document.SaveToFile("newFileName.docx", FileFormat.Docx)

Case2. Remove certain text, save the file and then close it.
Code: Select all
            Dim filePath As String = "C:\Users\Administrator\Desktop\sample.docx"
            Dim document As New Document()
            document .LoadFromFile(filePath)
            Dim text As TextSelection() = document .FindAllString("text to remove", True, True)
            For Each seletion As TextSelection In text
         seletion.GetAsOneRange().OwnerParagraph.ChildObjects.Remove(seletion.GetAsOneRange())
            Next
            document .SaveToFile(filePath)
            document .Close()


Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Fri Sep 29, 2017 10:01 am

Thank you.

Code provided for Case2 does not work properly - Error in the "Next" line.

Kindly advise,

Thanks!

kdr13
 
Posts: 72
Joined: Fri Apr 15, 2016 4:35 pm

Fri Sep 29, 2017 10:08 am

Hi Lior,

The code is correct, the issue might be caused by the docuemnt.
Could you please share your sample file to help us with a better investigation?

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Return to Spire.Doc