Replace a Picture in MS Word header with another in VBA - vba

Using MS access I need to open a word document and change certain images within the header. Whilst I have the code to find and replace any text that I need, I do not quite understand how to replace an image keeping it to a specific width and height.
The header within the Word document is constructed using a basic table with 3 columns as depicted below. The image in the right hand column will need to change for a specific logo.
I have managed so far to add an image to the document using the following code which gives the expected result...
With WordDoc.StoryRanges(wdPrimaryHeaderStory)
.InlineShapes.AddPicture FileName:="test.jpg"
End With
I understand that this is due to me not specifying a specific location, size etc but I am struggling to find resources which would instead allow me to either remove and add a new image to the right column or just swap the images out.
EDIT 1:

Related

Store image in Word document to insert later

I am currently building a word-template for a report. In this report are used red separators as part of the design. The separators are basically just images of red, curved lines.
Instead of copy and pasting these separators when needing them, is there a way to store the image somewhere in the document, allowing it to be inserted with just the click of a button in the ribbon? My first bet would be to create a macro somehow that would insert the image, however that would require the image to be stored in a very specific path on the computer.
As this document is gonna be used by lots of people without this image stored on their harddrive, i need another way.
Thanks so much in advance!
You could insert the images into bookmarks created via SET fields in, say, the document header, then reproduce them elsewhere in the document via cross-references. The images in the SET fields wouldn't themselves be visible.
For example:
{SET Image1 "Actual image1"}
to create the bookmark and
{REF Image1}
to display the image.
No code required.
By default, the size of the pictures when inserted via a cross-reference will be the same size as they are in the SET field, plus they'll be formatted as in-line with text.
If they're meant to be full-width images, simply make sure to insert the cross-reference into a new paragraph with 0 indenting.
To constrain the images' displayed size to something smaller than the width they're inserted into the SET field at, insert them into a table whose row height and/or column width has the required fixed dimension.
To apply text-wrapping, insert the cross-references into a table and format the table's wrapping as 'around'.
Note: The field brace pairs (i.e. '{ }') for the above example are all created in the document itself, via Ctrl-F9 (Cmd-F9 on a Mac or, if you’re using a laptop, you might need to use Ctrl-Fn-F9); you can't simply type them or copy & paste them from this message.

Insert Pictures by file path in document for hundreds of pages that match common name MS Access VBA

I have about 400 images that are in a separate directories by there name. I want to match these names with the filepath to the image attached to a report in access so the images will show up in access and I can print the report with the images included. I want to use VBA code as this is a repeating process and there are a lot of pages to insert a picture for otherwise done by hand.
I have some example data of my file paths. I have conditions.
I would like these images to show up in access so that they look like this when I export them. Here is an example. Each image from left to right will correspond to the trait 1,2,3 as shown in the table.
In access I have tried using hyperlink by I can't get it to match the Name to the name in the report like a master and child link or something in VBA might work too.
test3
Looks like this should be a two step process for you.
Add image placeholders. Insert an image where you want it on the report. Choose an image that would clearly show the image didn't load so you know when it's broken. Like a big red X.
Replace that image using an event that will load automatically as you make reports
EXAMPLE EVENT CODE:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Me![Imageframe].Picture = Me![Imagepath]
End Sub
Replace the name of the Imageframe with whatever you named your placeholder image. Replace the assignment with whatever contains your image path. You should be able to add as many of these as you want.
SOURCE:
http://www.databasedev.co.uk/bound_image_report.html

Can I use the Shrink Method for the whole document in word

I don't know anything about code. I work with e-learnings in Storyline 3. I sometimes localize these e-learnings and use the translation tool in Articulate which basically exports an MS Word file. Sometimes the target languages are longer and I need to decrease the font size by percentage for the whole document. Usually, there are at least 3 different font sizes that I need to decrease accordingly. I am wan to develop a macro that I will use for multiple documents.
I couldn't find a way to do this by percentage, but looks like the Shrink or Grow Methods will do the work! I found this code in the reference page but looks like it works only for a selected object. The issue is that the exported MS Word file is in a table with each text box in the storyline separated to a cell. When I select the whole table it does not work.
If Selection.Type = wdSelectionNormal Then
Selection.Font.Grow
Else
MsgBox "You need to select some text."
End If
Could you please help me and let me know if this would be possible for the whole document, or the selected table? It would be very much appreciated. Thanks in advance.
It is unclear from your question whether the table in the Word document contains the actual text boxes or just the text they contain.
If it is just the text then Shrink may work. I tested this on a document with a single table containing only text:
ActiveDocument.Range.Font.Shrink

Unable to extract and re-insert MS Word Content Control using VBA and InsertXML

This question is related to my other question: Range.InsertXML using Transform
In MS Word it is easy to insert a content control using VBA, for example:
ThisDocument.ContentControls.Add wdContentControlRichText, Selection.Range
I've recently started exploring more in the XML side of things, e.g.:
Debug.Print ThisDocument.Range.XML seems to (or actually does) produce the XML for a Word document. However, if I create a NEW, BLANK document and add a Content Control I am unable to extract and reinsert the Content Control (oCC).
My steps:
added 2 blank paragraphs to a new document
added oCC to the 2nd paragraph
selected the oCC paragraph
immediate window: thisdocument.Paragraphs(1).Range.InsertXML selection.Range.XML
At first glance it LOOKS like the Content Control was duplicated, BUT on closer inspection, it was deleted and only the formatted text remains (see image, top paragraph is actually just formatted text).
Thinking I could out smart MS Word I set the properties of the Content Control to '...can not be deleted', but that didn't help.
I've also tried to insert into a separate document in case the issue had something to do with duplication of something that ought to have been unique.
In a nutshell:
To answer this question I need a way to insert a Content Control to a document using a combination of VBA and XML (or confirmation that what I am attempting is not possible).
Just realized I should use Selection.Range.WordOpenXML instead of Selection.Range.XML

VBA Word - loop through document root elements only?

I am using VBA on Word and trying to do a task with a simple description:
Make document content fit on 1 or 2 pages, without leaving a lot of empty space on the last page.
To do that, I started with one simple step:
Determine total document content height.
(to later manipulate font size and line spacing according to my needs)
Problem is:
ActiveDocument.Content.Paragraphs lists all paragraphs with no regards to hierarchy. If I have a table in the content, all its contents will be listed, so I can't just use that to infer document content height.
I would like to work over the document structure tree, like
doc.header
doc.contents
paragraph1
table1
cell1
t1c1p1
cell2
t1c2p1
t1c2p2
paragraph2
paragraph3
doc.footer
And iterate only over the direct children of the content root to get their heights.
But I am having a hard time finding a way to do that. Can anyone give me some help?