How to escape forward slash in Word VBA Find? - vba

I have documents containing hidden markings like these
<tag>tag content</tag>
The tags themselves use a hidden font, the content does not.
I need to find the tag content but the forward slash seems to cause issues
ActiveWindow.View.ShowHiddenText = True
Find.ClearFormatting
Find.Text = "<tag>*</tag>"
Find.Forward = True
Find.Wrap = False
Find.Format = False
Find.MatchWildcards = True
Find.IgnoreSpace = True
Find.IgnorePunct = True
Find.Font.Hidden = wdUndefined
Find.Execute
When I remove the forward slash of one of the tags (and the search string of course) it works fine.
I´ve tried Find.Text = "<tag>*<\/tag>" to escape it but that does not seem to work.
My MS Office language is set to english but the documents originate from a german version, not sure if that matters?

In Word < and > are used as wildcards for the beginning and ending of a word. That's why you have to escape them as well
\<tag\>d*\<\/tag\>

Related

Word VBA: how to obtain an exact search result with find.execute when text is formatted as hidden

I'm using Selection.Find.Execute or MyRange.Find.Execute within a text containing revision marks while having set Options.DeletedTextMark = wdDeletedTextMarkHidden. Alternatively I'm searching within a text in which certain strings are formatted with Font.Hidden = True.
If immediately before or after the search result there will be hidden text, it will be included with the search result and any subsequent operation (like Selection.Range.HighlightColorIndex = wdGreen) will include the hidden portion.
Is there an easy way of making sure that the search result corresponds exactly to the searched term?
The best work-around I have found so far is the following function:
Function ExactRange(Optional rngFoundRange) as Boolean
If IsMissing(rngFoundRange) Then Set rngFoundRange = Selection.Range
Vi_LenInitial = rngFoundRange.End - rngFoundRange.Start
rngFoundRange.SetRange _
Start:=rngFoundRange.Characters(1).End - 1, _
End:=rngFoundRange.Characters(Len(rngFoundRange)).End
Vi_LenExact = rngFoundRange.End - rngFoundRange.Start
ExactRange = IIf(Vi_LenInitial = Vi_LenExact, True, False)
End Function
The function will be True if the found range was exact, it will be False if it was not exact and needed to be adjusted. In any case, after execution the found range should exactly match the search string.

Accept Formatting Changes in Word Headers, Footers and Main Document

I'm using the following VBA code to accept all formatting changes in a document. Which lines would I need to add in order to accept the changes in the Header and Footer as well?
ActiveDocument.ShowRevisions = True
ActiveDocument.ActiveWindow.View.ShowFormatChanges = True
ActiveDocument.ActiveWindow.View.ShowComments = False
ActiveDocument.ActiveWindow.View.ShowInsertionsAndDeletions = False
ActiveDocument.ActiveWindow.View.ShowInkAnnotations = False
ActiveDocument.AcceptAllRevisionsShown
ActiveDocument.ActiveWindow.View.ShowComments = True
ActiveDocument.ActiveWindow.View.ShowInsertionsAndDeletions = True
ActiveDocument.ActiveWindow.View.ShowFormatChanges = True
ActiveDocument.ActiveWindow.View.ShowInkAnnotations = True
Option 1:
Works for all Headers and Footers and the Main Document.
ActiveDocument.AcceptAllRevisions
Option 2
This will only Accept the revisions in the current viewable seciton of the Document.:
ActiveDocument.Revisions.AcceptAll
What you can then Add before is something like:
'Opening the Header
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Just run through the various wdSeek Options to go through the various sections if you want to look at specific revisions.
You can also run a script on each revision using a Do or For Loop with:
ActiveDocument.Revisions.Item(x)

Select text w/o the paragraph mark

I have a problem when assigning text in a Word document (usually one line) to a variable. The problem is that the paragraph mark also gets assigned to the variable and that causes the external system to reject the variable.
Since it's just one line, I do:
Selection.WholeStory
var = Selection.Text
In the external system, when assigning this var to a text box, the paragraph mark gets inserted and then processed as #.
How can I assign the text below to a variable w/o the paragraph mark:
This is a sample text with paragraph mark^p
You can just remove the vbCr:
Selection.WholeStory
var = Selection.Text
var = Replace(var, vbCr, "")

How do I get a variable to work within double quotes in ASP-classic?

The following code is part of a function that is being called for each file the program finds during it's search through a directory.
set searchname = objFSO.OpenTextFile(server.mappath(filename),1, true)
do until searchname.AtEndOfStream
lineData = lcase(searchname.ReadLine())
if instr(lineData,s)>0 then
instances = instances + 1
end if
Loop
This the part of my code I'm confused with. This code worked perfectly yesterday. I made a few edits but mostly with HTML and CSS. When I pulled this code out today to optimize it better, I just keep getting a permission denied error with this line.
set searchname = objFSO.OpenTextFile(server.mappath(filename),1, true)
The problem is that the folder the code scans through contains some files that have more than one word. So it's essential that I get the "filename" variable within double quotes. I've been looking everywhere for a solution and none of them have been helpful yet. Is this even possible or is there a workaround?
Any help would be much appreciated here.
EDIT: As requested here's the code that gets the filename. Although no idea how it pertains to my question.
Function filesearch(name)
ext = instr(name,".txt")
vdirectoryvalue = instr(name,"Files\")+6
idname = mid(name,vdirectoryvalue,ext)
vdirectory = len(idname) - 4
filename = left(idname,vdirectory)
instances = 0
set searchname = objFSO.OpenTextFile(server.mappath(idname), 1, true)
do until searchname.AtEndOfStream
lineData = lcase(searchname.ReadLine())
if instr(lineData,s)>0 then
instances = instances + 1
end if
Loop
End Function

Visual Studio's VB smart indent and format document are crazy--how to fix or workaround?

I did a little searching here and I am surprised that no one has asked this question. Is Visual Studio 2010's VB smart indenting just horribly broken for multi-line statements? Or am I doing/setting something incorrectly? I have it set to 4-character indents with keep tabs. Typing a multi-line statement in VB with natural returns yields the following (property names changed to protect the innocent).
Public Sub Sub1()
Dim foo As New MyClassA With {.FileName = "test",
.Format = MyEnumForImageFormat.jpg,
.IsReallySpecial = False,
.Name = "testN",
.SourceId = Guid.NewGuid(),
.VariantName = "TestV",
.Width = 800,
.Height = 600}
End Sub
How could anyone want to format their code this way? I thought, maybe I need to reformat (Ctrl+K Ctrl+D):
Public Sub Sub1()
Dim foo As New ImageBase With {.FileName = "test",
.Format = MyEnumForImageFormat.jpg,
.IsReallySpecial = False,
.Name = "testN",
.SourceId = Guid.NewGuid(),
.VariantName = "TestV",
.Width = 800,
.Height = 600}
End Sub
For good measure, I tried Reformat again. Lo and behold, it pushed it further left. I pushed it again, and it moved some more. Finally, after four reformats, my code looks like:
Public Sub Sub1()
Dim foo As New ImageBase With {.FileName = "test",
.Format = MyEnumForImageFormat.jpg,
.IsReallySpecial = False,
.Name = "testN",
.SourceId = Guid.NewGuid(),
.VariantName = "TestV",
.Width = 800,
.Height = 600}
End Sub
Better, but we really want the continuing lines indented by only a space? And why should I have to reformat four times to get what I want? To top it off, trying to add one more property definition above does not keep anything aligned--the default indent is to cascade offset to the right by a randomly-determined number of space and tab characters.
Using old-style continuation characters doesn't help. How can I have smart indenting with any degree of sanity?
You will find the automatic indenting to be better or worse depending on exactly where you choose to split a line. In your example my style is to bring the opening brace and first item to the next line, after which the indenting will behave itself better.
Public Sub Sub1()
Dim foo As New MyClassA With
{.FileName = "test",
.Format = MyEnumForImageFormat.jpg,
.IsReallySpecial = False,
.Name = "testN",
.SourceId = Guid.NewGuid(),
.VariantName = "TestV",
.Width = 800,
.Height = 600}
End Sub
Without wanting to assume too much, I believe the indenting of each line on a multi-line statement is governed by the preceding line. With this choice of style however, you will find adding items using the natural return to position themselves correctly. When copying any pasting a snippet into a target of different indentation level this will still cause indentation issues, but since the return positions itself correctly, I haven't had too much problem with resetting each line manually (repetitions of Down, End, Delete, Enter.)
A similar solution is useful with multi-line lambda functions, however these behave better than straight multi-line statements. Correctly positioning the opening call automatically corrects the indentation of the whole routine.
This isn't so good and the indent floats:
Public Sub Sub1()
Dim a As New Action(Sub()
Dim b As Double = 4.0
Dim c As Double = 5.0
Dim d As Double = b * c
End Sub)
End Sub
This behaved nicely with the indenter:
Public Sub Sub1()
Dim a As New Action(
Sub()
Dim b As Double = 4.0
Dim c As Double = 5.0
Dim d As Double = b * c
End Sub)
End Sub
Just as a personal style I prefer these, others may get the same result with a different appearance.
I found this an annoyance aswell. It is possible to turn off Pretty Listing for Vb.Net - have a look at this question: Turn off auto formatting in Visual Studio
In my opinion it goes too far the other way, but it should stop the problem you're talking about.
Edit: you might also want to play around with the auto-indent settings; I think the default is set to 'smart'.