在Excel单元格中设置文档路径

时间:2017-08-17 06:37:22

标签: excel-vba vba excel

我有一个VBA代码,我在其中设置word文件的路径,以便我可以将word文档用作电子邮件正文。 但我想在代码中插入一个单元格引用,这样我就不必一直更改代码。

提前谢谢!!

以下是使用

的代码
Sub Email()
Dim oOutApp As Object
Dim oMailItem As Object
Dim oWordApp As Object
Dim oWordDoc As Object
Dim oMailWordDoc As Object
Dim sh As Worksheet
Dim cell As Range
Dim FileCell As Range
Dim rng As Range
With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
Set sh = Sheets("details")
 On Error GoTo CleanUp
Set oWordApp = CreateObject("Word.Application")
Set oWordDoc = oWordApp.documents.Open("...File path..")
oWordDoc.Content.copy
Set oOutApp = CreateObject("Outlook.Application")
For Each cell In sh.Columns.Range("B3:B10000").cells.SpecialCells(xlCellTypeConstants)
Set rng = sh.cells(cell.Row, 1).Range("I1:J1")
        If cell.Value Like "*" And _
           Application.WorksheetFunction.CountA(rng) > 0 Then
Set oMailItem = oOutApp.CreateItem(0)
With oMailItem
    .To = cell.Value
    .cc = cells(cell.Row, "C").Value  'sh.Columns.Range("C3").cells
    .Subject = cells(cell.Row, "F").Value
    .Body = ""
     For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
           If Trim(FileCell) <> "" Then
               If Dir(FileCell.Value) <> "" Then
                 .Attachments.Add FileCell.Value
                  End If
                End If
               .Display
           Next FileCell
            Set oMailWordDoc = oOutApp.ActiveInspector.WordEditor
    Set editor = .GetInspector.WordEditor
    editor.Content.Paste
      .send
End With
End If
Next cell
Set oMailWordDoc = oOutApp.ActiveInspector.WordEditor
CleanUp:
oWordApp.Quit
Set oMailWordDoc = Nothing
Set oMailItem = Nothing
Set oOutApp = Nothing
Set oWordDoc = Nothing
Set oWordApp = Nothing
With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解你。 但我认为你所寻找的是这样的:

strPath = Sheets("Sheet1").Range("A1").value

因此您可以在第一张工作表内的单元格A1中编写路径,VBA代码将使用此路径作为Word文档。