Log in

View Full Version : [SOLVED:] Is it possible to extract Text and Images from a section at the same time?



Ozelotty
03-31-2020, 09:56 AM
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.

macropod
03-31-2020, 04:18 PM
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.

gmayor
03-31-2020, 08:45 PM
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.

Ozelotty
04-01-2020, 12:50 AM
Thanks, I'm going to try some things out then. Was just curious if what I thought of was possible.

macropod
04-01-2020, 12:58 AM
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.