Moving an image in microsoft word using VBA in excel - excel

I am trying to make an automated invoice maker and I currently am having difficulty positioning an image in a word doc using excel VBA
The image loads fine into the word document, yet it is the position that is causing me issues.
edit:
The specific issue that is causing is that the image is intended to be at the top of the page, but appears below the inserted text.
Thanks in advance for any help
Set wordObject = CreateObject("Word.Application")
'create the document object and add documents to it
Set documentObject = wordObject.documents.Add
'setting logo
Set logoObject = documentObject.inlineshapes
'make it visible
wordObject.Visible = True
'create selection object
Set selectionObjectHeader = wordObject.Selection
'place the logo
logoObject.AddPicture ("C:\Users\conno\Documents\logo.png")
With logoObject
.ConvertToShape
End With
With caniprelogoObject.Shapes(1)
.Top = 100
.Left = 100
End With

Related

Excel to update PowerPoint Presentation

I have a presentation and I have to update it every week. The information I update are a bunch of imagens I generate from a Excel pivot tables (copy from Excel and paste directly on PowerPoint).
Today I can do this doing this:
Set objPPT = CreateObject("PowerPoint.Application")
objPPT.Visible = True
Set PPTPrez =
objPPT.Presentations.Open("\\network_folder\presentation.pptm")
Set pSlide = PPTPrez.Slides(2)
If pSlide.Shapes.Count <> 0 Then
ActiveWorkbook.Sheets("Pivot1").Range("A8:Z18").CopyPicture
pSlide.Shapes.Paste
EndIf
It work flawless... But I need a litle bit more control and precision...
I need to select the current image on slide, delete it and paste the new one in the same location... Some slides have 3 images or more...
I cann't figure it out how to properly tell to VBA what image are what and choose the pivot table with the correct info for that image... I don't even know if this is possible...
But another solution I have tried is how to specify the position and dimensions of the image on the slide... I can before update, delete all imagens... In this scenario, how to specify the dimensions and positioning?
Thanks!!!
Ps.: Sorry my bad english
This example (based on your code) may point you in the right direction. You need to know the powerpoint shape name (which you can get via VBA or via the ribbon Home-Select-Selection Pane.
Option Explicit
Public Sub UpdateShapes()
Dim vPowerPoint As PowerPoint.Application
Dim vPresentation As Presentation
Dim vSlide As Slide
Dim vShapeName As String
Dim vShape, vNewShape
Set vPowerPoint = New PowerPoint.Application
vPowerPoint.Visible = True
' Open the powerpoint presentation
Set vPresentation = vPowerPoint.Presentations.Open("\\network_folder\presentation.pptm")
' Set slide to be worked on
Set vSlide = vPresentation.Slides(2)
' Set shape to (for this example) "Picture 3"
vShapeName = "Picture 3"
Set vShape = vSlide.Shapes(vShapeName)
' Copy and paste new shape (picture) of range specified
ThisWorkbook.Sheets("Sheet1").Range("A6:B9").CopyPicture
Set vNewShape = vSlide.Shapes.Paste
' Align size and position of new shape to that of old shape
With vNewShape
.Width = vShape.Width
.Height = vShape.Height
.Left = vShape.Left
.Top = vShape.Top
End With
' Delete original shape, rename new shape to original so code works next replace cycle
vSlide.Shapes(vShapeName).Delete
vNewShape.Name = vShapeName
End Sub

How to insert an image in Word with an userform in Excel-VBA

I have created an userform in Excel which has an image picker that saves the chosen file as the image of a picture. ( Image1.Picture)
I need to make a button that copies that image and appends it to an existing Word Document but I can't manage to make it work. Any idea? Thanks in advance for your help.
Try this function, I have built the function that inserts the image at strImagePath to the document at strDocPath (e.g "D:\MyImage.img" and "D:\MyDoc.docx").
If you have a problem with image types (.jpg or .gif,etc), do a research with function objShapes.AddPicture
Function FnImageInsert(strImagePath, strDocPath)
Dim objWord
Dim objDoc
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open(strDocPath)
objWord.Visible = True
objWord.Selection.TypeText (vbCrLf & "One Picture will be inserted here....")
objDoc.InlineShapes.AddPicture (strImagePath)
End Function
Then you can create a userform to insert these paths and run the function.
Hope it is helpful.

Using Excel VBA to automate form filling in Internet Explorer

I want to take values from an excel sheet and store them in an array. I then want to take the values from the array and use them to fill the web form.
I have managed to store the values in the array and I have managed to get VBA to open Internet Explorer (IE)
The code runs and no errors appear, but the text fields are not being populated, nor is the button being clicked
(The debugger points to [While .Busy] as the error source, located in the WITH block)
How do I go about filling the form (that has a total of 3 text boxes to fill)?
There is also a drop down menu that I need to choose a value from, but I need to fill the text boxes prior to moving on to that part of the task.
Sub CONNECT_TO_IE()
the_start:
Dim ie As Object
Dim objElement As Object
Dim objCollection As Object
acct = GET_CLIENT_NAME()
name = GET_CODE()
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate ("<<my_website>>")
ie.FullScreen = False
On Error Resume Next
Do
DoEvents
If Err.Number <> 0 Then
ie.Quit
Set ie = Nothing
GoTo the_start:
End If
Loop Until ie.readystate = 4
Application.Wait Now + TimeValue("00:00:10")
ie.Document.getElementbyid("<<field_1>>").Value = "PPP"
ie.Document.getElementbyid("<<field_2>>").Value = "PPP"
ie.Document.getElementbyid("<<field_3>>").Click
Set ie = Nothing
End Sub
UPDATE: Turns out the reason this wasn't working is because there are some settings in the HTML of the site that do not allow for the automation to occur, so any code versions I had were correct but they were doomed to fail. So you were correct in that regard #TimWilliams.
I know this because the website I was trying to access is on a secure server/virtual machine. I edited the code to fill in the google search bar and it did not work on the virtual machine however when I ran the same code locally, it worked fine.

Copy specific image from webpage and paste into Excel using VBA

I'm trying to take a character string from a cell, copy it into a free QR code generator (ex| http://goqr.me/ ), and bring the resulting image back into my spreadsheet. (I like this generator because you don't need to click "submit" - it updates the image as the text is entered. Assuming/hoping that feature makes this easier)
I'm trying to avoid the installation of new barcode fonts and the purchasing of barcode specific packages.
Using the below code, I can get the browser to open and enter the desired text. I need a way to get the image back into my spreadsheet now. The code I'm using currently just returns "[object]" (as text) in cell C1.
How can I use VBA to get this image onto my clipboard?
Public ieApp As Object
Sub Barcode()
Dim ieDoc As Object
Dim InputBox As Object
Dim qrBoxImage as Object
barcodeTerm = Range("B1").Value
If TypeName(ieApp) = "Object" Or TypeName(ieApp) = "Nothing" Then
Set ieApp = CreateObject("InternetExplorer.Application")
ieApp.Navigate ("http://goqr.me/")
End If
While ieApp.ReadyState <> 4
DoEvents
Wend
ieApp.Visible = True
Set ieDoc = ieApp.Document
Set InputBox = ieDoc.getElementsByName("text")
Set qrBoxImage = ieDoc.getElementByID("qrcode-preview-image")
InputBox.Item(0).Value = barcodeTerm
Range("C1").Value = qrBoxImage
Set ieDoc = Nothing
End Sub
You can follow the steps detailed over here:
http://social.technet.microsoft.com/wiki/contents/articles/23860.print-screen-to-an-image-using-access-vba.aspx
Although the example is used in Access, it's still VBA code so it still should work in Excel

How do I keep the new structure of web imported data after refresh?

I am creating an Excel database. I would like to import names, emails and job positions of all employees of a firm from the firm website.
I choose Data->From Web and select the whole page, as it is the only possibility.
The page shows no table with data; just a long list of photos of employees with names, emails and job positions next to them
I import the data into my Excel spreadsheet: the format is very bad. So I begin cut and paste creating a column for "names", one for "email" and similarly for "job position". All other information is manually canceled.
I would like to refresh data keeping this new format. Unfortunately, every time I refresh the imported data using the "refresh all" button, they return to the original format.
How can I keep the new format of my web imported data, after refresh?
I thank you all for your support!
Kr,
A
I've put together an example that will extract the name and title from that page you specified and put them into sheet 1.
The code will only work providing the layout of the underlying html remains the same. It does not support updating of an existing list (anything on sheet 1 is removed prior to reading the list again)
To use this code you must place it in a new code module (not the worksheet or workbook sections) and you can run it either from the code editor or via the macros menu in the main excel window.
' Note: This code requires the following references to be loaded.
' Microsoft HTML Object Library (mshtml.tlb)
' Microsoft Internet Controls (ieframe.dll)
' To add a reference
' In the VBA Code Editor, in the Tools Menu click the References item
' Scroll through the list and ensure that the references are selected
' Press OK and your done.
Sub Scrape()
Dim Browser As InternetExplorer
Dim Document As HTMLDocument
Dim Element As IHTMLElement
Dim Elements As IHTMLElementCollection
Dim empName As String
Dim empTitle As String
Dim Sheet As Worksheet
Set Sheet = ThisWorkbook.ActiveSheet
Sheet.UsedRange.ClearContents ' Nuke the old list
Set Browser = New InternetExplorer
Browser.navigate "http://www.hsbc.com/about-hsbc/leadership"
Do While Browser.Busy And Not Browser.readyState = READYSTATE_COMPLETE
DoEvents
Loop
Set Document = Browser.Document
Set Elements = Document.getElementsByClassName("profile-col1")
For Each Element In Elements
empName = Trim(Element.Children(1).Children(0).innerText)
empTitle = Trim(Element.Children(1).Children(1).innerText)
Sheet.Range("A1:B1").Insert xlShiftDown
Sheet.Cells(1, 1).Value = empName
Sheet.Cells(1, 2).Value = empTitle
'Debug.Print "[ name] " & empName
'Debug.Print "[ title] " & empTitle
Next Element
Set Browser = Nothing
Set Elements = Nothing
End Sub

Resources