Copying image in Word VBA - vba

I have an image control in a Word UserForm and a button which opens a dialog box to let the user select an image. If I want to copy this image to another document, how do I do it via code?
Here's the code to select the image via the button:
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = False
.Title = "Select Photo"
.Filters.Clear
.Filters.Add "Images", "*.gif; *.jpg; *.jpeg", 1
If .Show = -1 Then
filetoinsert = .SelectedItems(1)
For Each vrtSelectedItem In .SelectedItems
Next vrtSelectedItem
Me.myImage.Picture = LoadPicture(.SelectedItems(1))
Else
End If
End With
Set fd = Nothing
End Sub
At the bottom of this form, I have another button that will copy this photo to another document. Here's what i've tried so far:
Private Sub cmdTransferPhoto_Click()
'Copy and Paste photo on "NextDoc" document. On the this document, I have a Picture Content Control called "Picture" where i plan to paste the copied image
Set Picture = NextDoc.SelectContentControlsByTitle("Picture").Item(1)
Picture.Range.InlineShapes.AddPicture _
Me.myImage.Picture, linktofile:=False, savewithdocument:=True
End Sub
When I click, the cmdTransferPhoto button, I get this error
Run-time error '5152'
This is not a valid file name.
Try one or more of the following:
* Check the path to make sure it was type correctly.
*Select a file from the list of files and folders.
Ideas are welcome! Thanks.

As you could have discovered from the documentation, and should have noticed from the Intellisense, .InlineShapes.AddPicture takes FileName, datatype string, as the required argument. As you are attempting to pass an image (which will be a rather poor quality bitmap) into this argument it quite naturally fails.
You already obtained the filename which you used to load the picture into the image control, so why not use it?
Picture.Range.InlineShapes.AddPicture FileName:=filetoinsert, LinkToFile:=False, SaveWithDocument:=True

Related

Save Picture in an MS Access 2016 Form Image Control to folder

This is my first time posting and before I get into my question I just want to say how much I appreciate all the time and effort everyone puts into solving these problems.
I'm working on setting up a simple project management system which uses both MS Access and Excel. Through various forms in Access a user can set up a new project with name, address, logo etc. For the logo a user can double click on an image control box and choose a jpeg from any folder they choose.
My problem is with saving the logo. When the user clicks on the Save button, I would like to save the picture into a specific folder, not the one the user choose from.
This code, which works as expected, gives me the file path of where the user chose the picture from and displays the picture.
Private Sub CompLogo_DblClick(Cancel As Integer)
Dim sFile As String
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Choose Logo"
.Filters.Clear
.Filters.Add "JPEG", "*.jpg"
If .Show = -1 Then
sFile = .SelectedItems(1)
End If
End With
If sFile <> "" Then
Me.CompLogo.Picture = sFile
End If
End Sub
I know I can save the path as text in an Access table...
Sub SaveNewProject()
Set rst = CurrentDb.OpenRecordset("ProjectsTbl", dbOpenTable)
rst.AddNew
rst!Logo = LogoFilePath
rst.Update
rst.Close
Set rst = Nothing
End Sub
... but I need the actual jpeg to be saved in the specific folder so it can be used later in the Excel side of the program.
Any help you can give is much appreciated.

Access VBA - Dialog Box. Looking for an specific file

Now I have this code in order to open a dialog box to search for a concrete type of file, a .txt called memory: memory.txt
So:
Dim S As String
S = OpenCommDlg("C:\memory.txt")
If IsNull(S) Or S = "" Then Exit Sub
Unfortunately, this opens a dialog box in C:\, of course, but looking for image type archives, which is absolutely not what I'm looking for. You can see this at the right side of the attached image:
Anyone knows how to modify this code in order to find the kind of archive we are looking for, and it's name...
You can use the FileDialog method. This will save the full file path to your s string, and you can add multiple filters to filter by file type.
Dim s As String
With Application.FileDialog(msoFileDialogFilePicker)
.Filters.Clear
.Filters.Add "Text Files", "*.txt"
.Filters.Add "All Files", "*.*"
If .Show Then s = .SelectedItems(1)
End With
Debug.Print s
If you want to automatically fill in the inputbox with your filename, then you can just add this line to the with statement:
.InitialFileName = "C:\memory.txt"

Excel VBA How to prevent user from hitting cancel in msoFileDialogSaveAs

I am very new to VBA and I am creating a template for my boss. I want to force users to "save as" so that they don't overwrite the template. In other words, I'd like to disable the cancel button when the save as dialog box pops up.
Here is my code:
Dim fPth As Object
Set fPth = Application.FileDialog(msoFileDialogSaveAs)
With fPth
.InitialFileName = CTAPath
.InitialFileName = CTAName & "_CAP DATA"
.Title = "Save with your CTA file"
.InitialView = msoFileDialogViewList
.FilterIndex = 2
If .Show = -1 Then
.Execute
End If
End With
I'm thinking I should create an ELSE statement within the IF statement but I can't figure out what it should be. I've tried searching and I'm not coming up with any solutions.
Thank you!
That's not the way to do this: if the users choose the file itself in the "Save as" filelist, you might end up in the same situation.
I'd advise you to make the file read-only, so nobody can change or overwrite it.
I'm not sure you're able to disable Cancel button, but there is workaround...
You can loop .Show method till user hits Save button ;)
For example:
Sub PreventCancel()
Dim fPth As Object
Set fPth = Application.FileDialog(msoFileDialogSaveAs)
Dim result As Variant
With fPth
.InitialFileName = CTAPath
.InitialFileName = CTAName & "_CAP DATA"
.Title = "Save with your CTA file"
.InitialView = msoFileDialogViewList
.FilterIndex = 2
Do
result = .Show
Loop While result <> True
.Execute
End With
End Sub
[EDIT]
I'd suggest to use Application.GetSaveAsFilename Method (Excel) instead of FileDialog, because it gives you more control over it.
Please, read valuable comments to your question also.

VBA to Star Basic (OpenOffice), struggling

I have this task to convert recenntly written VBA code to OpenOffice version. I tried to launch it from OpenOffice, but it doesn't work (mostly "unsatisfied query..." error. I am now stuck on Open File Dialog, I can either use VBA compatible open file dialog as mine looks now like that (giving error):
FileToOpen = Application.GetOpenFilename("Please choose a file to import", "Excel Files *.dbf (*.dbf)")
I can also use OpenOffice file dialog box, but couldn't find any information on this.
Thanks in advance
I'm confused on what you're asking, but if you're having trouble creating a file dialog box, this is VBA code that will do it for you. I think this is what you're asking, but I could be wrong.
Private Sub cmdFileDialog_Click()
' This requires a reference to the Microsoft Office 11.0 Object Library.
Dim fDialog As Office.FileDialog
Dim varFile As Variant
' Clear the list box contents.
Me.FileList.Value = ""
' Set up the File dialog box.
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
' Change allowmultiselect to true if you want them to be able to select multiple files
.AllowMultiSelect = False
' Set the title of the dialog box.
.Title = "Select One or More Files"
' Clear out the current filters, and then add your own.
.Filters.Clear
.Filters.Add "All Files", "*.*"
' Show the dialog box. If the .Show method returns True, the
' user picked at least one file. If the .Show method returns
' False, the user clicked Cancel.
If .Show = True Then
' Loop through each file that is selected and then add it to the list box.
For Each varFile In .SelectedItems
Me.FileList.Value = varFile
Next
Else
MsgBox "You clicked Cancel in the file dialog box."
End If
End With
End Sub

VBA MS Access 2007 hyperlink insert button

I have a button which inserts a hyperlink into a new record. The field's IsHyperlink property is set to "yes", so I get the hand, but clicking on the inserted path does not go anywhere. I believe the button is updating the record with the path of the file as "text to display" rather than "address."
Private Sub MSDS_btn_Click()
Dim fd As Office.FileDialog
'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'Use a With...End With block to reference the FileDialog object.
With fd
'Set the initial path to the D:\Documents\ folder.
.InitialFileName = "D:\Documents\"
.Title = "Select MSDS"
'Use the Show method to display the File Picker dialog box and return the user's action.
'If the user presses the action button...
If .Show = -1 Then
DoCmd.GoToRecord , "", acNewRec
Me![Link MSDS] = .SelectedItems(1)
**
'If the user presses Cancel...
Else
End If
End With
'Set the object variable to Nothing.
Set fd = Nothing
End Sub
I know that putting the following code in at the ** works in Excel, I am after something like it which will work in Access!
ActiveSheet.Hyperlinks.Add Anchor:=Cells(ActiveCell.row, Range("LinkCol").Column), Address:=.SelectedItems(1), TextToDisplay:="MSDS"
Try this if you want the file path as both the hyperlink address and display text.
Me![Link MSDS] = "#" & .SelectedItems(1) & "#"
If you want the address with only the file name (without the path) as the display text, try this:
Me![Link MSDS] = Dir(.SelectedItems(1)) & "#" & .SelectedItems(1) & "#"
See HyperlinkPart Method for more background information. You might even prefer to manipulate your hyperlink field data using HyperlinkPart.