Change Web Image via Excel Macro - vba

I am using Excel macros to drive image changes for a webpage. If I want an image to change on the page, I execute the macro, which grabs the desired image and replaces the file the webpage is using, and the webpage refreshes automatically.
It works perfectly in a local machine, but the intent is for remote users to pull up the webpage and see the new image as I control the macro.
Here is the excel macro:
Sub update_image()
newName = "C:\Cases\Nominal\new_image_to_show.png"
oldName = "C:\Cases\Images\updated_image.png"
FileCopy newName, oldName
End Sub
When I replace the file locations with the web locations, the macro stops working. The web space is an internal location within my organization, and two options I tried were:
newName = "https:://location_of_content\Cases\Nominal\new_image_to_show.png"
oldName = "https:://location_of_content\Cases\Images\updated_image.png"
and
newName = "file://///location_of_content\Cases\Nominal\new_image_to_show.png"
oldName = "file://///location_of_content\Cases\Images\updated_image.png"
I also tried relative path nomenclature,
newName = ThisWorkbook.Path & "\Cases\Nominal\new_image_to_show.png"
but in both cases I get
Run Time Error '52'
Any ideas on how I can rename the paths? Or alternatives to Excel Macros?

Related

Local copy of word document loaded from shared drive not working with VBA saveas PDF method. Doesn't save the document

I have a macro that works fine in the shared drive when it is only accessed by one/first person. When it is accessed by the second person while the first person still has it open, it says it's already open. When I click "create a local copy and merge changes later" the macro will work up until it saves the file.
The macro basically invokes a userform to collect information, fill in the document, and then it should save the document as a PDF to the desktop. The PDF isn't saving in the local copy version for some reason. When the "Follow hyperlink" comes up, it says "Error 4198, command failed". I check my desktop and the file isn't there, leading me to believe that this error is in relation to the file not being created....
I just need the macro to allow the document to be saved to their desktop as a pdf, whether they're in the normal version, or the local copy created as a byproduct of the shared drive rules.
Main_Form.hide
enviro = CStr(Environ("USERPROFILE"))
sName = Format(Date, "mm-dd-yyyy") & " Denial Letter - Invoice " & Invoice_Text.Value & ".pdf"
sPath = enviro & "\Desktop\"
ThisDocument.SaveAs2 FileName:=sPath & sName, fileformat:=wdFormatPDF
fullName = sPath & sName
ThisDocument.FollowHyperlink fullName
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
Use a template (dotm), not a document. Create new documents from the template, that way there will be no document "lock".
Also, don't use ThisDocumentas that means specifically the document in which the VBA code is located, use ActiveDocument, instead.

Relative path to images in access reports

It is my first time building a database and I wanted to share a solution to a problem I encountered.
My problem was that I wanted to show different images for each record in a report, but I also wanted to be able to move the database. This was a problem. I search in all the forums and all the different solutions didn’t work. I also found an article written by Microsoft saying that the only way is to either store the full path to the images or to store the image in the database. But this causes a problem if the database is moved, or storing the images in the database will take up a lot of storage space.
The problem is that the codes doesn’t work for each record in the report, the codes are for the entire report. So writing codes to find the backend and the image folder would result in displaying the first image in the report for all the records in that report.
However I discovered, when only storing the name of the image in a table, it would sometimes work (but it shouldn’t have, because I didn’t have the path) but when I restarted the database it would stop working. Investigating further I discovered that whenever you open the file browser it will store the path in some kind of memory. As long as the path to the images is stored in the memory it will be able to link the images to the path.
So my solution…
When the form, from where you access the reports is opened, the file browser is opened and the path to the images is pasted in (using codes to find backend and the image folder) and then the browser is closed. And this creates a link to the image names (stored in a table) with the path. And each different images will be shows for each different records in the report.
Not a pretty solution. Whenever the form is opened, you will see a flash of the file browser. But it gets the job done.
In the load form event:
`' this will find the backend and the image folder:
Dim filepath As String
Dim strBackEndPath As String
Dim lenPath As Integer
Dim i As Integer
Dim j As Integer
strBackEndPath = CurrentDb.TableDefs("yourTabeInBackend").Connect
j = InStrRev(strBackEndPath, "=") + 1
strBackEndPath = Mid(strBackEndPath, j)
BackPath = Left(strBackEndPath, InStrRev(strBackEndPath, "\"))
filepath = BackPath & "YourImageFolder\"
'this will open the folder browser and paste in the path and close it:
Dim f As Object
Set f = Application.FileDialog(msoFileDialogFolderPicker)
Dim varFile As Variant
Dim strPath As String
Dim fileName As String
With f
.InitialFileName = (filepath)
.AllowMultiSelect = False
SendKeys "{ESC}", True
f.Show
For Each varFile In .SelectedItems
Next varFile
End With
`
You can move the pictures to a subfolder of the folder of your database.
Then save the pictures' names like this:
Picture1.jpg
Picture2.jpg
etc.
When you run the application, obtain the path to the pictures:
PictureFolder = CurrentProject.Path & "\FolderName\"
Then the path to a picture will be:
PictureFolder & Me!PictureFileName.Value
When you "move" your database, move both the database file and the folder with the picture files with it.
yup, i just encountered same problem and i agree with what Richard_Ha said, but in my case i solve it with storing image path on textbox and its work..
first textbox name "fileimage_txt" and bound to list of image filename on table
second textbox name "image_path" with property
Source Control : =GetImagePath()
visible : No (if u dont want it get printed)
and image control with property
Source Control : =[path_txt] & [fileimage_txt]

Deleting a file immediately after closing it - 'Permission Denied' error

I want to save an email as a Word file through a macro, and then replace this with a converted PDF file via Word. The conversion part is working fine - the issue is when I try to delete the original .doc file.
Dim objWrd As Object
Dim objWrdDoc As Object
Dim wrdCurrentPrinter As String
Set objWrd = CreateObject("Word.Application")
Set objWrdDoc = objWrd.Documents.Open(filePath & fileName)
''Print' file as PDF - current printer is stored so it can be reverted back afterwards
wrdCurrentPrinter = objWrd.ActivePrinter
objWrd.ActivePrinter = "Microsoft Print To PDF"
'File name is specified here to avoid Save As prompt. PrintToFile property is 'True'
objWrd.PrintOut , , , filePath & Replace(fileName, ".doc", ".pdf"), , , , , , , True
objWrd.ActivePrinter = wrdCurrentPrinter
objWrd.Quit
Set objWrd = Nothing
Kill filePath & fileName
At Kill filePath & fileName I get error 70 - Permission denied.
I am able to delete the file manually without any problems, and if I add a breakpoint and step through the 'Kill' line, it works when there is even a slight delay between Word closing and the Kill command. Therefore I suspect that the code is being processed so quickly so that the file is still open at the time of running the command.
I have a feeling I may need to go down the route of creating a delay, which I have been having trouble with in Outlook. Is this the likely solution or is there something else that I have missed?
I have been able to get this to work consistently by simply closing the Word document before quitting Word altogether:
objWrdDoc.Close
objWrd.Quit
Set objWrd = Nothing
The error has not appeared since adding this line and testing with various emails.

Use a generic pathway as default

I have a small program that creates a load of files and saves to a folder specified by the user. Currently the top of the userform looks like the following when initialised:
I'd prefer if when the userform opens and also when the dialog for choosing a appears (via the Destination button) that a general default is already shown:
Can the program find the pathway to a user's desktop?
To get the path to the users desktop, use:
PathToDesktop = Environ("USERPROFILE") & "\Desktop"
If you are doing this in excel-vba, the following code would save the current workbook as a copy to the user's desktop... you can probably figure out what you're doing from there.
Dim DesktopPath As String
DesktopPath = CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator
ActiveWorkbook.SaveAs DesktopPath & "filename.xls"

FORMTEXT Appearing in Word File Name Generated from Protected Form-Field Value and BookMark for VB Script

I am using the following VB Script in a Word 2010 Doc saved as a Microsoft Word Macro-Enabled Template that is protected for form fields:
Sub SaveWithBkMarkedText()
'This code saves the Word file using the bookmark value for Maintenance Memo.
'The file is also saved to a folder in KnowHow for files related to this template.
Dim FileName As String
FileName = ActiveDocument.Bookmarks("mmn").Range.Text
'Use the C:\ code when saving the file locally
ActiveDocument.SaveAs "C:\Download\TemplatesFolders\" & FileName & ".doc"
MsgBox "Your Draft has been saved to KnowHow's Release Documentation site." & _
&vbCrLf & "The file name uses the MM that you included earlier: " & FileName, _
vbInformation + vbOKOnly, "Draft Saved to Minerva"
End Sub
The value entered into the Form Field for a FORMTEXT legacy-form object uses the Bookmark as the file name. Example, if the user enters 12345 as the value, the file is saved using this value as the filename: 12345.doc. This worked fine until a week ago when the filename is now being Prefixed with FORMTEXT 12345.doc. I have tried using this same VB script in older versions of Word on a different machine, and created from a NEW Template with the script added in from scratch, and the same issue is appearing on that machine as well. Prior to this, I was able to update my template with NO problem, but now I can't update this one FORMTEXT field without it affecting the whole file. I can update any other FORMTEXT in the template that does not use the Bookmark value as the file name, and it works. Also, I have tried changing the Bookmark Reference to another FORMTEXT object, as well as saving the file as a .DOCX and the same problem occurs regardless. What is causing the FORMTEXT to appear in the filename?
You have to un-protect the document (template) and then make the VBA programming. Once it is done, then you can protect it again (for filling forms) and you will not see the "FORMTEXT" in the filename when you run the macro.
Hope it helps.
To the OP, did you resolve this issue? I'm now having the same problem, I'm using form field text with bookmarks and using VB.net to get the bookmark.text which is now prefixed with FORMTEXT, just using a bookmark on it own and its OK, no prefix. I'm going to try and remove the first 9 characters from the result using code, a workaround, yes but it might work.
Know this is an old thread but ran into same issue. As a workaround...
Replace FORMTEXT with null "". In OP circumstance:
Dim FileName As String
FileName = ActiveDocument.Bookmarks("mmn").Range.Text
FileName = Replace(FileName, "FORMTEXT ", "")
Not a "fix" for the issue or elegant but it works.
Had the same issue. Simply delete current bookmark and add a new one. If that does not work than instead of using the following:
FileName = ActiveDocument.Bookmarks("mmn").Range.Text
try using:
Selection.GoTo What:=wdGoToBookmark, Name:="mmn"
Filename = Selection.Text