vba Word-。始终在选择范围之外找到匹配项,从而无限地循环

发布于 2025-02-10 02:50:51 字数 3342 浏览 1 评论 0原文

当将表从pdf转换为单词时,我最终得到了类似于以下格式:

([space]是一个空格字符)

text [space.spac.spacing 10pts] text [space.space.spacing 30pts]文本

文本[space.space.spacing 14pts ] text [space.pacing 31分]文本

space.spacing 33pts]文本

不是带有3列和3行包含每个«文本'的

space.spacing 12ts ]文本[[
常规
文本PDF转换中的文本文本

不用创建列,而是通过调整[空格]来模仿列。

因此,我的想法是,应该通过识别转换后表的每个段落的每个空间的间距来重新创建表格,最终用可识别的符号代替它们,以便我可以在稍后将文本转换为表格。

我的想法在某种程度上是以下内容:

 ' For each paragraph of the selected text (which is the converted table)
    ' Find all [space] within the paragraph range
        ' If a [space] is found, check its spacing
            ' 1st case : [space].spacing is <= 1 pts (so a normal space)
                ' Do nothing
            ' 2nd case : [space].spacing is >= 10 pts (so previous Text is supposed to be within a small column) 
                ' insert ££ (symbol for small column)
            ' 3rd case [space].spacing is >= 30 pts (so previous Text is supposed to be within a small column) 
                ' insert §§ (symbol for large column)
 ' Once all [space] are found within the current paragraph, do the same with the next paragraph, until the last paragraph of the selected text 

我当前的代码如下:

Private Sub Test()
Dim RngSearch As Range
Dim RngCurrent As Range
Dim Paragraph As Paragraph

For Each Paragraph In ActiveDocument.Paragraphs
    Set RngCurrent = Paragraph.Range
    RngCurrent.Select 'For testing purposes
    With RngCurrent.Find
        .Text = " "
        Do While RngCurrent.Find.Execute
            RngCurrent.Select 'For testing purposes
            Select Case RngCurrent.Font.Spacing
                Case Is >= 30
                    RngCurrent.Font.Spacing = 1
                    RngCurrent.InsertAfter ("§§")
                Case Is >= 10
                    RngCurrent.Font.Spacing = 1
                    RngCurrent.InsertAfter ("¤")
                Case Else
                    ' Do Nothing
            End Select
        Loop
    End With
Next Paragraph
End Sub

因此,它有点有一个问题:它无限地循环。每次完成文本时,它都会无限期地返回。

我设法将问题跟踪到以下代码:

    With RngCurrent.Find
        .Text = " "
        Do While RngCurrent.Find.Execute
            RngCurrent.Select
            ' Use Case function
        Loop
    End With

没有它,通过段落的循环正常起作用(在最后一段结束),

    For Each Paragraph In ActiveDocument.Paragraphs
        Set RngCurrent = Paragraph.Range
        RngCurrent.Select
        ' Code here
    Next Paragraph

但是一旦.find.text(“”)注入了,它实际上是不再像我想象的那样在rngcurrent.find.execute应该建立的那样不再查看。

我觉得解决方案是非常愚蠢的,但是我一直在寻找两天的原因。每次我使用.find(“”)时,每次都会停止按照我的浮标行动。

我已经尝试使用.wrap = wdfindstop,但是它在段落的第一场比赛中停止,并过早地转到下一段。

    With RngCurrent.Find
        .Text = " "
        .wrap = wdFindStop
        Do While RngCurrent.Find.Execute
            RngCurrent.Select
            ' Use Case function
        Loop
    End With

奇怪的是.wrap = wdfindask什么都不问我...也许这意味着什么。

我相信这是因为每个段落中总是有空间?这样它可以无限期地循环吗?

When converting a table from PDF to word, I ended up with a format similar to the following:

([space] is a space character)

Text [space.spacing 10pts] Text [space.spacing 30pts] Text

Text [space.spacing 14pts] Text [space.spacing 31pts] Text

Text [space.spacing 12pts] Text [space.spacing 33pts] Text

Instead of a regular table with 3 columns and 3 rows containing each « Text » such as below

TextTextText
TextTextText
TextTextText

In other words, instead of creating a column, the PDF conversion has created a regular paragraph, mimicking columns by adjusting [spaces].spacing according to the length of the text within the column.

So my inital thought was that it should be possible to recreate a table by identifing the spacing of each space for each paragraph of the converted table, eventually replacing them with identifiable symbols so I can convert the text into a table later on.

My idea was somewhat the following :

 ' For each paragraph of the selected text (which is the converted table)
    ' Find all [space] within the paragraph range
        ' If a [space] is found, check its spacing
            ' 1st case : [space].spacing is <= 1 pts (so a normal space)
                ' Do nothing
            ' 2nd case : [space].spacing is >= 10 pts (so previous Text is supposed to be within a small column) 
                ' insert ££ (symbol for small column)
            ' 3rd case [space].spacing is >= 30 pts (so previous Text is supposed to be within a small column) 
                ' insert §§ (symbol for large column)
 ' Once all [space] are found within the current paragraph, do the same with the next paragraph, until the last paragraph of the selected text 

My current code is the following :

Private Sub Test()
Dim RngSearch As Range
Dim RngCurrent As Range
Dim Paragraph As Paragraph

For Each Paragraph In ActiveDocument.Paragraphs
    Set RngCurrent = Paragraph.Range
    RngCurrent.Select 'For testing purposes
    With RngCurrent.Find
        .Text = " "
        Do While RngCurrent.Find.Execute
            RngCurrent.Select 'For testing purposes
            Select Case RngCurrent.Font.Spacing
                Case Is >= 30
                    RngCurrent.Font.Spacing = 1
                    RngCurrent.InsertAfter ("§§")
                Case Is >= 10
                    RngCurrent.Font.Spacing = 1
                    RngCurrent.InsertAfter ("¤")
                Case Else
                    ' Do Nothing
            End Select
        Loop
    End With
Next Paragraph
End Sub

So it kinda word with one issue : it loops infinitely. Each time the text is finished, it goes back again indefinitely.

I managed to track the issue to the following code :

    With RngCurrent.Find
        .Text = " "
        Do While RngCurrent.Find.Execute
            RngCurrent.Select
            ' Use Case function
        Loop
    End With

Without it, the looping through paragraphs works normally (it ends at the last paragraph)

    For Each Paragraph In ActiveDocument.Paragraphs
        Set RngCurrent = Paragraph.Range
        RngCurrent.Select
        ' Code here
    Next Paragraph

But once .find.text (" ") is injected, it actually doesn't look within each Paragraphs.Range anymore as I supposed Do While RngCurrent.Find.Execute should have established.

I feel like the solution is something very stupid, but I've been searching for the reason why or alternatives for 2 days now. Everytime, it stops acting as per my understading when I'm using .find(" ").

I already tried using .wrap = wdFindStop, but it stops at the first match within the paragraph, and goes to the next paragraph prematurely.

    With RngCurrent.Find
        .Text = " "
        .wrap = wdFindStop
        Do While RngCurrent.Find.Execute
            RngCurrent.Select
            ' Use Case function
        Loop
    End With

Strangely .wrap = wdFindAsk doesn't ask me anything... maybe that means something.

I believe it's because there are always spaces within each paragraph ? So it can loops indefinitely?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

面犯桃花 2025-02-17 02:50:51

方式过于复杂的事情:

Sub MakeTable()
Application.ScreenUpdating = False
Dim i As Single
With Selection
  i = .Characters.First.Font.Size
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Wrap = wdFindStop
    .Text = " "
    .Replacement.Text = "^t"
    .Replacement.Font.Size = i
    .Font.Size = 10
    .Execute Replace:=wdReplaceAll
    .Font.Size = 30
    .Execute Replace:=wdReplaceAll
  End With
  .ConvertToTable Separator:=vbTab
End With
Application.ScreenUpdating = True
End Sub

You're way over-complicating things:

Sub MakeTable()
Application.ScreenUpdating = False
Dim i As Single
With Selection
  i = .Characters.First.Font.Size
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Wrap = wdFindStop
    .Text = " "
    .Replacement.Text = "^t"
    .Replacement.Font.Size = i
    .Font.Size = 10
    .Execute Replace:=wdReplaceAll
    .Font.Size = 30
    .Execute Replace:=wdReplaceAll
  End With
  .ConvertToTable Separator:=vbTab
End With
Application.ScreenUpdating = True
End Sub
天邊彩虹 2025-02-17 02:50:51

因此,我终于发现并不完全是解决方案,而是对于可能需要类似解决方案的任何人的解决方法。我决定走“硬”路径,而不是使用.find =“”,并检查段落中的每个单词(在MS Word中,似乎以[Space]字符结束)。然后,如果其间距优于值,我检查单词的最后一个字符(通常是一个空间)。情况,做点什么。

For Each RngWord In Paragraph.Range.Words
    Set RngChar = RngWord.Characters.Last
    Select Case RngChar.Font.Spacing
        Case Is > 300
            RngChar.Font.Spacing = 1
            RngChar.InsertAfter ("£")
        Case Is > 100
            RngChar.Font.Spacing = 1
            RngChar.InsertAfter ("#")
        Case Is > 15
            RngChar.Font.Spacing = 1
            RngChar.InsertAfter ("¤")
        Case Else
            ' Do Nothing
    End Select
Next RngWord

它可以完成这项工作,而且不是那么慢,但是我想有更好的解决方案:)

So I finally found not exactly a solution but a workaround for anyone who may need a similar solution. Instead of using a .find =" ", I decided to go the "hard" path and check for every word in a paragraph (which in MS Word, seems to end with a [space] character). Then, I check for the last character of a word (which is often a space) if its spacing is superior to a value. It the case, do something.

For Each RngWord In Paragraph.Range.Words
    Set RngChar = RngWord.Characters.Last
    Select Case RngChar.Font.Spacing
        Case Is > 300
            RngChar.Font.Spacing = 1
            RngChar.InsertAfter ("£")
        Case Is > 100
            RngChar.Font.Spacing = 1
            RngChar.InsertAfter ("#")
        Case Is > 15
            RngChar.Font.Spacing = 1
            RngChar.InsertAfter ("¤")
        Case Else
            ' Do Nothing
    End Select
Next RngWord

It does the job, and isn't that slow, but I guess there are better solution :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文