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>
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Using "Word" as an object variable is not good. Is this XL or Word VBA? Your code to save the document is all wrong. I don't see any need to get the Word application that you are already working with just before saving some unknown "doc" variable. You need to open/create a document with the Word application, place the pictures at some specified location, then save the document as some specified name and location and then close/quit the Word application. HTH. Dave
 
Upvote 0
This is XL VBA, and sorry I define doc to be

Code:
Set doc = Word.Documents.Open("C:\Users\andrewt.EEE-MFG\My Documents\DC Disconnect Data Sheet Template.docx")

I will change "Word" to WordApp to avoid confusion.

So I open word with the above code then place the pictures by inserting them into the picture content controls (see code in first message) and then save it to my working directory. How would you recommend saving?

If I don't "GetObject" then Word does not activate and VBA thinks it is looking in XL from what I can tell
 
Upvote 0
Under further investigation I do not need to "GetObject" it still recognizes the word document without using that line of code.

I am still having the problem with the picture content controls causing unreadable content though.
 
Upvote 0
Untested. HTH. Dave
Code:
Sub PictoWord()
Dim ObjWord As Object, oCC1 As Object
Dim WrdDoc As Object, oCC2 As Object
'from XL, open Word template, insert pics, save file
'uses late binding
On Error GoTo ErFix
'Set the Word Object
Set ObjWord = CreateObject("Word.Application")
'open doc
Set WrdDoc = ObjWord.Documents.Open("C:\Users\" _
 & "andrewt.EEE-MFG\My Documents\DC Disconnect Data Sheet Template.docx")
With WrdDoc
Set oCC1 = WrdDoc.ActiveDocument.SelectContentControlsByTitle("TabPic").Item(1)
oCC.Range.InlineShapes.AddPicture Filename:="X:\XFER\ANDREW-T\DCD " _
       & LblVL & "Tabs\" & TermPlt & ".jpg", LinkToFile:=False, _
        Range:=oCC1.Range, SaveWithDocument:=True
        
Set oCC2 = WrdDoc.ActiveDocument.SelectContentControlsByTitle("LabelPic").Item(1)
oCC.Range.InlineShapes.AddPicture Filename:="X:\XFER\ANDREW-T\DCD " _
 & LblVL & "Labels\E" & LblVE & LblSB & LblPole & ".tif", LinkToFile:=False, _
        Range:=oCC2.Range, SaveWithDocument:=True
End With

'close and save .dot as .doc
'****YOU NEED TO SPECIFY FILE
WrdDoc.SaveAs "D:\TEST.DOC" 'change file name to suit

'clean up & quit
Set oCC2 = Nothing
Set oCC1 = Nothing
Set WrdDoc = Nothing
ObjWord.Quit
Set ObjWord = Nothing
Exit Sub

ErFix:
On Error GoTo 0
MsgBox "error"
Set oCC2 = Nothing
Set oCC1 = Nothing
Set WrdDoc = Nothing
ObjWord.Quit
Set ObjWord = Nothing
End Sub
 
Upvote 0
What is different about this code as opposed to mine? I see you are using a with statement and 2 different content controls callouts (oCC1 & oCC2). I am still encountering the unreadable content error.

I'm not sure what you are talking about with the .dot and .doc

I am not actually using a built in template, I am using a .docx that I have just named as my template. My code calls that template places information and saves it off as something else.

Thanks again for your help!
 
Upvote 0
The code I posted ensures control of the Word application and sets object variables to nothing. The oCC1 & oCC2 variables aren't really necessary... but easier to follow. I should have noticed that you were'nt actually opening a template. I think you will have to open the doc, insert the pics, save the doc then use copyfile to the new location... I'll post some code later. In the mean time... do the pics actually fit the controls? There were a couple of errors with the code I posted.Trial replacing this chunk of code and see if the pics are where there supposed to be. 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
        
Set oCC2 = .SelectContentControlsByTitle("LabelPic").Item(1)
oCC2.Range.InlineShapes.AddPicture Filename:="X:\XFER\ANDREW-T\DCD " _
 & LblVL & "Labels\E" & LblVE & LblSB & LblPole & ".tif", LinkToFile:=False, _
        Range:=oCC2.Range, SaveWithDocument:=True
End With

Wrddoc.Visible = True
MsgBox "Check doc for pics"
Wrddoc.Visible = False
End With
 
Upvote 0
Dave, the pictures do not end up where they are supposed to the get blown up to much larger than I want. For some reason inserting the pictures from vba bypasses the sizing of the picture content controls. To fix this I have put the picture content control in a textbox which keeps the pictures the size I want.
 
Upvote 0
I'm not sure if your still looking for help? Here's the copy file part that I mentioned. Dave
Code:
Dim FSo As Object
Set FSo = CreateObject("Scripting.FileSystemObject")
'close and save doc
WrdDoc.Close savechanges:=True
'copy file to new location (source,destination,save)
FSo.CopyFile "C:\Users\" _
 & "andrewt.EEE-MFG\My Documents\DC Disconnect Data Sheet Template.docx", _
    "D:\TEST.DOCx", True ' change destination to suit
Set FSo = Nothing
 
Upvote 0
Ok I am now using the copyfile but after inserting the pictures into the content control it is still giving me the unreadable content once I close down the document and open it back up
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

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