Excel宏创建一个文件并打开它

问题描述 投票:-1回答:1

我在一列有补救票号(如HD0000001006530)。

我要创建每个单元格引用到自己的超链接。单击超链接将运行的宏。

宏必须创建类型.artask与内容的文件像下面并打开它。打开.artask文件将打开补救票HD0000001006530。


[捷径] NAME = HPD:服务台 类型= 0 服务器= remedyprd 票= HD0000001006530 <---该值将来自Excel单元格

excel vba remedy
1个回答
1
投票

在您的macroenabled excel文件,这个代码复制到所选择的“工作表”代码窗格。

           Private Function Createfile(ByVal cellvalue As String)
            Open "c:\" & cellvalue & ".artask" For Output As #1 'your target file name and address. you may change it to the desired folder
            Print #1, "[Shortcut]"
            Print #1, "Name = HPD: HelpDesk"
            Print #1, "Type = 0"
            Print #1, "Server = remedyprd"
            Print #1, "Ticket =" & cellvalue
            Close #1
           End Function

 'EDIT BEGINS:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
Dim cell As Range
 If Dir("C:\" & cellvalue & ".artask") = "" Then 'For memory optimization we should first check if the file exists or not

For Each cell In Range("A1:A" & Me.UsedRange.Rows.Count) 'Specify the Range, in that case it is from A1 to end of the column A
   cell.Select
  ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="c:\" & Selection.Text & ".artask", TextToDisplay:=Selection.Text

    Next cell 'Loop through cells

            Createfile (Selection.Value)
        End If
          End If

      End Sub
    'EDIT ENDS

如果你有任何问题,请让我知道这一点。

© www.soinside.com 2019 - 2024. All rights reserved.