Extract data from content controls in Word to Excel - vba

I have a Word document that is "form-fillable", i.e. it has content control objects in it such as rich text and date picker content controls. I am looking to extract the data from specific fields into Excel. For example, every form has the project title, start date, and manager. I would like 1 row for that form with those three pieces of data. Eventually this will need to be done for a few hundred of these forms every few months, but for now I'd like to just start with one.
I envision having a button on the Excel sheet that will run VBA code to pull the data from the Word document, and populate the proper cells in the sheet. With the filepath for the Word doc being specified by the user.
I am new to VBA. How do I point my code at the right file, and then again at the specific field I need? Do I give the fields titles in the Word doc?
This is in MS Office '13

Your application is going to have a large number of specific details which are difficult to address without the specifics. To get started, here is some very simple code to get the Date from a drop-down in Excel from Word.
Note to work with Word, you need to create a reference to "Microsoft Word 15.0 Object Library" on the Excel side. Tools -> References in the VBA Editor.
When working across applications, it can help to write the VBA in Word and then move it over to Excel once you have the properties you want.
This VBA for the Excel side of the equation:
Sub GetDataFromWordFile()
Dim wrd As Word.Application
Dim file As Word.Document
Set wrd = New Word.Application
Set file = wrd.Documents.Open("test.docx")
Range("A1") = file.ContentControls(1).Range.Text
file.Close
wrd.Quit
End Sub
My Word file includes a single ContentControl. Not sure how you address the ones you want (trial and error?).
The Excel side drops the date in the right place.

Related

Event change of table in Word

Private Sub Document_Change(ByVal Target As Range)
Set table = ActiveDocument.Tables(1)
If Not Intersect(table, Target) Is Nothing Then
Target.AutoFormat ApplyColor: Red
End If
End Sub
I have the following code, but it does not seem to work in VBA Word.
Can anyone help me out?
I suppose that you copied the code from Excel VBA and tried to rebuild it a bit.
There are a few differences between Excel and Word VBA.
Intersect does not exist in Word VBA.
DocumentChange event in Word works differently from what you would expect - It occurs when a new document is created, when an existing document is opened, or when another document is made the active document. (https://msdn.microsoft.com/en-us/library/office/ff822189.aspx)
If you want to make changes in red, you may do the following:
Record the wholeDocument in a static string.
When there is a change - record again in a new string.
Compare the strings and color the differences.
However, this is a bit tough, as far as word does not have Changeevent as we are expecting it (e.g. as in Excel). Thus, you should run the VBA code a few times automatically.

How to copy textbox value in a word document into a textbox in a different word document with VBA (MS Word 2003)

Hopefully an easy question.
I have two word documents (MS Word 2003), each document has a text box object. Upon pressing a command button, I want the text that is in the text box in one document ("Reference_Text.docx") to go into the text box of another document.
Below shows the code I am trying to use in an event when the command button is pushed. I am trying to have the text box (txtLocation_Analysis) populated with the text that is in "txtLocation_Analysis_1", which is in the Reference_Text.docx file.
Dim objWord As Word.Application
Dim wd As Word.Document
Set wd = objWord.Documents.Open("C:\Users\Tim\Desktop\Reference_Text.docx")
ThisDocument.txtLocation_Analysis.Text = '(Want to call a "txtLocation_Analysis_1.text" from wd document)
Let me know if this makes sense - and if any help can be provided please :)
Hugh
Did you try
ThisDocument.txtLocation_Analysis.Text = "txtLocation_Analysis_1.text" ' A
or
ThisDocument.txtLocation_Analysis.Text = wd.txtLocation_Analysis_1.Text ' B
Sorry, from the wording it's still not clear to me but I think you want either (A) the string "txtLocation_Analysis_1.text" or (B) the contents of another textbox in the document you opened, appear in the textbox in ThisDocument.

MS Word Macro to replace all hyperlinks with a single word

I prepare a MS Word file with a large amount of news articles, each followed by a full, long clickable hyperlink (e.g. http://www.newssite.com/this-is-the-name-of-the-article.html).
I need to replace all the hyperlinks in the file with a single word: "Link", while keeping their url adresses and click functionality. I can use MS Word "change hyperlink" dialogue to do that, but doing it by hand takes incredible amount of time.
The hyperlinks, however, are not always formatted as 'hyperlink' style. Is there any condition for MS Word Macro replacement that will lookup hyperlinks by their functionality, not by their style or text?
Despite this task seems to be quite common, I could not find anything like it in the web.
Since you have the hyperlinks already created. It should just require looping through the documents hyperlinks collection and changing the 'TextToDisplay' property. This should get you started:
Public Sub ChangeHyperlinksText()
Dim hlink As Hyperlink
For Each hlink In ThisDocument.Hyperlinks
hlink.TextToDisplay = "Link"
Next hlink
End Sub

How can I add a macro to a Word 2010 or PowerPoint 2010 chart?

In PowerPoint 2010 or Word 2010, when I choose Insert -> Chart, it creates a new chart with an Excel worksheet for the data.
If I add a macro to the Excel worksheet, this seems to be discarded when I close the worksheet and re-open it.
However, it is possible to have a chart with macros, because I also have some Word documents & PowerPoint presentations that I created in Office 2003, which had embedded Excel charts with macros. When I converted those to Office 2010, they look just like "normal" Office 2010 charts, but the macros are preserved.
It looks to me like the embedded chart that gets created when you choose Insert -> Chart is in the "pptx" format rather than the "pptm" format, and so macros are not saved.
(If I query ActiveWorkbook.FileFormat, I get "51", which is "Open Xml Spreadsheet"; this is indeed "pptx", as opposed to "Open Xml Spreadsheet with Macros", which is "52").
How can I insert a chart with the "pptm" behavior? Or change the behavior of an existing chart?
NOTE: I do not want to insert a chart "object", since this means that the chart is not editable within the host application (it merely inserts an Excel chart that you need to "open" in order to edit it).
Let me give you a head start... I can achieve what I want by the following method:
Create a new document in Word (or PowerPoint).
Save the document as a 97-2003 document (which forces it into compatibility mode).
Insert a chart using Insert->Object->Microsoft Excel Chart. This embeds an old-style Excel chart.
Open the embedded chart by right-click->Open, and add a macro.
Close the chart, then choose File->Info and use the Compatibility Mode "Convert" button to convert the document into an Office 2010 document.
Now you have a "native" Word or PowerPoint 2010 chart, which you can edit directly in Word or PowerPoint. If you click on the chart and choose Chart Tools->Design->Edit Data, then the Excel worksheet that opens has the macros you created in step 4. Success!
Now, does anyone have a simpler way?
In my opinion you could achieve what you need in the following way:
(unfortunately, I can't present it with pictures and exact English commands as I'm using local-not English-version of Office)
Choose Insert >> Object >> Object...
In Object window take first page (like Create new)
Search for Microsoft Excel Chart (or something similar) and press
OK
You will get Workbook with two sheets as presented on the picture
below
(there are chart editing feature available on the Ribbon)
Press Alt+F11 while you are in Chart edition like presented in the
picture above. You will get IDE for Excel opened where you will find
appropriate workbook.
Add new module and macro there
Now you could exit chart edition in Word and save document as *.docm
After you will reopen it, go to Chart Edition (double click) >> Alt+F11 >> you will
find you macro there saved within the workbook with *docm extension.
Now that the bounty has passed (with no better answers), I'll add the partial answer from my question, in order to be able to close the question.
Create a new document in Word (or PowerPoint).
Save the document as
a 97-2003 document (which forces it into compatibility mode).
Insert
a chart using Insert->Object->Microsoft Excel Chart. This embeds an
old-style Excel chart.
Open the embedded chart by right-click->Open,
and add a macro.
Close the chart, then choose File->Info and use the
Compatibility Mode "Convert" button to convert the document into an
Office 2010 document.
Now you have a "native" Word or PowerPoint 2010 chart, which you can edit directly in Word or PowerPoint. If you click on the chart and choose Chart Tools->Design->Edit Data, then the Excel worksheet that opens has the macros you created in step 4. Success!
I'm still interested in being able to do this more directly...
The issue is with how the charts data is held within the application. Converting and originating are two different processes. Your data series is basically an array not an excel sheet. It just uses excel to display the data to you to edit. You need to keep your code in the module for powerpoint/word and access the chart via the shapes object if you do not want to embed a excel sheet.
Sub GetChartName()
Dim sSlide As Slide
Dim cChart As Chart
Dim sShape As Shape
Set sSlide = PowerPoint.ActivePresentation.Slides(1)
For Each sShape In sSlide.Shapes
If sShape.HasChart Then
Set cChart = sShape.Chart
MsgBox cChart.Name
Set cChart = Nothing
End If
Next
Set sSlide = Nothing
End Sub

format numbers in text box in ppt via vba

I have connected Excel and Powerpoint via VBA to send values from the Excel sheet to the PPT.
All is working well except one thing: I need to transfer values from cells in Excel to text box shapes in ppt while preserving the number formatting from excel. How do I do that?
I do this for about 10 such boxes and my current code using copy from excel and paste in powerpoint, keeps on giving out of range error on random places.
Will paste the code I am using in a short while.
Try using the numberFormat from Excel when you bring over the Value.
Example:
With Workbooks(1).Sheets(1).Range("A1")
valueToPaste = Format(.Value, .NumberFormat)
End With
For the sake of the example, I'm pretending you are calling this from Excel and only want to know how to extract the value with it's format. We are using the first sheet of the first workbook in Range A1. It should be easy enough to update to your specific needs.
There are probably some exceptions, particularly for custom formats, but this should work for the majority of formats you would use in Excel.