Using VB to modify Word2013 documents RRS feed

  • General discussion

  • It’s been a long time (just after Office XP came out) since I’ve created a VB program to modify some Word documents. Now I need to create a VB (VS2012) program to modify some Word2013 documents. I’ll be removing Bold, italic, and some high lighting. I’ll also be removing and adding some paragraphs. I’ll also be modifying some sentences.

    What references do I need to set up in my VB Windows application? Can someone help me get started?

    Tuesday, July 16, 2013 3:46 PM

All replies

  • You've posted to the Open XML SDK forum. Do you want to be doing this with Open XML, or is your intention to use Automation (like we did back in the days of Office XP)?

    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, July 16, 2013 3:59 PM
  • Cindy,

    I read your blog. I hope you decide to create a tutorial for OpenXML. I'll gladly send you a sample document that you can use. I'm not familiar with the OpenXML SDK, but I'm willing to learn. What are the advantages of the OpenXML SDK over Automation? I remember a little of the VB program I wrote to modify some OfficeXP documents. Either way It'll be a learning curve that I looking forward to. I like learning (or relearning) new ways of doing things in VB programming.

    • Edited by AirForce9797 Wednesday, July 17, 2013 10:44 AM correction
    Tuesday, July 16, 2013 4:42 PM
  • I'm planning to do an E-Book next year, after I get the German Word book finished...

    Anyway, the Office Open XML file formats are basically a Zip package of individual files that, together, make up the Word document (or Excel workbook or PowerPoint presentation). You can see that by adding .zip to the end of a Word file then looking at the Zip archive Windows will show you.

    The Open XML SDK is a "friendly" way of working directly with the file, rather than opening it in the Office application (Word). The advantages are 1) speed; 2) works on a server; 3) the Word application is not required on the machine where the code is running.

    Disadvantages: 1) Biggish learning curve; 2) Field results have to be updated (or the update confirmed) by the user.

    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, July 17, 2013 1:26 PM
  • I’m looking forward to the eBook. I know about the Office documents format from back when 2007 came out.

    I’m stumbling around learning on my own. I have a form that includes the following to import statements.

    Imports DocumentFormat.OpenXml.Packaging

    Imports DocumentFormat.OpenXml.Wordprocessing

    I was using the following procedure to update some test documents.

        Private Sub SearchAndReplace(strWORD As String, strFIND As String, strREPLACE As String)

            Dim docWORD As WordprocessingDocument = WordprocessingDocument.Open(strWORD, True)

            Using docWORD

                Dim docTEXT As String = Nothing

                Dim SR As StreamReader = New StreamReader(docWORD.MainDocumentPart.GetStream)

                Using SR

                    docTEXT = SR.ReadToEnd

                End Using

                Dim regTEXT As Regex = New Regex(strFIND)

                docTEXT = regTEXT.Replace(docTEXT, strREPLACE)

                Dim SW As StreamWriter = New StreamWriter(docWORD.MainDocumentPart.GetStream(FileMode.Create))

                Using SW


                End Using

            End Using

        End Sub

    The sentence, “If not, a formal violation letter will be sent.” Appears in all the documents. As a test, I used that as the strFIND and an empty string as the strReplace. In some documents it would work in others it didn’t work.

    I found that in some documents “if not,” was at the end of a tagged section and the rest of the sentence was in another tagged section. Only the documents that contained the sentence in one tagged section would work.

    Wednesday, July 17, 2013 4:09 PM
  • Are you still working on this?

    I'm afraid my life got very busy during the summer and I just noticed that this discussion "slipped through the cracks"...

    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, October 16, 2013 3:41 PM