Consulting

Results 1 to 5 of 5

Thread: Is it possible to extract Text and Images from a section at the same time?

  1. #1
    VBAX Newbie
    Joined
    Mar 2020
    Posts
    2
    Location

    Is it possible to extract Text and Images from a section at the same time?

    Hi everyone,

    doing my first real VBA project for work and am a bit stuck. The basics is this:

    I have a main document with all the information for 3 types of clients. Using a form, depending on which client type is selected a new document with the appropriate parts of information is generated. Meaning that from 1 document I have to generate 3 different documents.

    All of this wouldn't be a problem if it was only text. I'm able to extract the text and insert it into the new file without problems. The problem is that there are multiple images in the document though. These don't get copied at the same time.

    The only solution I could see would be to actually select different parts and copy the selections but I would obviously rather have it work by extracting the values of different sections.

    What I have, that is copying only text looks something like this:

    Private Sub Client1()
    Dim i As Integer
    Dim strText As String
    'Copying and inserting Specific Paragraphs for Clients. Change I-Values in If statement to customize client-output
    For i = 1 To 10
    If i = 1 Or i = 3 Or i = 6 Then
    strText = mdocOld.Sections(i).Range

    mdocNew.Range.Sections.Add

    mdocNew.Range.InsertAfter strText
    End If
    Next i
    End Sub

    Now obviously a String can only store text, so I'm a bit at a loss for what to do in order to store both text and images at the same time.

    Thanks in advance for the help.

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    If you start off with a template containing all three variations, and bookmark each variant, all you need do is create a new document from that template then delete the unwanted bookmarked ranges.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    While Paul's suggestion is a perfectly valid one, there are also other ways that you can do this, that don't require you to copy parts of a document and insert them in another. You could, for example save the alternative parts including their illustrations as a building blocks, in the template, and insert them as required at bookmarks. Or you could insert the fixed text parts of the document as text/images into the template and insert content controls to accommodate the variable parts, then use your macro to populate the content controls. Much depends on what the differences are.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  4. #4
    VBAX Newbie
    Joined
    Mar 2020
    Posts
    2
    Location
    Thanks, I'm going to try some things out then. Was just curious if what I thought of was possible.

  5. #5
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by Ozelotty View Post
    Was just curious if what I thought of was possible.
    Yes, but not using.InsertAfter strText. And, as Graham and I have pointed out, there are better ways.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •