VBA/Word picture content control causing unreadable content

atycks

New Member
Joined
Jan 15, 2013
Messages
10
I am using VBA to create a word document (.docx). This word document contains plain text content controls as well as picture content controls. I then use VBA to automatically select a picture based on the code below

Code:
        Set oCC = Word.ActiveDocument.SelectContentControlsByTitle("TabPic").Item(1)
        On Error GoTo TabErrorHandler
        oCC.Range.InlineShapes.AddPicture Filename:="X:\XFER\ANDREW-T\DCD " & LblVL & "Tabs\" & TermPlt & ".jpg", LinkToFile:=False, _
        Range:=oCC.Range, SaveWithDocument:=True
        
        Set oCC = Word.ActiveDocument.SelectContentControlsByTitle("LabelPic").Item(1)
        On Error GoTo LabelErrorHandler
        oCC.Range.InlineShapes.AddPicture Filename:="X:\XFER\ANDREW-T\DCD " & LblVL & "Labels\E" & LblVE & LblSB & LblPole & ".tif", LinkToFile:=False, _
        Range:=oCC.Range, SaveWithDocument:=True

After the pictures are inserted via my code I save the document using

Code:
Set Word = GetObject(, "Word.Application")
    doc.Save

After the document has been closed down I try to open it again and I am told "The file <filename> cannot be opened because there are problems with the contents."

When I click details it says "Unspecified error" and "Location: Part: /word/document.xml, Line: 2, Column: 0"

If I click ok it says "Word found unreadable content in "<filename>". Do you want to recover the contents of this document? If you turst the source of this document, click Yes.

Clicking Yes opens the document with all the contents and it is now renamed to Document 1. If I click no it does not open.

Please help as I am almost done with my code, just confused why the picture content controls are giving me problems. I can insert the pictures into the content controls manually and I get no errors saving but doing it from VBA gives unreadable content. I have read some about xml stuff but I have no idea what they are talking about. Thanks so much!</filename></filename>
 
Is the outcome document correct? Here's some untested code to fit the pic to the control. Dave
Code:
With Wrddoc
Set oCC1 = .SelectContentControlsByTitle("TabPic").Item(1)
oCC1.Range.InlineShapes.AddPicture Filename:="X:\XFER\ANDREW-T\DCD " _
       & LblVL & "Tabs\" & TermPlt & ".jpg", LinkToFile:=False, _
        Range:=oCC1.Range, SaveWithDocument:=True
End With
'Format the shape
With Wrddoc.oCC1.Range.InlineShapes(1)
    .LockAspectRatio = msoTrue
    .ScaleHeight 0.3, True 'height scaled to 30% of original picture size
    .ScaleWidth 0.3, True 'width scaled to 30% of original picture size
    .WrapFormat.AllowOverlap = False
    .WrapFormat.Side = 1 'wdWrapLeft
    .WrapFormat.Type = 0 'wdWrapSquare
End With
 
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
It is not liking the .wrapformat command also the scale command makes the image disappear (I stepped thru the code to find out what line was making it disappear. The outcome document is correct the only thing I'm trying to figure out is how to get it to stop being unreadable content. I know it has to do with the picture content controls cause otherwise it is fine. For now I have been manually saving it as a .doc not a .docx and then it runs compatibility mode and converts the content controls to static content and the pictures to effects. Is there any way to convert my picture content controls to effects in vba?
 
Upvote 0
Any chance that you could just use a bookmark or table as a placeholder for the pics? You could VBA save the file as a .doc and then copyfile it to a .docx (maybe?)... sorry I have no idea re. conversion of pic content control to effects. Dave
 
Upvote 0
Re: [Solved] VBA/Word picture content control causing unreadable content

I got to work with bookmarks! I hadn't used bookmarks before they seem very useful especially for inserting pictures.

Thank you for all your help! I will mark this as solved!

Here is my code for inserting the pictures into my word document.

Code:
    On Error GoTo TabErrorHandler
Set oCC1 = WordApp.ActiveDocument.Bookmarks("TabPic")
    oCC1.Range.InlineShapes.AddPicture Filename:=ActiveWorkbook.Path & "\DCD " & LblVL & "Tabs\" & TermPlt & ".jpg", LinkToFile:=False, _
    Range:=oCC1.Range, SaveWithDocument:=True
    
    If ConvKit.Value = False Then
    On Error GoTo LabelErrorHandler
Set oCC2 = WordApp.ActiveDocument.Bookmarks("LabelPic")
    oCC2.Range.InlineShapes.AddPicture Filename:=ActiveWorkbook.Path & "\DCD " & LblVL & "Labels\E" & LblVE & LblSB & LblPole & ".jpg", LinkToFile:=False, _
    Range:=oCC2.Range, SaveWithDocument:=True
    End If
 
Last edited:
Upvote 0
Re: [Solved] VBA/Word picture content control causing unreadable content

Thank you for posting your outcome as it may be useful for others. You are Welcome. Dave
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top