tags:

views:

157

answers:

3

Is it possible to do this via a macro?

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 copys, and pastes the document into the correct input box on the page (in fact it will be the only textarea form field).

Nearly there. The ms word VBA macro 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 now

+3  A: 

I'm assuming you don't have control of the webpage (if you do, there's a different answer to this, let us know.)

If you use a browser like Firefox of Chrome, you can use Greasemonkey to build a script to automate the browser, including access to the clipboard.

Update

The alternative is to do the entire operation within Word. VBA has a toolkit call the Webservices Reference Tool, there are alternatives to this too, e.g. in the form of PocketHTTP. Both of these approaches cut out the browser by effectively using Word as your http client. There are probably other similar tools / libraries available (I expect a custom HTTP/POST client written as a .Net DLL would be reasonably trivial)

Here's an example of the possible workflow.

  1. Create a VBA macro
    • takes the current selected text and stores it in a variable.
    • presents the user with a set of select boxes that provide the same options as the web form.
    • Let's the user confirm when finished.
    • on confirm, POST the variables to referralAdd2.cfm via PocketHTTP or Webservices Reference Tool.

Requirements? Make sure that PocketHTTP is installed on the user machine, or that Webservices Reference Tool is already available.

slomojo
Added description of current manual workflow
Saul
" you could take a look at the web-form in use and see if you can do a POST directly from Word to the webpage's form service." How would I do this slomojo?
Saul
What is the website address, without seeing the source for this form it's impossible to say what you can do.
slomojo
Its on a private network but added the form details, the Word document contents need to go into the "message" text area. The web page is on my server, so the form code can be amended.
Saul
Hi Saul, for this to be cross browser is impossible. However, you can avoid the browser altogether and have Word talk to the ColdFusion service `referralAdd2.cfm` directly. If you'd like me to explain how to do that, let me know.
slomojo
Yes please, that may spark some ideas my side
Saul
added some info.
slomojo
A: 

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.)

Ben
Thats a good link Ben, but it needs to be cross browser.
Saul
+1  A: 

In case you can modify the web-application, you may do the following:

  1. MS-Word: Copy content to clipboard.
  2. MS-Word: Open Url as "http://thisaddress.com/SomePage?pasteClipboard=true"
  3. 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
MK
Yes the web app is under my control. Any further clues to 1 and 2 please?
Saul
Very nearly there, and thanks for your help. This opens a new browser window each time. Is there a way of modifying this so that a new browser is opened only if the browser is not already open.
Saul