Hide Text in between headers MS word 2007 - vba

I have a document that is roughly 200 pages and is essentially a list of test procedures for a specific software. Now this document has certain parts to it the pertain to different versions of the software and these parts are mixed in so their not nicely formatted in a specific order. What I would like to do is Be able to hide the parts of the document that are not needed when testing a different version. I know MS word has a font option to hide text but I would like to be able to setup up a button/hypertext link/macro that will easily hide the unneeded sections. Is this possible and how would I do it? I've started experimenting with VBA script to design my own macro but have only found a way to hide one part per shortcut hit. Is there a way to do this so all parts are effected simultaneously?
EDIT:
The document is organized like this
Version 1
Test Option button
/
Version 2
Test Option button
Check that Sample button is disabled
/
Version 1
Test Save button
/
Version 3
Test Save to USB button
/
So as you can see it's completely unorganized the code I currently have for one macro really doesn't work because instead of selecting between the two point I specify it selects the whole document.
Sub TextSelectTest()
'
' TextSelectTest Macro
' Base Test
'
With Selection.Find
.Text = "Version1"
.Forward = False
.MatchWildcards = False
.Wrap = wdFindStop
.Execute
End With
Selection.Extend
With Selection.Find
.Text = "/"
.Forward = True
.Execute
.Text = ""
End With
Selection.Extend
With Selection.Font
.Hidden = True
End With
End Sub

I don't think hiding font is most professional solution as the result is visible only for printing. But that could be the easiest in this situation especially you suggested it.
First step: set sections in your documents. It's quite easy and should be done ones in Word app. You will need to insert as many section separation marks as many parts of the document you need to manage. We will need to know which section should be/shouldn't be part of each Manual but I'll back to that later.
Second steps: Than you will need the following subroutine which will 'hide' all sections and than show appropriate ones:
Sub HideUnhide_Document_Section(secIndex As Variant)
Dim Doc As Document
Set Doc = ActiveDocument
Dim secDoc As Variant
'to hide all section first, by iteration
For Each secDoc In Doc.Sections
secDoc.Range.Font.Hidden = True
Next secDoc
'alternatively we could hide whole content without iteration:
'secDoc.Content.Font.Hidden = True
'to un-hide chosen sections
For Each secDoc In secIndex
Doc.Sections(secDoc).Range.Font.Hidden = False
Next secDoc
End Sub
And to manage your hiding process I would propose the following code:
Sub Call_Hide()
Dim arrVersion1 As Variant
'put all sections for appropriate version
arrVersion1 = Array(1, 3)
'to unhide
HideUnhide_Document_Section arrVersion1
End Sub
You could either prepare similar separate subroutine for each version or parametrize that one. It that second situation it will have to have separate arrays (arrVarsionX) for each Version of your manuals.

Related

VBA to create Outline View

I use the Web Layout View with collapsible Headings.
How to expand all headings but keep normal Text collapsed ?
this VBA Code processes 1k words in 1second .
But on longer documents ( 100k words) it just freezes.
Sub outline_doc2()
ActiveDocument.ActiveWindow.View.CollapseAllHeadings
Dim para As Paragraph
For Each para In ActiveDocument.Paragraphs
' if heading is NOT normal Text , expand...
If para.OutlineLevel <> wdOutlineLevelBodyText Then
para.CollapsedState = False
'... but if next paragraph is normal text , then do not expand
If para.Next.OutlineLevel = wdOutlineLevelBodyText Then
para.CollapsedState = True
End If
End If
Next
End Sub
These changes are sensitive to the UI, so I'd recommend setting up the ScreenUpdating property while you are doing some work on the Word document:
Application.ScreenUpdating = False
' some work here
Application.ScreenUpdating = True
Currently using a workaround method :
manually indicate Headings to Expand with a "+" prefix
Letting an external Script iterate over the Headings and send Expand command it it matches the "+" Prefix
On a Document of 50k words , my updated VBA Script still produced no results after 4 minutes so I terminated the process.
The External Script had the Document outlined after 1:30 minutes.
App i use for this : strokesplus

Finding by style and selecting next table

I've been tasked with writing VBA code to automate Word. I have no idea about VBA.
What is needed:
There is a standard Word document based on a template, which has a section for writing issues. Each section has a heading, and below the heading there is a table with a couple of rows.
Sections view
The overall document layout with the different sections.
Issue view
A standard issue, with a Heading for the issue title, then a table to be filled, and then a description.
How do I write a macro that will:
Review ALL the issues in the document to see if the issue has a list of "Affected Hosts" in the table below the heading that contains more than 10 hosts
If this is not the case, ignore and move on to the next one.
If this is the case, that list should be replaced with some generic text such as "See Appendix G", and then add to that Appendix G the issue title and below it the list of all those hosts.
Where I am at:
I looked for examples of code snippets, looked at the documentation, etc. This is all I have:
Sub TidyAffectedSystems()
'
' TidyAffectedSystems Macro
'
'
' Loop over all issues by finding the appropriate style (IssueHeading)
With ActiveDocument.Range
With .Find
.ClearFormatting
.Forward = True
.Format = True
.Style = "Issue Heading" ' Heading style to find
.Wrap = wdFindStop
.Execute
End With
Do While .Find.Found
MsgBox .Text
' If it is the last one, then finish looping over
If .End = ActiveDocument.Range.End Then Exit Do
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
End Sub
This code tries to find the headings based on the style ("Issue Heading"), and then print that heading. I'm doing this to make sure that at least I am finding the right sections, but that's it.
I don't know how to for example select the table below the current found item to then see if that table has more than 10 hosts and all that, and of course no idea how to then replace, take it to an Appendix and repeat.

Justify All Text except in line broken by a line break

I'm new here so thank you in advance for your patience. Also, I'm not a native English speaker so some things might get lost in translation.
I found this wonderful vba macro to "Justify all text is Microsoft Word" [from Alvin567] and you all 1 and it works just as planned.
I would like to adapt it so that it doesn't justify paragraphs that has Shift+Enter (linebreak I think) in my document. I can't seem to find how to refer to that specific character, since it's different than "Chr(13)".
I'm usually good at adapting codes from the recording tool or find online what I'm looking for even though I never learned it through any courses, but with this one, I can't seem to figure it out on my own.
Any help would be greatly appreciated.
So here is the code :
Sub JustifyAllTheText(control As IRibbonControl) 'Don't forget to link it with RibbonX
On Error Resume Next
Dim para As Paragraph
Dim searchRange As Range
Set searchRange = Selection.Range
searchRange.End = ActiveDocument.Content.End
For Each para In searchRange.Paragraphs
If para.Range.Font.Size = 10 Then
'If para.Range.Font.ColorIndex = wdBlack Then 'I don't need it but kept it just in case
If Not para.Range.InlineShapes.Count > 0 Then
'If Not para.Range.IsEndOfRowMark = True Then 'Added line to test linebreak but doesn't work to made into text
If Not para.Range = vbLf Then
If Not para.Range.Information(wdWithInTable) Then
para.Range.ParagraphFormat.Alignment = wdAlignParagraphJustify
End If
End If
End If
End If
Next para
End Sub
Thanks!
You cannot justify lines ending in manual line breaks via any of the paragraph justification options. Instead, you need to modify a compatibility setting, thus:
Sub Demo()
Application.ScreenUpdating = False
ActiveDocument.Compatibility(wdExpandShiftReturn) = True
Application.ScreenUpdating = True
End Sub
Any lines you don't want to have justified that way will then require a tab to be inserted before the manual line break.
Moreover, you should not force the justification of paragraphs via code like:
para.Range.ParagraphFormat.Alignment = wdAlignParagraphJustify
That is liable to lead to document bloat and, potentially, corruption. Rather, you should modify the underlying paragraph Style - which also means making the change once for all affected paragraphs throughout the document.
Thank you all for your help.
Since it wasn't a good thing to justify that way, I manage to get my result with a mix of a Find and replace macro to get rid of the Shift+Enter and they I modified the Normal style to justify it. Put them both together and added a button on my RibbonX custom tab. All in all, everthing ends up as it should be and we save time with the help of a button.
Thank you!
You can check for
If InStr(para.Range.Text, vbVerticalTab) = 0 Then
If you replace your current codeline If Not para.Range = vbLf Then with this line, your macro will exlude paragraphs that have a soft return from applying wdAlignParagraphJustify
vbVerticalTab is equal to chr(11) which is in Word the "character" for Shift+Enter

Delete a variable number of lines in a Word table

BACKGROUND: My daughter works as a Special Needs instructor for a very large school district (with overwhelmed IT especially at the start of the new school year) and has an untouchable/unreachable database that creates a pdf report. She and many of her ~50 Special Needs Instructor co-workers want summary reports that hold only data applicable to their respective needs (1 page) instead of multiple pages (2-6 pages). She alone has over 75 reports of this type.
THUS FAR: On a PC running Windows 10 with Office 365, I've manually converted a pdf file to MS Word and modified a stack overflow obtained VBA macro to delete everything after "Delete Hereafter".
THE NEEDS: From a list of file names in an Office document, open the PDFs as DOCm files and save, then modify each DOCm file and save.
THE SOUGHT-AFTER MODIFICATION: Each report has two tables with text between. The second table always begins with the same header: "Second Table Title". For the second table, she wants to delete rows 2 through a row which always contains "Text Needed" less 1, a variable number of rows.
Your assistance would be appreciated not only by her, but by her co-workers as well. I am a novice user who has benefitted from Stack Overflow answers many times, but need a bit more help to solve this problem.
For example:
Sub TableCleaner()
Application.ScreenUpdating = False
Dim i As Long, r As Long
With ActiveDocument.Tables(2)
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchCase = True
.Forward = True
.Wrap = wdFindStop
.Text = "Text Needed"
.Replacement.Text = ""
.Execute
End With
If .Find.Found = True Then i = .Cells(1).RowIndex - 1
End With
For r = i To 2 Step -1
.Rows(r).Delete
Next
End With
Application.ScreenUpdating = True
End Sub

Highlighting duplicate content from user input in a Word document written in Hebrew

I am trying to highlight duplicate content based on user input, in dark red (color doesn't matter).
I ran the code in the Word document, written in Hebrew, and it appeared to fail. I ran it with English text and it worked. Unfortunately the task is for the code to function in Hebrew not English.
When the code appears, it shows '?????? ???' in the UserInput value when user inputs Hebrew text during debugging.
It seems the character type is not supported, how can I make the character type support the Hebrew text? Is it a different Unicode?
Sub HighlightDupl()
Dim UserInput As String
Dim SentArray() As String
Dim n As Long, i As Long
Application.ScreenUpdating = False
UserInput = InputBox("הדבק משפט לבדיקת כפילויות -- Paste sentence to check for duplicates", "הַצהָרָה -- Statement")
' Check if input is empty, if yes - quit program
If UserInput Is Nothing Then Exit Sub
TargetList = Array(UserInput)
For i = 0 To UBound(TargetList)
Set range = ActiveDocument.range
With range.Find
.Text = TargetList(i)
.Format = True
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Do While .Execute(Forward:=True) = True
range.HighlightColorIndex = wdDarkRed
Loop
End With
Next
End Sub
The Hebrew in the code is also not read properly so I removed it from the code I ran, I left it there now as a reference for Hebrew text.
Recently when trying to work on the program again, the code gave a few runtime errors like Type Mismatch. Was there an update in the last few weeks that suddenly made my code error?
I changed the UserInput variable to a Variant type after and there was some progress but the code didn't function as intended. The content will be mostly Hebrew text but there could be numbers as well.
Something else I've tried is to change the input language and proofing language to Hebrew. The document is mixed with both English and Hebrew. Should I have all English removed first for this to work?
At a general level, you could use a wildcard Find/Replace, where:
Find = (<*>) \1
Replace = ^&
and you set the Highlight colour to whatever you prefer. No macros needed.
The above wildcard Find/Replace will find any duplicate pair of words (Hebrew or otherwise) separated by a space, without the need to nominate the words concerned.
To be able to use Unicode (e.g. Hebrew) input via a VBA InputBox, you need to do two things:
in the Windows Control Panel, choose Region>Administrative>Change System Local> Current system locale: and set the native language you want to use. Note that this only directly affects programs that don't support Unicode. You will need to restart Windows for this change to take effect.
In VBA Editor, choose Tools>Options>Editor Format>Font and select, say, Courier New (or any other font that supports Unicode).