[英]Excel VBA to Word Document Delete last line
The goal is to replace the last line of a document.目标是替换文档的最后一行。 The last line always starts with a $.
最后一行总是以 $ 开头。 I am using a find here but don't have to, I couldn't get any other way to function.
我在这里使用查找但不必,我无法通过任何其他方式获得 function。
I have used replace.text successfully within find but due to some conditions I can't use that without tons of if statements running different Finds.我已经在 find 中成功使用了 replace.text,但由于某些情况,如果没有大量运行不同 Find 的 if 语句,我就无法使用它。
Everything seems to be working I try except for going to the end of the page or expanding to delete/replace the entire line after the $.除了转到页面末尾或扩展以删除/替换 $ 之后的整行之外,我尝试的一切似乎都在工作。
Specifically the.Expand function doesn't work for me the 10 different ways I've tried.具体来说,.Expand function 在我尝试过的 10 种不同方法中对我不起作用。 and.EndKeys never works for me.
and.EndKeys 从不适合我。 (I have tried every combination of WordDoc.Expand WordApp.Expand WordSelection.Expand setting activedocument etc.)
(我已经尝试了 WordDoc.Expand WordApp.Expand WordSelection.Expand 设置 activedocument 等的每种组合。)
The code is a bit of a mess at this point.此时代码有点混乱。 The
For Each oRange
is the only part with issues and is my 10th iteration of trying to get it to work by now. For Each oRange
是唯一有问题的部分,也是我尝试让它工作的第 10 次迭代。
Sub OpenDoc()
Dim strFile As String
Dim strPN As String
Dim WordApp As Object, WordDoc As Object
Dim WordSelection As Object
Const wdReplaceOne = 1
Dim oRange As Object
Dim RoundPrice As Integer
Dim PriceString As String
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
For i = Selection.Rows.Count To 1 Step -1
If Cells(i, 4).Value <> "" Then
If Cells(i, 4).Value <> "PN" Then
'Print SD
If IsError(Cells(i, 28).Value) = True Then
RoundPrice = Cells(i, 12).Value
RoundPrice = RoundPrice * 0.85
PriceString = RoundPrice
PriceString = Left(PriceString, Len(PriceString) - 1)
PriceString = PriceString & "9"
strPN = Cells(i, 4).Value
strFile = "c:\Users\Robert\Desktop\Masterlist\B_" & strPN & ".docx" 'change to path of your file
If Dir(strFile) <> "" Then 'First we check if document exists at all at given location
'Word session creation
Set WordApp = CreateObject("Word.Application")
'word will be closed while running
WordApp.Visible = True
'open the .doc file
Set WordDoc = WordApp.Documents.Open(strFile)
'WordDoc.PrintOut
Set WordSelection = WordApp.Selection
For Each oRange In WordDoc.StoryRanges
With oRange.Find
.Forward = False
.Text = "$"
.Execute Find
End With
With WordSelection
.Expand Unit:=wdLine
.Text = "$" & PriceString
End With
Next oRange
WordDoc.SaveAs ("c:\Users\Robert\Desktop\B" & "_" & strPN & ".docx")
WordApp.Quit
End If
End If
End If
End If
Next i
End Sub
Your code isn't working because WordSelection
doesn't get moved after the Set
statement.您的代码不起作用,因为
WordSelection
在Set
语句之后没有移动。 You can accomplish what you need just using the Range.您只需使用 Range 即可完成所需的工作。 You should also check that Find actually found something.
您还应该检查 Find 是否确实找到了一些东西。
For Each oRange In WordDoc.StoryRanges
With oRange
With .Find
.Forward = False
.Text = "$"
End With
If .Find.Execute Then
.Expand Unit:=wdParagraph
.Text = "$" & PriceString
End If
End With
Next oRange
You should also get rid of the late binding as you gain nothing at all by using it.您还应该摆脱后期绑定,因为使用它一无所获。 Instead set a reference to the Word object library and declare the objects properly.
而是设置对 Word object 库的引用并正确声明对象。 That way you will also have the benefit of Intellisense.
这样,您还将受益于 Intellisense。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.