VBA - Apply document formatting to open Word doc - vba

I am using data in an Excel workbook to create an HTML string when a user clicks a button. I then use VBA to save that string as an .html file, open it in Word, and bring Word to the foreground. That all works great.
Now, I'd like to add a line(s) to the VBA to select the document formatting.
In the Word doc that opens, there's this in the ribbon:
When I hover over that formatting option, it's called "Lines (simple)." I can click it in Word, and it formats the text exactly how I want it. But, I'd like to do so automatically with the VBA over in Excel that created and launched the Word document. Unfortunately I just don't know which command or property that is and haven't been able to find it with searches or document inspection. I'd appreciate your help.

Related

VB code to place specific page of Visio document into Word

I've been working on a Word macro that includes a step allowing the user to select and insert a Visio file into the active Word document. Since many of the source Visio files are multipaged, and the specific Visio drawing to place in Word could be on any page of the Visio document, I need a way to allow the user to select the page they want to place.
Currently, my VBA code uses Appplication.FileDialog(msoFileDialogFilePicker) to set a variable for the Visio drawing's filepath (myVisioPath), then creates a variable for the Visio file and inserts it it into the Word doc as follows:
Dim myViz As InlineShape
Set myViz = ActiveDocument.InlineShapes.AddOLEObject(ClassType:="Visio.Drawing.15", FileName:=myVisioPath, LinkToFile:=False, DisplayAsIcon:=False, Range:=Selection.Range)
myViz.Select `Selects the Visio for subsequent actions`
The current working version of the macro selects whatever is on page 1 of the selected Visio file. I need a way to allow the user to select page 2, or page 3, or whatever page the target drawing is on in the Visio file. Currently, I'm thinking of creating a userform to allow choosing a page number, but I'm open to suggestions.
The post by Mathieu Guindon led me to the answer (would have voted it as the answer but that doesn't appear to be an option). Anyway, the code posted at Choose active page in Visio provided the basis of my solution. Set a variable for the Visio.Application object and refer to it to get both the page count of the user-selected Visio file (for error trapping if the user selects a page number that isn't available in the file) and to copy the user-selected page.

Jumping to a section in an MS Word document

I've just input a form button on my Excel sheet that the users can press to get more information on how parts of my code run. I'm planning on writing a big Word document that explains the different parts of the code in different sections. I'm looking to have a button that the user can click on in Excel for each part of the code, and that will direct them to the specific section within the Word document that I'd like to link.
So I can open a specific Word document with VBA no problems, but I'm unsure how/if I can jump to sections within the Word document.
If you use Hyperlinks instead of buttons, you can jump directly to bookmarks in your Word document, no VBA needed.
E.g. create a bookmark "Section5" in your Word document.
Then create a hyperlink in an Excel cell, with this target:
file://your_path\your_document.docx#Section5

VBA code in a template

In a Word template I have a command button with VBA code behind it.
The problem is that the code gets lost when a document is created using that template. The button is still visible, but the VBA code behind it disappears for some reason. This causes the button just to be clickable without performing any action.
The documents are saved in .docx format.
How can I 'glue' the button to its code so it doesn't get lost ?
First, make sure your VBA code is saved in the .dotm template that you make available to everyone (and not in your personal normal.dot/dotm template — this is only available on your machine).
Then, make sure the documents generated from the template are saved as .docm (not .docx).
.docx documents cannot contain VBA code. Anything saved in .dotx or .docx format will, by definition, lose all VBA code.
In a comment you say
"this doenst matter, as the document is just used for the macro."
This is incorrect; it does very much matter. .docx documents can't "be used for" macros because they can't contain macros.
For the macro to be always available, you need to store the macro in the normal template.
For example when recording a macro, select All Documents (Normal.dot) on Store Macro in.

How can I embed a PDF in a Word Doc, and access that PDF with VBA?

I have a word doc with some ActiveX buttons on it. When one of these buttons is pressed, a UserForm pops up with a corresponding PDF displayed, like so:
This is exactly the behavior I want. However, the problem is that for this to work, the user of the word doc needs to have each of the PDFs saved on their machine as well. Here's the code for one of the buttons:
Private Sub AC1Button_Click()
DisplayForm.AcroPDF1.LoadFile ("C:\Users\arose\Desktop\Security Control Doc\Sub PDFs\AC1.pdf")
DisplayForm.Show
End Sub
As you can see, the path is hardcoded in. I need to be able to distribute this word doc without needing to distribute a bunch of PDFs along with it, so is there any way to embed PDFs in a word document in such a way that they're accessible by VBA?
I've discovered here that it's reasonably easy to embed a PDF in any office doc. And I've tried that:
But I can't figure out how to access that PDF object in my VBA code, in order to display it on the UserForm.
Any insight is appreciated, thanks!
Embed the files (and display as icon to stop them taking over your document)
To activate the first OLE object in your document,
ThisDocument.InlineShapes(1).OLEFormat.Activate
is the command.

How to copy picture from Word to Powerpoint programmatically?

I am creating a macro that converts a Word 2007 document into a structured PowerPoint 2007 presentation. I am looping over all the paragraphs of the document and copying them over to the new presentation.
I am able to copy and paste the paragraphs to the presentation just fine. But I also need to be able to copy and paste the pictures from the Word document into the PowerPoint (and in the right location between paragraphs).
So far, I am able to detect if a paragraph is a picture by looking at the paragraph's style attribute - it will say "Figure". But, I don't know what to do from there. After looking online, it looks like I should be able to do this:
paragraphFromDocument.Range.Copy
currentSlide.Shapes.Paste
But, this doesn't seem to work. How do I copy a picture from Word to PowerPoint?
Thanks
EDIT
I've also tried:
paragraphFromDocument.Range.CopyAsPicture
currentSlide.Shapes.PasteSpecial(ppPasteMetafilePicture)
and get this error message:
Shapes.PasteSpecial : Invalid Request. Clipboard is empty or contains data which may not be posted here.
But, when I use that CopyAsPicture command, I am able to open up PowerPoint (with the picture still on the clipboard from the macro) and use the Paste Special command to paste the picture to the slide.
paragraphFromDocument.Range.Copy
currentSlide.Shapes.Paste
actually did end up working, but I had to remove all special characters from the range first. That is why it was giving me the error message.