how to change a text in visio document (in one specific sheet) according to file name of that document - vba

i have 200 files and i want to change a text box contains revision no in that text box according to the revision number written in my file name.
i have tried it through macro (VBA code) and i need a vba cod for this purpose with a dedicated short key for this purpose.

Related

How to pull text from a shape in a Word document using VBA?

I'm trying to grab the text from inside a shape on a Word document.
Sub textgrab()
MsgBox ActiveDocument.Shapes("Rectangle 85").TextFrame.TextRange.Text
End Sub
I get the error:
Run-time error '-2147024809 (80070057)':
The item with the specified name wasn't found.
In the Word document when I go to the top menu, hit the shape format tab, and in the arrange section, I select 'selection pane', I get a list of all the shapes, 'Rectangle 85' is there.
When I select it, it highlights the box i'm trying to grab the value from.
This is a pdf that I've opened in Word. I'm trying to automate a process that will open a pdf invoice, grab the dollar total, and pull it into Excel.
Solution for those that stumble upon this later. I used the following:
ActiveDocument.ActiveWindow.Panes(1).Pages(1).Rectangles.Item(i).Range
Word can only extract text from Drawing objects. These are inserted in the UI, for example, from Insert/Shapes. Shape.TextFrame.TextRange has no OCR capabilities, so can't be used to get text "embedded" in other kinds of graphic objects, such as an embedded PDF file or a JPG or anything similar.
When uncertain whether a particular Shape supports reading or writing text, right-click it in the UI and see if the menu selection Add Text or Edit Text is available.

Change Path to Picture Links in Excel

I have manually pasted a large number of linked pictures into a 2010 Excel spreadsheet using insert picture -> select picture location --> link to file. These pictures are part of a report. I update the pictures using R each quarter, and my report automatically updates. Perfect.
I now need to change the directory where the plots are kept, and need to update the links. As there are around 200 of them (its a big report), I want to do this in VBA. Whilst I can loop through the pictures ok (ActiveSheet.Pictures), I can't seem to find the links/address. Any idea how I can see the underlying file location so I might change it - the reference has to be stored somewhere (note - these don't seem to be stored as hyperlinks).
Any idea how I can see the underlying file location so I might change it - the reference has to be stored somewhere
Create a new folder
Paste a copy of the .xlsx or .xlsm excel file
Uncompress the file with a zip tool (i'm using 7-Zip)
Delete the .xlsx or .xlsm file (optional)
Now we have all the component parts of the original file as plain text xml files and folders
Inside the folder xl\drawings\ _rels there are files named as drawing2.xml.rels, drawing3.xml.rels, ...
It seems that each file corresponds to a sheet and stores the paths to images in this format:
Target="file:///C:\Users\myusername\Documents\MyImageFolder\My%20Image%20Name.png"
Change the paths with a text editor
Compress all the contents of the folder to a .zip
Change the extension to the original .xlsx or .xlsm
These steps could be automated with VBA, AutoIt, etc., here some references:
An example with AutoIt and 7-zip
http://www.jkp-ads.com/Articles/Excel2007FileFormat.asp
http://www.jkp-ads.com/Articles/Excel2007FileFormat02.asp
Ron de Bruin zip examples with VBA
Read and change multiple XML files in Excel (2007) VBA
Excel uses the Formula Bar as the link in this case, just the same as it would link between ranges in two different worksheets. When I select a linked picture, the formula below populates in the formula bar:
=[TrialWB.xlsm]Sheet1!$C$3:$E$6
You can access the Shape's formula using the code below and inserting your picture's specific name:
ActiveSheet.Pictures("Picture Name").Formula = "=[TrialWB.xlsm]Sheet1!$C$4:$E$6"
In updating the links, you'll have to change the file path in the formula. This might look like:
ActiveSheet.Pictures("Picture Name").Formula = "='C:\Reports2015\[TrialWB.xlsm]Sheet1'!$C$4:$E$6"
changing to
ActiveSheet.Pictures("Picture Name").Formula = "='C:\Reports2016\[TrialWB.xlsm]Sheet1'!$C$4:$E$6"
This question may be of some further assistance for accessing formulas:
Excel: create image from cell range
And here's a useful Microsoft page for formula file path editing: https://support.office.com/en-us/article/Create-an-external-reference-link-to-a-cell-range-in-another-workbook-c98d1803-dd75-4668-ac6a-d7cca2a9b95f

Microsoft Word MacroButton - placeholder text visibility

I have a Microsoft Office 2013 Word template, in which I have some text-field elements, created by using Quick Parts -> Field -> MACROBUTTON noname [Type your text here].
If I fill only some of these fields (i.e. "[Name]", "[Address]") and I print or save as PDF, all the fields that I have not filled will display as [Insert your text here] in the printed paper or PDF. To be clear, the placeholder text must be manually removed (or replaced with the text you want).
I've readed somewhere, that you can create a macro, which will not display the placeholder text in the PFD- or printed version of the document, if there is no text written manually to that specific field (you leave it as it was). As this would be handy in cases, where you don't fill all the neccessery fields, my question is:
Q: Can this be achieved only by using Macro Button, and if not, what is needed to create text fields as described below that are not included in the printed or PDF saved version of the document?
This cannot be achieved without using actual macro code. Right now your solution contains no macro code, the fields simply function as "targets" and when the user types on the field it is deleted. Where the user does not type, the prompt remains. You'd need code to delete these fields from the document.
Given your requirement, the code would have to fire in the DocumentBeforeSave and the DocumentBeforePrint events. These events require a class and supporting code in a standard module. The basic information on how to set these up is in the Word object model language reference: https://msdn.microsoft.com/en-us/library/office/ff821218.aspx
An alternative to MacroButton fields would be to use ContentControls. But here, again, code and the same events would be required to remove/hide placeholder text.

Text from excel cells to individual word files

I have an Excel file that looks like this:
A B
1 Title_1 Description_1
2 Title_2 Description_2
Is it possible to output each title and description to individual Word files with column A for file name of the Word file and column B to its content?
For example the macro would create a new Word file give it file name 'Title_1' and copy 'Description_1'. Then create a new Word file again give it file name 'Title_2' and copy 'Description_2'. And do this until all data from the Excel file is copied.
Thank you for your time. :)
What you want to look into is called a "Mail Merge". Use a search engine and look for "mail merge excel word 2007" (or whatever year of MS Office that you are working with). You can create an excel macro that creates instances of word, populates the word doc with whatever you want from your excel spreadsheet, save the word doc with any name you want, export to PDF, etc.
Here are some hints to start you off:
in the VBE go to tools references and add one for Microsoft Office.
Use these two lines of code to create an object:
Dim MyWord
Set MyWord = CreateObject("Word.Application")
Now you can precede any code for word with MyWord for example to add a Document in word would be Documents.Add so remotely from Excel it will be MyWord.Documents.Add
You will want to loop through your cells in excel. On each iteration of the loop populate two variables, one with the text you want in the file and one with the file name.
Then simply use MyWord to enter the results of the variable then save the file using the result of the other variable.
Post back if you get stuck but give it a go first.
This is easy form any operating system, assuming you are using windows you will use a batch script(easiest).
First use save as/export on the data ribbon to save as a CSV.
Second you will need a batch script to read the CSV and use the first value as the file name the second value as the contents. If you want it to be a doc or docx instead of txt just rename the file in the same loop or add it to the file name when you output to text.
Here is a similar post with all the resources you would need to slap together a quick batch script.
read csv file through windows batch file and create txt file

In batch file, how to get focus on a specific window (opened file)

A very simple question I suppose, but I reached a deadlock with this:
I have to use a .bat file to imput plain text data into the right cells an excel sheet with lots of graphics content, vba parts, macro that deactivate "normal" EXCEL buttons and functions, password to protect the pre-typed functions, sudden and unexpected changes in the version of the "taget file", and many other complications...
My need is to be absolutely sure that the .bat is sending the sequence into the right version of the .xlsm file.
To do that I want to store the last known filename (that include the file version) in the .bat file, and I want to take focus on the excel window in wich the data have to be written ONLY IF the title of the excel window is exactly the same.
So Here is the question: How to get the focus on a specific open file from a .bat file?
You can't. If you wanted to use vbscript or jscript you could do what you want in a command prompt in an unreliable way (but it will work most circumstances).
Excel has it's own forms.
Put column headers in a row. Put selection in same row. Alt + D, O.
Plus you can make Excel only allow entries on some cells, like a invoice form.
Right click cell, Properties, Protection, Unlock. Then Alt + T, P, P.
Word has it's own forms similar to Excel (Word is also a spreadsheet).
Excel VBA language (and VBScript too) has input box command.
Sub main()
Sheet1.Range("A1").Value2 = InputBox("enter Value")
End Sub