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

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.

Related

PowerPoint VBA - Search an highlight Text

I want code a search function via VBA that searches for a specific text and highlights the first result in PowerPoint (like the normal search window, that can be opened with Ctrl + F).
I want to code this in VBA, because I want to search for multiple texts (I have a sub, where I deliver the search text as parameter) and do not want to have to type the search texts manually in the search window.
Having a sub with a parameter is not the problem.
The problem is highlighting the first result.
I found this code here (the first answer in this topic):
powerpoint search box vba
The only thing that I need is to switch to the slide and highlight the text.

PowerPoint: Repeat text on every slide

I'm working on creating a PowerPoint template for daily class presentations. In the template I'd like to have a hunk of text that is prominently displayed on the first slide and which repeats at the bottom of the subsequent slides at the bottom in a smaller size. The text will change every day.
The ideas I've had so far:
Use a text field. As far as I can tell, PowerPoint doesn't have anything like a text field that can be dynamically set.
Use a footer - this works and I can modify the master to get the look I want, but I'd really like to be picking up the value of the text from the first page so that edits would be automatically applied and to save the initial step of setting the footer.
Using VBA - I'd be willing to give this a shot, but I've never used VBA and the learning curve seems steep so it would be nice to know if the idea is feasible in VBA.
Can this be done? How would you approach it?
Here's an example of what I'm hoping to be able to do. Ideally the solution would work on both the Mac (2013) and Windows (2016) version of PowerPoint.
You can connect your presentation with an excel file. And running the code in the ppt would pull out the text in the excel file and update the titles instantly.
Create a presentation with a textbox with some temporary text. Put the below code in ppt. save as pptm.
Sub AddMotionPath()
Dim Temp As String
Excel.Application.Workbooks.Open ("D:\Users\Desktop\Book1.xlsx") ' update the path of the excel file
Workbooks("Book1.xlsx").Activate 'activate the file
For p = 1 To 4
Temp = Workbooks("Book1.xlsx").Worksheets("Sheet1").Range("B" & p + 1).Value ' Column B has the titles
ActivePresentation.Slides(p).Shapes(1).TextFrame.TextRange.Text = Temp ' this updates the titles from excel to ppt slide
Next
Excel.Application.Workbooks("Book1.xlsx").Close False 'closes the excel file
End Sub
Let me know if this works for you. You can update the excel file and run the macro in ppt. The text in the slides will be updated automatically.

Extract data from content controls in Word to Excel

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.

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

Word VBA Get Selection (Text) Background Color - But Not Highlighted or Shading

I have a Word Document that was created by saving a PDF document as a Word Document. The PDF document has some text that was highlighted in Yellow. I need to detect the highlighted text. I guess the conversion is not perfect. Some of the text in the Word Document is showing that it is highlighted, and some just has a Yellow background but is not showing as being highlighted. I can select some of the text and click on the highlight button on the menu and it shows a highlight color, and for some of the words, it shows No Color.
Also in the VBA immediate window if I type
?selection.formattedtext.highlightcolorindex, I get 7 for some selections, and 0 for others.
For those selections that do not show up as highlighted, how can I detect them?
Update: I selected a "Highlighted" word and executed call Selection.ClearFormatting and the font changed, but the yellow highlight remained. So that tells me that the yellow highlight is not a format. I also selected No Color with the text selected and the yellow highlight remained. I have verified that it is neither Highlight or Shading. Hmmm... What could be causing the highlight?
Update: I discovered that if I delete the "highlighted text" that is not really highlighted, the text that is to the right of the deleted text moves over to the left and the highlight remains. So then I discovered that I can Select the highlight if there is no text under a portion of the highlight. Then I can drag the highlight to another portion of the page. So, this highlight is an object. If I right click on it, I have options such as Format Object.
So now I know that these "Highlights" are actually like objects. If there is no text under the object, I can grab it and drag it around. How can I, using VBA, find these types of objects?
What I really want is the entire paragraph under the highlight objects. Is that possible?
Well, I figured out how to do it. I used another StackOverflow question to lead me to the answer How do you select an image that was just pasted to MS Word via VBA
I am running this code from Microsoft Access on a Microsoft Word object.
Public Sub ReadWord2(strPath As String)
'Const wdColorRed = 255
Dim objWord As Object
Dim objDoc As Word.Document
Dim strParagraph As String
Dim intShapeCount As Integer
Dim Shape As Word.Shape
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Open(strPath)
Set objSelection = objWord.Selection
intShapeCount = objDoc.Shapes.Count
'objDoc.Shapes.SelectAll
For Each Shape In objDoc.Shapes
'If Shape.AutoShapeType = 1 Then
Shape.Select
'Debug.Print Shape.Fill.BackColor
'Debug.Print Shape.AutoShapeType, Shape.Name
'Debug.Print Shape.TextFrame.TextRange.Text
objSelection.Expand wdParagraph
Debug.Print objSelection.Text, Shape.Fill.BackColor
'End If
Next
End Sub
There is actually more code in the sub, but I deleted the parts that are not relevant. Hopefully this will help someone else who runs into the same problem. The objSelection.Text will contain the paragraph text that the shape is part of. Now I just need to write code to handle the text and make sure that it is text that I want.