I have a table of products where there is say a pdf for a specific products user manual. I'm storing the model name and it's file path in my products table (in Access). I've created a form in Access that allows the user to search by product name and it narrows down the number of files and shows the results from the search in a list box. However my biggest problem is opening the actual PDF. It opens the file, but I have to store the file path exactly how it is and the path of the files are long. Is there a way to open the PDF hyperlinks without using the Followhyperlink command? Or is there a way that I can show only the file name of the pdf in my list box rather than the entire path name? If I change the display text in my products table it doesn't open the hyperlink, I get an error. Any help would be greatly appreciated!
Application.FollowHyperLink() has problems with security, especially when opening files on a network drive. See e.g. here: http://blogannath.blogspot.de/2011/04/microsoft-access-tips-tricks-opening.html
A better method is the ShellExecute() API function.
Essentially it looks like this (trimmed from http://access.mvps.org/access/api/api0018.htm ):
' This code was originally written by Dev Ashish.
' http://access.mvps.org/access/api/api0018.htm
Private Declare Function apiShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" _
(ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) _
As Long
Public Const WIN_NORMAL = 1 'Open Normal
Private Const ERROR_SUCCESS = 32&
Public Function fHandleFile(stFile As String) As Boolean
Dim lRet As Long
lRet = apiShellExecute(hWndAccessApp(), "Open", stFile, vbNullString, vbNullString, WIN_NORMAL)
If lRet > ERROR_SUCCESS Then
' OK
fHandleFile = True
Else
Select Case lRet
' Handle various errors
End Select
fHandleFile = False
End If
End Function
Now for your listbox:
Set it to 2 columns, the first being the model name, the second the file path.
Set the column width of the second column to 0, so it will be invisible.
And in the doubleclick event, call fHandleFile with the second column (file path):
Private Sub lstManuals_DblClick(Cancel As Integer)
Call fHandleFile(Me.lstManuals.Column(1))
End Sub
Related
I have a string containing unicode characters in VBA.
I want to display that string in a message box containing it.
However, instead of the string, the message box only contains a questionmark.
MCVE:
Dim s As String
s = ChrW(5123)
MsgBox s
MsgBox is not compatible with non-ANSI unicode characters.
We can display message boxes with the WinAPI MessageBoxW function, however, and that is .
Let's declare that function, and then create a wrapper for it that's nearly identical to the VBA MsgBox function:
Private Declare PtrSafe Function MessageBoxW Lib "User32" (ByVal hWnd As LongPtr, ByVal lpText As LongPtr, ByVal lpCaption As LongPtr, ByVal uType As Long) As Long
Public Function MsgBoxW(Prompt As String, Optional Buttons As VbMsgBoxStyle = vbOKOnly, Optional Title As String = "Microsoft Access") As VbMsgBoxResult
MsgBoxW = MessageBoxW(Application.hWndAccessApp, StrPtr(Prompt), StrPtr(Title), Buttons)
End Function
This function is only compatible with Microsoft Access. However, for Excel you can swap Application.hWndAccessApp with Application.hWnd to make it work. For other VBA compatible applications, you'll have to find the appropriate way to get the hWnd.
You can use it like MsgBox, as long as you don't use the context-dependent help functionality:
Dim s As String
s = ChrW(5123)
MsgBoxW s
An alternative could be my ModernBox:
MsgMox ChrW(5125) & ChrW(5123) & ChrW(5121) & ChrW(5130), vbInformation, "Unicode"
Display:
I am using vba code to open an access database from another access database using the following codes
Public Declare Function ShellExecuteA Lib "shell32.dll" _
(ByVal hWnd As Long, ByVal strOperation As String, _
ByVal strFile As String, ByVal strParameters As String, ByVal strDirectory As _
String, ByVal nShowCmd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Dim lngreturn As Long
lngreturn = ShellExecuteA(GetDesktopWindow(), "OPEN", strFilePath, "", "", vbNormalFocus)
It works fine except that it keeps opening new instances of the database even if there is one already open in the background. I need codes to bring the opened database to the foreground. NB:- I have used AppActivate and it does not work because I am already in msaccess and the other database that I want to bring to the foreground is also in access.
Set an Application title for the second database in File - Options - Current database.
Then use that title with AppActivate.
Alternatively, instead of using ShellExecuteA, build a full command line (including the path to msaccess.exe), and use the Shell() function to start the second database:
Runs an executable program and returns a Variant (Double) representing the program's task ID if successful
Store this task ID in a public or static variable, and use it with AppActivate.
I insert textbox to my Excel 2013 document and put some text
正体字/繁体字
with UTF-8 symbols
In textbox its looks ok but when I trying to msgbox it with command
MsgBox ActiveSheet.Shapes("Textbox 1").TextFrame.Characters.Text
I get something like
???/???
So how to set UTF_8 charset to get normally this text in msgbox or into variable?
You can create something that looks like a MsgBox and functions like a MsgBox, but can better handle UniCode:
Public Declare Function MessageBoxU Lib "user32" Alias "MessageBoxW" _
(ByVal hwnd As Long, _
ByVal lpText As Long, _
ByVal lpCaption As Long, _
ByVal wType As Long) As Long
Sub MsgBoxSubstitute()
Dim s As String
s = ChrW(8451)
MessageBoxU 0, StrPtr(s), StrPtr("MsgBox Substitute"), 0
End Sub
Using the Windows API. Note it has a nice built-in mechanism to dismiss the message.
The only workaround is to create a form and display your message in a label. I think labels are UTF-8 enabled.
I want to be able to download images by using multiple image urls on vba. This is the code I have right now, and everytime I try download, it fails (It reads "Files not Found!"). Any help will be appreciated!!!
Option Explicit
Private Declare Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, _
ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Private Sub Image()
Dim i As Long
Dim url As String
Dim done As Long
Worksheets("Sheet1").Activate
For i = 1 To 4
url = Sheet1.Range("A" & i).Text
done = URLDownloadToFile(0, url, "C:\Users\Public\Pictures\", 0, 0)
Next
'Test.
If done = 0 Then
MsgBox "Files have been downloaded!"
Else
MsgBox "Files not Found!"
End If
End Sub
I have got this code to work by doing two simple things:
Make sure you have http:// included in your urls, without the http URLDownloadToFile fails to download the file.
The usage of the URLDownloadToFile function is expecting a filename rather than a path name for the downloaded file. A quick and dirty fix is to do the following:
done = URLDownloadToFile(0, url, "C:\Users\Public\Pictures\pic" & i & ".gif", 0, 0)
which assigns a name to each file using i.
A better solution would parse the filename from the url and save using this filename.
Other than that the code worked fine for me.
In an already open word document select all text
copy selected text to clipboard
check default browser open at correct web address
if not open default browser at web address "http://thisaddress.com"
give focus to browser
paste clipboard text into input box called "input1"
or some other way to get MSword document contents to a web page input?
Currently the workflow involves a secretary logging in to the website, then filling out a web form, switching to their open MS Word document, selecting all, copying the WP document, then back to the web form and pasting into an input box, then hitting submit. What I want to do ideally have a button in MS word which opens the browser to the correct web page then copies and pastes the document into the correct input box on the page (in fact it will be the only textarea form field).
The MS Word VBA code is:
Option Explicit
Enum W32_Window_State
Show_Normal = 1
Show_Minimized = 2
Show_Maximized = 3
Show_Min_No_Active = 7
Show_Default = 10
End Enum
Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" (ByVal hWnd As Long, _
ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Function OpenURL(URL As String, WindowState As W32_Window_State) As Boolean
' Opens passed URL with default application, or Error Code (<32) upon error
Dim lngHWnd As Long
Dim lngReturn As Long
lngReturn = ShellExecute(lngHWnd, "open", URL, vbNullString, _
vbNullString, WindowState)
OpenURL = (lngReturn > 32)
End Function
Sub TestMacro()
Application.ActiveDocument.Select
Selection.Copy
OpenURL "http://localhost:8500/index.cfm?wordContent=" & Selection, W32_Window_State.Show_Maximized
End Sub
and in the coldfusion handling form
<html>
<head>
</head>
<body>
<form id="form1">
<Textarea ID="txtArea" rows=6><cfoutput>#url.wordContent#</cfoutput></textarea>
</form>
</body>
</html>
Just would like to work out how to not open a new browser window if one is already open.
In case you can modify the web-application, you may do the following:
MS-Word: Copy content to clipboard.
MS-Word: Open Url as "http://thisaddress.com/SomePage?pasteClipboard=true"
SomePage: if query-string param pasteClipboard == true, then add a javascript function to get the clipboard data into your form field.
Update:
In your macro you simply call Selection.Copy, and to open the URL using default browser check this link http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_23225744.html
Using the code from the previous link, I made a test macro as :
Sub TestMacro()
Application.ActiveDocument.Select
Selection.Copy
OpenURL "http://thisaddress.com/SomePage?pasteClipboard=true", W32_Window_State.Show_Maximized
End Sub
I hope this was helpful.
Update 2:
Just use W32_Window_State.Show_Default, Here is the full macro:
Option Explicit
Enum W32_Window_State
Show_Normal = 1
Show_Minimized = 2
Show_Maximized = 3
Show_Min_No_Active = 7
Show_Default = 10
End Enum
Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" (ByVal hWnd As Long, _
ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Function OpenURL(URL As String, WindowState As W32_Window_State) As Boolean
' Opens passed URL with default application, or Error Code (<32) upon error
Dim lngHWnd As Long
Dim lngReturn As Long
lngReturn = ShellExecute(lngHWnd, "open", URL, vbNullString, _
vbNullString, WindowState)
OpenURL = (lngReturn > 32)
End Function
Sub TestMacro()
Application.ActiveDocument.Select
Selection.Copy
OpenURL "http://thisaddress.com/SomePage?pasteClipboard=true", W32_Window_State.Show_Default
End Sub
Another option is to look into controlling Internet Explorer from inside Word using a control.
Here is an example.
Note, this will only work with IE (unless there are dll versions of Firefox etc.)